Class: Rubocop::Cop::Style::SignalException

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/signal_exception.rb

Overview

This cop checks for uses of fail and raise.

Constant Summary collapse

FAIL_MSG =
'Use `fail` instead of `raise` to signal exceptions.'
RAISE_MSG =
'Use `raise` instead of `fail` to rethrow exceptions.'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#autocorrect(node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/style/signal_exception.rb', line 32

def autocorrect(node)
  @corrections << lambda do |corrector|
    name =
      case style
      when :semantic then command?(:raise, node) ? 'fail' : 'raise'
      when :raise then 'raise'
      when :fail then 'fail'
      end

    corrector.replace(node.loc.selector, name)
  end
end

#on_rescue(node) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/style/signal_exception.rb', line 11

def on_rescue(node)
  if style == :semantic
    begin_node, rescue_node = *node

    check_for(:raise, begin_node)
    check_for(:fail, rescue_node)
    allow(:raise, rescue_node)
  end
end

#on_send(node) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rubocop/cop/style/signal_exception.rb', line 21

def on_send(node)
  case style
  when :semantic
    check_for(:raise, node) unless ignored_node?(node)
  when :raise
    check_for(:raise, node)
  when :fail
    check_for(:fail, node)
  end
end