class RuboCop::Cop::Naming::MethodName
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'
@example 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)
@example 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)
@example ForbiddenIdentifiers: [‘def’, ‘super’]
# bad def def; end def super; end
@example ForbiddenPatterns: [‘_v1z’, ‘_gen1z’]
# bad def release_v1; end def api_gen1; end
Constants
- MSG
- MSG_FORBIDDEN
Public Instance Methods
Source
# File lib/rubocop/cop/naming/method_name.rb, line 116 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
Also aliased as: on_defs
Source
# File lib/rubocop/cop/naming/method_name.rb, line 106 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) else handle_attr_accessor(node) end end
Private Instance Methods
Source
# File lib/rubocop/cop/naming/method_name.rb, line 191 def attr_name(name_item) sym_name(name_item) || str_name(name_item) end
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
Source
# File lib/rubocop/cop/naming/method_name.rb, line 167 def forbidden_name?(name) forbidden_identifier?(name) || forbidden_pattern?(name) end
Source
# File lib/rubocop/cop/naming/method_name.rb, line 142 def handle_attr_accessor(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
Source
# File lib/rubocop/cop/naming/method_name.rb, line 129 def handle_define_method(node) return unless node.first_argument&.type?(:str, :sym) handle_method_name(node, node.first_argument.value) end
Source
# File lib/rubocop/cop/naming/method_name.rb, line 157 def handle_method_name(node, name) return 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
Source
# File lib/rubocop/cop/naming/method_name.rb, line 135 def handle_new_struct(node) arguments = node.first_argument&.str_type? ? node.arguments[1..] : node.arguments arguments.select { |argument| argument.type?(:sym, :str) }.each do |name| handle_method_name(name, name.value) end end
Source
# File lib/rubocop/cop/naming/method_name.rb, line 206 def message(style) format(MSG, style: style) end
Source
# File lib/rubocop/cop/naming/method_name.rb, line 195 def range_position(node) if node.loc.respond_to?(:selector) selector_end_pos = node.loc.selector.end_pos + 1 expr_end_pos = node.source_range.end_pos range_between(selector_end_pos, expr_end_pos) else node.source_range end end
Source
# File lib/rubocop/cop/naming/method_name.rb, line 172 def register_forbidden_name(node) if node.any_def_type? name_node = node.loc.name method_name = node.method_name elsif node.literal? name_node = node method_name = node.value elsif (attrs = node.attribute_accessor?) name_node = attrs.last.last method_name = attr_name(name_node) else name_node = node.first_argument method_name = node.first_argument.value end message = format(MSG_FORBIDDEN, identifier: method_name) add_offense(name_node, message: message) end
rubocop:disable Metrics/MethodLength, Metrics/AbcSize