Sha256: 58aadb95d258a738d4e75a03d2de1cac20c544f7ea359181fb3549f19b6fa5a8

Contents?: true

Size: 1.79 KB

Versions: 6840

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop 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 < Cop
        MSG_BRACE = 'Use `%<method>s(%<n>d..%<n>d).first`' \
                    ' instead of `%<method>s[%<m>d]`.'.freeze
        MSG_FIRST = 'Use `%<method>s(%<n>d..%<n>d).first`' \
                    ' instead of `%<method>s.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)

          add_offense(node)
        end

        private

        def message(node)
          method_name = node.receiver.method_name
          caller_arg = node.receiver.first_argument
          n = caller_arg ? int_value(caller_arg) : 1

          if node.method_name == :[]
            m = int_value(node.first_argument)
            n += m
            format(MSG_BRACE, n: n, m: m, method: method_name)
          else
            format(MSG_FIRST, n: n, method: method_name)
          end
        end

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

Version data entries

6,840 entries across 6,834 versions & 27 rubygems

Version Path
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/caller.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.2.0 lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.1.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.67.2 lib/rubocop/cop/performance/caller.rb
rubocop-0.67.1 lib/rubocop/cop/performance/caller.rb
rubocop-0.67.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.66.0 lib/rubocop/cop/performance/caller.rb
rubocop-performance-1.0.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.65.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.64.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.63.1 lib/rubocop/cop/performance/caller.rb
rubocop-0.63.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.62.0 lib/rubocop/cop/performance/caller.rb
rubocop-0.61.1 lib/rubocop/cop/performance/caller.rb
rubocop-0.61.0 lib/rubocop/cop/performance/caller.rb
rubocop-performance-0.0.1 lib/rubocop/cop/performance/caller.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/caller.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/caller.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/caller.rb