Class: Laser::RescueExceptionWarning

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

Overview

Warning for rescuing “Exception” or “Object”.

Instance Attribute Summary

Attributes inherited from Warning

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

Instance Method Summary (collapse)

Methods inherited from FileWarning

options

Instance Method Details

- (Object) fix(body = self.body)



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/laser/warnings/rescue_exception.rb', line 31

def fix(body = self.body)
  result = ""
  all_lines = 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

- (Boolean) match?(body = self.body)

Returns:

  • (Boolean)


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

def match?(body = self.body)
  find_sexps(:rescue).map do |_, types, name|
    next if types.nil?
    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 = Laser::RescueExceptionWarning.new(file, body, position: type[1][2])
        warning.position[0] -= 1
        warning.line_number = type[1][2][1]
        warning
      end
    end
  end.flatten.compact
end