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.67.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.67.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.66.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-performance-1.0.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.65.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.64.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.63.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.63.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.62.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.61.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.61.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-performance-0.0.1 lib/rubocop/cop/performance/reverse_each.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/reverse_each.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/reverse_each.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/reverse_each.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.60.0 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.59.2 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.59.1 lib/rubocop/cop/performance/reverse_each.rb
rubocop-0.59.0 lib/rubocop/cop/performance/reverse_each.rb