123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Performance::StringIdentifierArgument

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, AutoCorrector, Base
Instance Chain:
self, Base
Inherits: Base
  • Object
Defined in: lib/rubocop/cop/performance/string_identifier_argument.rb

Overview

Identifies places where string identifier argument can be replaced by symbol identifier argument. It prevents the redundancy of the internal string-to-symbol conversion.

This cop targets methods that take identifier (e.g. method name) argument and the following examples are parts of it.

Examples:

# bad
send('do_something')
attr_accessor 'do_something'
instance_variable_get('@ivar')
respond_to?("string_#{interpolation}")

# good
send(:do_something)
attr_accessor :do_something
instance_variable_get(:@ivar)
respond_to?(:"string_#{interpolation}")

# good - these methods don't support namespaced symbols
const_get("#{module_path}::Base")
const_source_location("#{module_path}::Base")
const_defined?("#{module_path}::Base")

Constant Summary

Instance Method Summary

Instance Method Details

#argument_replacement(node, value) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/string_identifier_argument.rb', line 105

def argument_replacement(node, value)
  if node.str_type?
    value.to_sym.inspect
  else
    ":\"#{value.to_sym}\""
  end
end

#on_send(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/string_identifier_argument.rb', line 64

def on_send(node)
  return if COMMAND_METHODS.include?(node.method_name) && node.receiver

  string_arguments(node).each do |string_argument|
    string_argument_value = string_argument.value
    next if string_argument_value.include?(' ') || string_argument_value.include?('::')

    register_offense(string_argument, string_argument_value)
  end
end

#register_offense(argument, argument_value) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/string_identifier_argument.rb', line 95

def register_offense(argument, argument_value)
  replacement = argument_replacement(argument, argument_value)

  message = format(MSG, symbol_arg: replacement, string_arg: argument.source)

  add_offense(argument, message: message) do |corrector|
    corrector.replace(argument, replacement)
  end
end

#string_argument_compatible?(argument, node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/string_identifier_argument.rb', line 89

def string_argument_compatible?(argument, node)
  return true if argument.str_type?

  argument.dstr_type? && INTERPOLATION_IGNORE_METHODS.none? { |method| node.method?(method) }
end

#string_arguments(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/string_identifier_argument.rb', line 77

def string_arguments(node)
  arguments = if node.method?(TWO_ARGUMENTS_METHOD)
                [node.first_argument, node.arguments[1]]
              elsif MULTIPLE_ARGUMENTS_METHODS.include?(node.method_name)
                node.arguments
              else
                [node.first_argument]
              end

  arguments.compact.filter { |argument| string_argument_compatible?(argument, node) }
end