class RuboCop::Cop::Style::NegatedIf
Checks for uses of if with a negated condition. Only ifs without else are considered.
Constants
- MSG
Public Instance Methods
message(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 20 def message(node) if node.loc.keyword.is?('if') format(MSG, 'unless', 'if') else format(MSG, 'if', 'unless') end end
on_if(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 13 def on_if(node) return unless node.loc.respond_to?(:keyword) return if node.loc.keyword.is?('elsif') check_negative_conditional(node) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 30 def autocorrect(node) lambda do |corrector| condition, _body, _rest = *node # look inside parentheses around the condition condition = condition.children.first while condition.type == :begin # unwrap the negated portion of the condition (a send node) pos_condition, _method, = *condition corrector.replace( node.loc.keyword, node.loc.keyword.is?('if') ? 'unless' : 'if' ) corrector.replace(condition.source_range, pos_condition.source) end end