Class: Rubocop::Cop::ParenthesesAroundCondition

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/parentheses_around_condition.rb

Constant Summary collapse

ERROR_MESSAGE =
"Don't use parentheses around the condition of an " +
'if/unless/while/until, unless the condition contains an assignment.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, #has_report?, inherited, #initialize

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubocop/cop/parentheses_around_condition.rb', line 9

def inspect(file, source, tokens, sexp)
  [:if, :elsif, :unless, :while, :until,
   :if_mod, :unless_mod, :while_mod, :until_mod].each do |keyword|
    each(keyword, sexp) do |s|
      if s[1][0] == :paren && s[1][1][0][0] != :assign
        positions = all_positions(s[1])
        if positions.first.lineno == positions.last.lineno
          add_offence(:convention, positions.first.lineno, ERROR_MESSAGE)
        end
      end
    end
  end
end