Class: RuboCop::Cop::Style::SendWithLiteralMethodName

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/send_with_literal_method_name.rb

Overview

Detects the use of the ‘public_send` method with a literal method name argument. Since the `send` method can be used to call private methods, by default, only the `public_send` method is detected.

NOTE: Writer methods with names ending in ‘=` are always permitted because their behavior differs as follows:

source,ruby

def foo=(foo)

@foo = foo
42

end

self.foo = 1 # => 1 send(:foo=, 1) # => 42


Examples:

# bad
obj.public_send(:method_name)
obj.public_send('method_name')

# good
obj.method_name

AllowSend: true (default)

# good
obj.send(:method_name)
obj.send('method_name')
obj.__send__(:method_name)
obj.__send__('method_name')

AllowSend: false

# bad
obj.send(:method_name)
obj.send('method_name')
obj.__send__(:method_name)
obj.__send__('method_name')

# good
obj.method_name

Constant Summary collapse

MSG =
'Use `%<method_name>s` method call directly instead.'
RESTRICT_ON_SEND =
%i[public_send send __send__].freeze
STATIC_METHOD_NAME_NODE_TYPES =
%i[sym str].freeze
METHOD_NAME_PATTERN =
/\A[a-zA-Z_][a-zA-Z0-9_]*[!?]?\z/.freeze
RESERVED_WORDS =
%i[
  BEGIN END alias and begin break case class def defined? do else elsif end ensure
  false for if in module next nil not or redo rescue retry return self super then true
  undef unless until when while yield
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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?, #message, #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_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

#on_send(node) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/style/send_with_literal_method_name.rb', line 68

def on_send(node)
  return if allow_send? && !node.method?(:public_send)
  return unless (first_argument = node.first_argument)
  return unless STATIC_METHOD_NAME_NODE_TYPES.include?(first_argument.type)

  offense_range = offense_range(node)
  method_name = first_argument.value
  return if !METHOD_NAME_PATTERN.match?(method_name) || RESERVED_WORDS.include?(method_name)

  add_offense(offense_range, message: format(MSG, method_name: method_name)) do |corrector|
    if node.arguments.one?
      corrector.replace(offense_range, method_name)
    else
      corrector.replace(node.loc.selector, method_name)
      corrector.remove(removal_argument_range(first_argument, node.arguments[1]))
    end
  end
end