Class: RuboCop::Cop::Naming::MethodName

Inherits:
Base
  • Object
show all
Includes:
AllowedPattern, ConfigurableNaming, ForbiddenIdentifiers, ForbiddenPattern, RangeHelp
Defined in:
lib/rubocop/cop/naming/method_name.rb

Overview

Makes sure that all methods use the configured style, snake_case or camelCase, for their names.

Method names matching patterns are always allowed.

The cop can be configured with ‘AllowedPatterns` to allow certain regexp patterns:

source,yaml

Naming/MethodName:

AllowedPatterns: - '\AonSelectionBulkChange\z' - '\AonSelectionCleared\z' 

As well, you can also forbid specific method names or regexp patterns using ‘ForbiddenIdentifiers` or `ForbiddenPatterns`:

source,yaml

Naming/MethodName:

ForbiddenIdentifiers: - 'def' - 'super' ForbiddenPatterns: - '_v1\z' - '_gen1\z' 

Examples:

EnforcedStyle: snake_case (default)

# bad def fooBar; end # good def foo_bar; end # bad define_method :fooBar do end # good define_method :foo_bar do end # bad Struct.new(:fooBar) # good Struct.new(:foo_bar) # bad alias_method :fooBar, :some_method # good alias_method :foo_bar, :some_method

EnforcedStyle: camelCase

# bad def foo_bar; end # good def fooBar; end # bad define_method :foo_bar do end # good define_method :fooBar do end # bad Struct.new(:foo_bar) # good Struct.new(:fooBar) # bad alias_method :foo_bar, :some_method # good alias_method :fooBar, :some_method

ForbiddenIdentifiers: [‘def’, ‘super’]

# bad def def; end def super; end

ForbiddenPatterns: [‘_v1z’, ‘_gen1z’]

# bad def release_v1; end def api_gen1; end

Constant Summary collapse

MSG =
'Use %<style>s for method names.'
MSG_FORBIDDEN =
'`%<identifier>s` is forbidden, use another method name instead.'
OPERATOR_METHODS =
%i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ +@ -@ !@ ~@ [] []= ! != !~ `].to_set.freeze

Constants included from ForbiddenIdentifiers

ForbiddenIdentifiers::SIGILS

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Constants included from ConfigurableNaming

ConfigurableNaming::FORMATS

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from ForbiddenPattern

#forbidden_pattern?, #forbidden_patterns

Methods included from ForbiddenIdentifiers

#forbidden_identifier?, #forbidden_identifiers

Methods included from ConfigurableFormatting

#check_name, #class_emitter_method?, #report_opposing_styles, #valid_name?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#define_data?(node) ⇒ Object

 122
# File 'lib/rubocop/cop/naming/method_name.rb', line 122 def_node_matcher :define_data?, '(send (const {nil? cbase} :Data) :define ...)'

#new_struct?(node) ⇒ Object

 119
# File 'lib/rubocop/cop/naming/method_name.rb', line 119 def_node_matcher :new_struct?, '(send (const {nil? cbase} :Struct) :new ...)'

#on_alias(node) ⇒ Object

 149 150 151
# File 'lib/rubocop/cop/naming/method_name.rb', line 149 def on_alias(node) handle_method_name(node.new_identifier, node.new_identifier.value) end

#on_def(node) ⇒ Object Also known as: on_defs

 138 139 140 141 142 143 144 145 146
# File 'lib/rubocop/cop/naming/method_name.rb', line 138 def on_def(node) return if node.operator_method? || matches_allowed_pattern?(node.method_name) if forbidden_name?(node.method_name.to_s) register_forbidden_name(node) else check_name(node, node.method_name, node.loc.name) end end

#on_send(node) ⇒ Object

 124 125 126 127 128 129 130 131 132 133 134 135 136
# File 'lib/rubocop/cop/naming/method_name.rb', line 124 def on_send(node) if node.method?(:define_method) || node.method?(:define_singleton_method) handle_define_method(node) elsif new_struct?(node) handle_new_struct(node) elsif define_data?(node) handle_define_data(node) elsif node.method?(:alias_method) handle_alias_method(node) else handle_attr_accessor(node) end end

#str_name(node) ⇒ Object

 116
# File 'lib/rubocop/cop/naming/method_name.rb', line 116 def_node_matcher :str_name, '(str $_name)'

#sym_name(node) ⇒ Object

 113
# File 'lib/rubocop/cop/naming/method_name.rb', line 113 def_node_matcher :sym_name, '(sym $_name)'