Sha256: f406317e6fa48ff5286b4a95b4b2a11428c5bc76cc36d7c552080cf6aea99a4d

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 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 < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `reverse_each` instead of `reverse.each`.'
        RESTRICT_ON_SEND = %i[each].freeze

        def_node_matcher :reverse_each?, <<~MATCHER
          (send (send _ :reverse) :each)
        MATCHER

        def on_send(node)
          reverse_each?(node) do
            range = offense_range(node)

            add_offense(range) do |corrector|
              corrector.replace(range, 'reverse_each')
            end
          end
        end

        private

        def offense_range(node)
          range_between(node.children.first.loc.selector.begin_pos, node.loc.selector.end_pos)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-performance-1.10.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-performance-1.10.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-performance-1.9.2 lib/rubocop/cop/performance/reverse_each.rb