Class: Rubocop::Cop::MethodAndVariableSnakeCase

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

Constant Summary collapse

ERROR_MESSAGE =
'Use snake_case for methods and variables.'
SNAKE_CASE =
/^@?[\da-z_]+[!?=]?$/
CONSTANT =
/^[A-Z]/

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

#check(type, name, pos) ⇒ Object



25
26
27
28
29
# File 'lib/rubocop/cop/method_and_variable_snake_case.rb', line 25

def check(type, name, pos)
  if [:@ivar, :@ident, :@const].include?(type) && name !~ SNAKE_CASE
    add_offence(:convention, pos.lineno, ERROR_MESSAGE)
  end
end

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



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

def inspect(file, source, tokens, sexp)
  each(:def, sexp) { |s| check(*s[1]) }

  each(:assign, sexp) do |s|
    case s[1][0]
    when :var_field
      check(*s[1][1]) unless s[1][1][1] =~ CONSTANT
    when :field
      if s[1][1][0] == :var_ref && s[1][1][1][0..1] == [:@kw, 'self']
        check(*s[1][3])
      end
    end
  end
end