Sha256: 7bd74a052801451a4572ab84c9bb3142c62acd4cd538b97382cd098e9b890fa1

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

# encoding: utf-8
# 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
        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|
            source_buffer = node.source_range.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
        end

        def autocorrect(node)
          ->(corrector) { corrector.replace(node.loc.dot, UNDERSCORE) }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/reverse_each.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.42.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.41.2 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.41.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.41.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.40.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.39.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.38.0 lib/rubocop/cop/performance/reverse_each.rb