Class: Rubocop::Cop::IfUnlessModifier
- Inherits:
-
Cop
- Object
- Parser::AST::Processor
- Cop
- Rubocop::Cop::IfUnlessModifier
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
#body_length, #check, #length
Methods inherited from Cop
#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #inspect, #name
Instance Method Details
#elsif?(node) ⇒ Boolean
67
68
69
|
# File 'lib/rubocop/cop/favor_modifier.rb', line 67
def elsif?(node)
node.loc.keyword.source == 'elsif'
end
|
#error_message ⇒ Object
42
43
44
45
|
# File 'lib/rubocop/cop/favor_modifier.rb', line 42
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
|
#if_else?(node) ⇒ Boolean
71
72
73
|
# File 'lib/rubocop/cop/favor_modifier.rb', line 71
def if_else?(node)
node.loc.respond_to?(:else) && node.loc.else
end
|
#modifier_if?(node) ⇒ Boolean
63
64
65
|
# File 'lib/rubocop/cop/favor_modifier.rb', line 63
def modifier_if?(node)
node.loc.end.nil?
end
|
#on_if(node) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/rubocop/cop/favor_modifier.rb', line 47
def on_if(node)
return if ternary_op?(node)
return if modifier_if?(node)
return if elsif?(node)
return if if_else?(node)
add_offence(:convention, node.loc.line, error_message) if check(node)
super
end
|
#ternary_op?(node) ⇒ Boolean
59
60
61
|
# File 'lib/rubocop/cop/favor_modifier.rb', line 59
def ternary_op?(node)
node.loc.respond_to?(:question)
end
|