Class: Rubocop::Cop::ParenthesesAroundCondition

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

Constant Summary collapse

MSG =
"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

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name, #on_comment

Constructor Details

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

Instance Method Details

#inspect(source, tokens, ast, comments) ⇒ 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(source, tokens, ast, comments)
  on_node([:if, :while, :until], ast) do |node|
    cond, _body = *node

    cond_source = cond.loc.expression.source

    if cond_source.start_with?('(') && cond_source.end_with?(')')
      add_offence(:convetion,
                  cond.loc.line,
                  MSG)
    end
  end
end