Sha256: 64448cd881bbc27fade92839e5d5a88aaddfa9749e74a4b50262077ca78dbe87

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop is used to identify usages of `reverse.each` and
      # change them to use `reverse_each` instead.
      #
      # @example
      #   # bad
      #   [].reverse.each
      #
      #   # good
      #   [].reverse_each
      class ReverseEach < Cop
        MSG = 'Use `reverse_each` instead of `reverse.each`.'.freeze
        UNDERSCORE = '_'.freeze

        def_node_matcher :reverse_each?, <<-MATCHER
          (send $(send array :reverse) :each)
        MATCHER

        def on_send(node)
          reverse_each?(node) do |receiver|
            location_of_reverse = receiver.loc.selector.begin_pos
            end_location = node.loc.selector.end_pos

            range = range_between(location_of_reverse, end_location)

            add_offense(node, range)
          end
        end

        def autocorrect(node)
          ->(corrector) { corrector.replace(node.loc.dot, UNDERSCORE) }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.50.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.49.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.49.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.48.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.48.0 lib/rubocop/cop/performance/reverse_each.rb