Sha256: 59cb5cfaee122c5c6f1bc639a957eb9eb4d244b660255ddd84d1233794df186c

Contents?: true

Size: 1.72 KB

Versions: 12

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where `reverse.first(n)` and `reverse.first`
      # can be replaced by `last(n).reverse` and `last`.
      #
      # @example
      #
      #   # bad
      #   array.reverse.first(5)
      #   array.reverse.first
      #
      #   # good
      #   array.last(5).reverse
      #   array.last
      #
      class ReverseFirst < Base
        include RangeHelp
        extend AutoCorrector

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

        def_node_matcher :reverse_first_candidate?, <<~PATTERN
          (call $(call _ :reverse) :first (int _)?)
        PATTERN

        def on_send(node)
          reverse_first_candidate?(node) do |receiver|
            range = correction_range(receiver, node)
            message = build_message(node, range)

            add_offense(range, message: message) do |corrector|
              replacement = build_good_method(node)

              corrector.replace(range, replacement)
            end
          end
        end
        alias on_csend on_send

        private

        def correction_range(receiver, node)
          range_between(receiver.loc.selector.begin_pos, node.source_range.end_pos)
        end

        def build_message(node, range)
          good_method = build_good_method(node)
          bad_method = range.source
          format(MSG, good_method: good_method, bad_method: bad_method)
        end

        def build_good_method(node)
          if node.arguments?
            "last(#{node.first_argument.source})#{node.loc.dot.source}reverse"
          else
            'last'
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
rubocop-performance-1.23.1 lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.23.0 lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/reverse_first.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/reverse_first.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/reverse_first.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.20.1/lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.20.1 lib/rubocop/cop/performance/reverse_first.rb
rubocop-performance-1.20.0 lib/rubocop/cop/performance/reverse_first.rb