Class: Rubocop::Cop::SymbolSnakeCase

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

Constant Summary collapse

ERROR_MESSAGE =
'Use snake_case for symbols.'
SNAKE_CASE =
/^@?[\da-z_]+[!?=]?$/
OPERATORS =
%w(! + - % * ** [])

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



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

def inspect(file, source, tokens, sexp)
  each(:symbol_literal, sexp) do |s|
    symbol_ident = s[1][1][1]

    # handle a couple of special cases
    next if OPERATORS.include?(symbol_ident)

    unless symbol_ident =~ SNAKE_CASE
      line_no = s[1][1][2].lineno
      add_offence(:convention,
                  line_no,
                  ERROR_MESSAGE)
    end
  end
end