Class: Rubocop::Cop::Style::SymbolName

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

Overview

This cop checks whether symbol names are snake_case. There's also an option to accept CamelCase symbol names as well.

Constant Summary collapse

MSG =
'Use snake_case for symbols.'
SNAKE_CASE =
/^[\da-z_]+[!?=]?$/
CAMEL_CASE =
/^[A-Z][A-Za-z\d]*$/

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

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

Instance Method Details

#allow_camel_case?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/rubocop/cop/style/symbol_name.rb', line 13

def allow_camel_case?
  self.class.config['AllowCamelCase']
end

#on_send(node) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/style/symbol_name.rb', line 17

def on_send(node)
  receiver, method_name, *args = *node
  # Arguments to Module#private_constant are symbols referring to
  # existing constants, so they will start with an upper case letter.
  # We ignore these symbols.
  if receiver.nil? && method_name == :private_constant
    args.each { |a| ignore_node(a) }
  end
end

#on_sym(node) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/style/symbol_name.rb', line 27

def on_sym(node)
  return if ignored_node?(node)
  sym_name = node.to_a[0]
  return unless sym_name =~ /^[a-zA-Z]/
  return if sym_name =~ SNAKE_CASE
  return if allow_camel_case? && sym_name =~ CAMEL_CASE
  add_offence(:convention, node.loc.expression, MSG)
end