Class: Rubocop::Cop::IfUnlessModifier

Inherits:
Cop
  • Object
show all
Includes:
FavorModifier
Defined in:
lib/rubocop/cop/favor_modifier.rb

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods included from FavorModifier

#body_length, #check, #length

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#error_messageObject



45
46
47
48
# File 'lib/rubocop/cop/favor_modifier.rb', line 45

def error_message
  'Favor modifier if/unless usage when you have a single-line body. ' +
    'Another good alternative is the usage of control flow &&/||.'
end

#on_if(node) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/favor_modifier.rb', line 50

def on_if(node)
  # discard ternary ops and modifier if/unless nodes
  return unless node.loc.respond_to?(:keyword) &&
    node.loc.respond_to?(:else)

  add_offence(:convention, node.loc.line, error_message) if check(node)

  super
end