Sha256: 297f0df3749bd9b35fb0d532fa617c9a6000700ba924402130c7c4d26bd09f6d

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 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
        include RangeHelp

        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, location: 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.57.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.56.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.55.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.54.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.53.0 lib/rubocop/cop/performance/reverse_each.rb