Class: Rubocop::Cop::Style::IfUnlessModifier
- Inherits:
-
Cop
- Object
- Cop
- Rubocop::Cop::Style::IfUnlessModifier
show all
- Includes:
- FavorModifier
- Defined in:
- lib/rubocop/cop/style/favor_modifier.rb
Overview
Checks for if and unless statements that would fit on one line
if written as a modifier if/unless.
Constant Summary
Constants inherited
from Cop
Cop::OPERATOR_METHODS
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offences, #processed_source
Instance Method Summary
collapse
#body_has_comment?, #body_length, #check, #length, #max_line_length
Methods inherited from Cop
#add_offence, all, #autocorrect?, #autocorrect_action, #convention, #cop_config, #cop_name, cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, rails?, style?, #warning
Instance Method Details
#elsif?(node) ⇒ Boolean
91
92
93
|
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 91
def elsif?(node)
node.loc.keyword.is?('elsif')
end
|
#error_message ⇒ Object
63
64
65
66
|
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 63
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
95
96
97
|
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 95
def if_else?(node)
node.loc.respond_to?(:else) && node.loc.else
end
|
#investigate(processed_source) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 68
def investigate(processed_source)
return unless processed_source.ast
on_node(:if, processed_source.ast) do |node|
next if ternary_op?(node)
next if modifier_if?(node)
next if elsif?(node)
next if if_else?(node)
if check(node, processed_source.)
convention(node, :keyword, error_message)
end
end
end
|
#modifier_if?(node) ⇒ Boolean
87
88
89
|
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 87
def modifier_if?(node)
node.loc.end.nil?
end
|
#ternary_op?(node) ⇒ Boolean
83
84
85
|
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 83
def ternary_op?(node)
node.loc.respond_to?(:question)
end
|