Class: Rubocop::Cop::AmpersandsPipesVsAndOr

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

Constant Summary collapse

ERROR_MESSAGE =
'Use &&/|| for boolean expressions, and/or for control flow.'

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

#check(keyword, sexp) ⇒ Object



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

def check(keyword, sexp)
  each(keyword, sexp) do |sub_sexp|
    condition = sub_sexp[1]
    if condition[0] == :binary && [:and, :or].include?(condition[2])
      add_offence(:convention,
                  sub_sexp.flatten.grep(Position).first.lineno,
                  ERROR_MESSAGE)
    end
  end
end

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



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

def inspect(file, source, tokens, sexp)
  [:if, :unless, :while, :until].each { |keyword| check(keyword, sexp) }
end