Class: RuboCop::Cop::Naming::MethodName
- 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'
Constant Summary collapse
- MSG =
'Use %<style>s for method names.'
- MSG_FORBIDDEN =
'`%<identifier>s` is forbidden, use another method name instead.'
Constants included from ForbiddenIdentifiers
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Constants included from ConfigurableNaming
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
- #on_send(node) ⇒ Object
- #str_name(node) ⇒ Object
- #sym_name(node) ⇒ Object
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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
permalink #on_def(node) ⇒ Object Also known as: on_defs
[View source]
90 91 92 93 94 95 96 97 98 |
# File 'lib/rubocop/cop/naming/method_name.rb', line 90 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 |
permalink #on_send(node) ⇒ Object
[View source]
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rubocop/cop/naming/method_name.rb', line 75 def on_send(node) return unless (attrs = node.attribute_accessor?) attrs.last.each do |name_item| name = attr_name(name_item) next if !name || matches_allowed_pattern?(name) if forbidden_name?(name.to_s) register_forbidden_name(node) else check_name(node, name, range_position(node)) end end end |
permalink #str_name(node) ⇒ Object
[View source]
73 |
# File 'lib/rubocop/cop/naming/method_name.rb', line 73 def_node_matcher :str_name, '(str $_name)' |
permalink #sym_name(node) ⇒ Object
[View source]
70 |
# File 'lib/rubocop/cop/naming/method_name.rb', line 70 def_node_matcher :sym_name, '(sym $_name)' |