class RuboCop::Cop::Style::NegatedIf
Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:
- both - prefix - postfix
@example
# EnforcedStyle: both # enforces `unless` for `prefix` and `postfix` conditionals # good unless foo bar end # bad if !foo bar end # good bar unless foo # bad bar if !foo
@example
# EnforcedStyle: prefix # enforces `unless` for just `prefix` conditionals # good unless foo bar end # bad if !foo bar end # good bar if !foo
@example
# EnforcedStyle: postfix # enforces `unless` for just `postfix` conditionals # good bar unless foo # bad bar if !foo # good if !foo bar end
Constants
- MSG
Public Instance Methods
on_if(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 82 def on_if(node) return if node.elsif? || node.ternary? return if correct_style?(node) check_negative_conditional(node) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 95 def autocorrect(node) negative_conditional_corrector(node) end
correct_style?(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 99 def correct_style?(node) style == :prefix && node.modifier_form? || style == :postfix && !node.modifier_form? end
message(node)
click to toggle source
# File lib/rubocop/cop/style/negated_if.rb, line 91 def message(node) format(MSG, node.inverse_keyword, node.keyword) end