Module: RuboCop::Cop::NegativeConditional

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

Overview

Some common code shared between ‘NegatedIf` and `NegatedWhile` cops.

Instance Method Summary collapse

Methods included from NodePattern::Macros

def_node_matcher, def_node_search, node_search, node_search_all, node_search_body, node_search_first

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 = node.condition

  return if empty_condition?(condition)

  condition = condition.children.last while condition.begin_type?

  return unless single_negative?(condition)
  return if node.if_type? && node.else?

  add_offense(node)
end

#negated_condition(node) ⇒ Object



36
37
38
39
40
# File 'lib/rubocop/cop/mixin/negative_conditional.rb', line 36

def negated_condition(node)
  condition = node.condition
  condition = condition.children.first while condition.begin_type?
  condition
end

#negative_conditional_corrector(node) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/mixin/negative_conditional.rb', line 26

def negative_conditional_corrector(node)
  condition = negated_condition(node)

  lambda do |corrector|
    corrector.replace(node.loc.keyword, node.inverse_keyword)
    corrector.replace(condition.source_range,
                      condition.children.first.source)
  end
end