Class: RuboCop::Cop::Style::UnlessMultipleConditions Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, T::Sig
Defined in:
brew/Library/Homebrew/rubocops/unless_multiple_conditions.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This cop checks that unless is not used with multiple conditions.

Constant Summary collapse

MSG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"Avoid using `unless` with multiple conditions."

Instance Method Summary collapse

Instance Method Details

#on_if(node) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::IfNode)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'brew/Library/Homebrew/rubocops/unless_multiple_conditions.rb', line 17

def on_if(node)
  return if !node.unless? || (!node.condition.and_type? && !node.condition.or_type?)

  add_offense(node.condition.source_range.with(begin_pos: node.loc.keyword.begin_pos)) do |corrector|
    corrector.replace(node.loc.keyword, "if")
    corrector.replace(node.condition.loc.operator, node.condition.inverse_operator)
    [node.condition.lhs, node.condition.rhs].each do |subcondition|
      if !subcondition.source.start_with?("(") || !subcondition.source.end_with?(")")
        corrector.insert_before(subcondition, "(")
        corrector.insert_after(subcondition, ")")
      end
      corrector.insert_before(subcondition, "!")
    end
  end
end