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
cybrid_api_organization_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_bank_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_id_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_organization_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_bank_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_organization_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_id_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_bank_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_id_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_organization_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
ory-client-1.15.17 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_bank_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_organization_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_id_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_organization_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_bank_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_id_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_organization_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_bank_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb
cybrid_api_id_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/reverse_each.rb