Sha256: 407024dd1424b162e5a59e436384eebde2818a43db32087d8968773a5b9178f3

Contents?: true

Size: 1.76 KB

Versions: 105

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where `caller[n]` can be replaced by `caller(n..n).first`.
      #
      # @example
      #   # bad
      #   caller[1]
      #   caller.first
      #   caller_locations[1]
      #   caller_locations.first
      #
      #   # good
      #   caller(2..2).first
      #   caller(1..1).first
      #   caller_locations(2..2).first
      #   caller_locations(1..1).first
      class Caller < Base
        extend AutoCorrector

        MSG = 'Use `%<preferred_method>s` instead of `%<current_method>s`.'
        RESTRICT_ON_SEND = %i[first []].freeze

        def_node_matcher :slow_caller?, <<~PATTERN
          {
            (send nil? {:caller :caller_locations})
            (send nil? {:caller :caller_locations} int)
          }
        PATTERN

        def_node_matcher :caller_with_scope_method?, <<~PATTERN
          {
            (send #slow_caller? :first)
            (send #slow_caller? :[] int)
          }
        PATTERN

        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"

          message = format(MSG, preferred_method: preferred_method, current_method: node.source)
          add_offense(node, message: message) do |corrector|
            corrector.replace(node, preferred_method)
          end
        end

        private

        def int_value(node)
          node.children[0]
        end
      end
    end
  end
end

Version data entries

105 entries across 103 versions & 7 rubygems

Version Path
rubocop-performance-1.23.1 lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.23.0 lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/caller.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/caller.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/caller.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/caller.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/caller.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/caller.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/caller.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/caller.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/caller.rb