Module: RuboCop::Cop::NegativeConditional

Included in:
Style::NegatedIf, Style::NegatedWhile
Defined in:
lib/rubocop/cop/mixin/negative_conditional.rb

Overview

Some common code shared between FavorUnlessOverNegatedIf and FavorUntilOverNegatedWhile.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



9
10
11
# File 'lib/rubocop/cop/mixin/negative_conditional.rb', line 9

def self.included(mod)
  mod.def_node_matcher :single_negative?, '(send !(send _ :!) :!)'
end

Instance Method Details

#check_negative_conditional(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/cop/mixin/negative_conditional.rb', line 13

def check_negative_conditional(node)
  condition, _body, _rest = *node

  # Look at last expression of contents if there are parentheses
  # around condition.
  condition = condition.children.last while condition.type == :begin

  return unless single_negative?(condition) &&
                !(node.loc.respond_to?(:else) && node.loc.else)

  add_offense(node, :expression)
end