Class: RuboCop::Cop::Performance::Caller
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/caller.rb |
Overview
Identifies places where caller[n]
can be replaced by caller(n..n).first
.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/caller.rb', line 23'Use `%<preferred_method>s` instead of `%<current_method>s`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/caller.rb', line 24%i[first []].freeze
Instance Method Summary
- #on_send(node)
- #int_value(node) private
Instance Method Details
#int_value(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/caller.rb', line 61
def int_value(node) node.children[0] end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/performance/caller.rb', line 40
def on_send(node) return unless caller_with_scope_method?(node) method_name = node.receiver.method_name caller_arg = node.receiver.first_argument n = caller_arg ? int_value(caller_arg) : 1 if node.method?(:[]) m = int_value(node.first_argument) n += m end preferred_method = "#{method_name}(#{n}..#{n}).first" = format(MSG, preferred_method: preferred_method, current_method: node.source) add_offense(node, message: ) do |corrector| corrector.replace(node, preferred_method) end end