class Rubocop::Cop::Style::MethodAndVariableSnakeCase

This cop makes sure that all methods and variables use snake_case for their names. Some special arrangements have to be made for operator methods.

Constants

MSG
OPERATOR_METHODS

phrogz.net/programmingruby/language.html#table_18.4

SNAKE_CASE

Public Instance Methods

inspect(source_buffer, source, tokens, node, comments) click to toggle source
# File lib/rubocop/cop/style/method_and_variable_snake_case.rb, line 19
def inspect(source_buffer, source, tokens, node, comments)
  return unless node
  on_node([:def, :defs, :lvasgn, :ivasgn, :send], node) do |n|
    name = case n.type
           when :def
             name_of_instance_method(n)
           when :defs
             name_of_singleton_method(n)
           when :lvasgn, :ivasgn
             name_of_variable(n)
           when :send
             name_of_setter(n)
           end

    next unless name
    next if name =~ SNAKE_CASE || OPERATOR_METHODS.include?(name)

    add_offence(:convention, n.location.expression, MSG)
  end
end
name_of_instance_method(def_node) click to toggle source
# File lib/rubocop/cop/style/method_and_variable_snake_case.rb, line 40
def name_of_instance_method(def_node)
  def_node.children.first
end
name_of_setter(send_node) click to toggle source
# File lib/rubocop/cop/style/method_and_variable_snake_case.rb, line 52
def name_of_setter(send_node)
  receiver, method_name = *send_node
  return nil unless receiver && receiver.type == :self
  return nil unless method_name.to_s.end_with?('=')
  method_name
end
name_of_singleton_method(defs_node) click to toggle source
# File lib/rubocop/cop/style/method_and_variable_snake_case.rb, line 44
def name_of_singleton_method(defs_node)
  defs_node.children[1]
end
name_of_variable(vasgn_node) click to toggle source
# File lib/rubocop/cop/style/method_and_variable_snake_case.rb, line 48
def name_of_variable(vasgn_node)
  vasgn_node.children.first
end