Class: Wool::RescueExceptionWarning

Inherits:
FileWarning show all
Defined in:
lib/wool/warnings/rescue_exception.rb

Overview

Warning for rescuing “Exception” or “Object”.

Instance Attribute Summary

Attributes inherited from Warning

#body, #file, #line_number, #name, #settings, #severity

Instance Method Summary collapse

Methods inherited from FileWarning

options

Methods inherited from Warning

all_types, all_warnings, concrete_warnings, #count_occurrences, #desc, #fixable?, #generated_warnings, #get_indent, #indent, inherited, #initialize, opt, options, setting_accessor, type

Methods included from Advice

#advice_counter, #after_advice, #argument_advice, #before_advice, #bump_advice_counter!, #with_advice

Methods included from ModuleExtensions

#attr_accessor_with_default, #cattr_accessor, #cattr_accessor_with_default, #cattr_get_and_setter, #cattr_reader, #cattr_writer

Methods included from SexpAnalysis

#find_sexps, #parse

Methods included from LexicalAnalysis

#find_keyword, #find_token, #lex, #select_token, #split_on_keyword, #split_on_token, #text_between_token_positions

Constructor Details

This class inherits a constructor from Wool::Warning

Instance Method Details

#fixObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wool/warnings/rescue_exception.rb', line 29

def fix
  result = ""
  all_lines = self.body.lines.to_a
  result << all_lines[0..position[0]-1].join if position[0]-1 >= 0
  result << all_lines[position[0]][0,position[1]]
  result << 'StandardError'
  if trailing = all_lines[position[0]][position[1] + 'Exception'.size .. -1]
    result << trailing
  end
  result << all_lines[position[0]+1..-1].join if position[0]+1 < all_lines.size
  result
end

#match?(file = self.body) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wool/warnings/rescue_exception.rb', line 9

def match?(file = self.body)
  find_sexps(:rescue).map do |_, types, name|
    case types[0]
    when :mrhs_new_from_args
      list = types[1] + types[2..-1]
    when Array
      list = types
    end
    list.map do |type|
      if type[0] == :var_ref &&
         type[1][0] == :@const && type[1][1] == "Exception"
        warning = RescueExceptionWarning.new(file, body, :position => type[1][2])
        warning.position[0] -= 1
        warning.line_number = type[1][2][1]
        warning
      end
    end.compact
  end.flatten
end