Sha256: 78174e445d4475d9fbaa11599bbb52cac932ac1c1825a09b40021ff2a5643a41

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 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)
          ->(corrector) { corrector.replace(node.loc.dot, '_') }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.35.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.34.2 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.34.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.34.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.33.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.32.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.32.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.31.0 lib/rubocop/cop/performance/reverse_each.rb