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 included from Util

Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods included from ConfigurableNaming

#after_dot, #check, #matches_config?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #both_styles_detected, #correct_style_detected, #opposite_style_detected, #parameter_name, #style, #unrecognized_style_detected

Methods inherited from Cop

#add_offense, all, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_type, #debug?, #display_cop_names?, #exclude_file?, #include_file?, inherited, #initialize, #join_force?, lint?, non_rails, qualified_cop_name, rails?, #relevant_file?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, numeric_range_size, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes

Methods included from PathUtil

match_path?, relative_path

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