Sha256: 58885296e606e4c671095f38d281455edd159333dd15439172b0fcd5ff8e6f57

Contents?: true

Size: 1.04 KB

Versions: 6787

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 _ :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

6,787 entries across 6,781 versions & 26 rubygems

Version Path
rubocop-0.58.2 lib/rubocop/cop/performance/reverse_each.rb
rubocop-performance-0.0.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.58.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.58.0 lib/rubocop/cop/performance/reverse_each.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.57.2 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.57.1 lib/rubocop/cop/performance/reverse_each.rb