Class: Rubocop::Cop::Style::MethodName

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

Overview

This cop makes sure that all methods use the configured style, snake_case or camelCase, for their names. Some special arrangements have to be made for operator methods.

Constant Summary

Constants included from ConfigurableNaming

ConfigurableNaming::CAMEL_CASE, ConfigurableNaming::SNAKE_CASE

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods included from ConfigurableNaming

#after_dot, #check, #matches_config?

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

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

Instance Method Details

#message(style) ⇒ Object



36
37
38
# File 'lib/rubocop/cop/style/method_name.rb', line 36

def message(style)
  format('Use %s for methods.', style)
end

#name_of_instance_method(def_node) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/style/method_name.rb', line 20

def name_of_instance_method(def_node)
  expr = def_node.loc.expression
  match = /^def(\s+)([\w]+[!?=]?\b)/.match(expr.source)
  return unless match
  space, method_name = match.captures
  begin_pos = expr.begin_pos + 'def'.length + space.length
  Parser::Source::Range.new(expr.source_buffer, begin_pos,
                            begin_pos + method_name.length)
end

#name_of_singleton_method(defs_node) ⇒ Object



30
31
32
33
34
# File 'lib/rubocop/cop/style/method_name.rb', line 30

def name_of_singleton_method(defs_node)
  scope, method_name, _args, _body = *defs_node
  after_dot(defs_node, method_name.length,
            "def\s+" + Regexp.escape(scope.loc.expression.source))
end

#on_def(node) ⇒ Object



12
13
14
# File 'lib/rubocop/cop/style/method_name.rb', line 12

def on_def(node)
  check(node, name_of_instance_method(node))
end

#on_defs(node) ⇒ Object



16
17
18
# File 'lib/rubocop/cop/style/method_name.rb', line 16

def on_defs(node)
  check(node, name_of_singleton_method(node))
end