Class: Rubocop::Cop::SymbolArray

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

Constant Summary collapse

ERROR_MESSAGE =
'Use %i or %I for array of symbols.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

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

Constructor Details

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

Instance Method Details

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/symbol_array.rb', line 8

def inspect(file, source, tokens, sexp)
  # %i and %I were introduced in Ruby 2.0
  unless RUBY_VERSION < '2.0.0'
    each(:array, sexp) do |s|
      array_elems = s[1]

      # no need to check empty arrays
      next unless array_elems && array_elems.size > 1

      symbol_array = array_elems.all? { |e| e[0] == :symbol_literal }

      if symbol_array
        add_offence(:convention,
                    all_positions(s).first.lineno,
                    ERROR_MESSAGE)
      end
    end
  end
end