Sha256: 6b9bea4484e64c723766afc3d80c08b4d54fc268b97c641f45b796b5585bd5f2

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# encoding: utf-8

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`.'

        def on_send(node)
          receiver, second_method = *node
          return unless second_method == :each
          return if receiver.nil?
          _, first_method = *receiver
          return unless first_method == :reverse

          source_buffer = node.loc.expression.source_buffer
          location_of_reverse = receiver.loc.selector.begin_pos
          end_location = node.loc.selector.end_pos

          range = Parser::Source::Range.new(source_buffer,
                                            location_of_reverse,
                                            end_location)
          add_offense(node, range, MSG)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.dot, '_')
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.30.0 lib/rubocop/cop/performance/reverse_each.rb