Class: Rubocop::Cop::SymbolArray

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

Constant Summary collapse

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

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



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

def inspect(source, tokens, ast, comments)
  # %i and %I were introduced in Ruby 2.0
  unless RUBY_VERSION < '2.0.0'
    on_node(:array, ast) do |s|
      next unless s.loc.begin && s.loc.begin.source == '['

      array_elems = s.children

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

      symbol_array = array_elems.all? { |e| e.type == :sym }

      if symbol_array
        add_offence(:convention,
                    s.loc.line,
                    MSG)
      end
    end
  end
end