Sha256: e0592790548b8621eebb6096c7b4c9d61b90f7f1731cf9adf657db8e9f469aba

Contents?: true

Size: 1.5 KB

Versions: 6770

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This class auto-corrects `#each` enumeration to `for` iteration.
    class EachToForCorrector
      extend NodePattern::Macros

      CORRECTION_WITH_ARGUMENTS =
        'for %<variables>s in %<collection>s do'.freeze
      CORRECTION_WITHOUT_ARGUMENTS = 'for _ in %<enumerable>s do'.freeze

      def initialize(block_node)
        @block_node = block_node
        @collection_node = block_node.send_node.receiver
        @argument_node = block_node.arguments
      end

      def call(corrector)
        corrector.replace(offending_range, correction)
      end

      private

      attr_reader :block_node, :collection_node, :argument_node

      def correction
        if block_node.arguments?
          format(CORRECTION_WITH_ARGUMENTS,
                 collection: collection_node.source,
                 variables: argument_node.children.first.source)
        else
          format(CORRECTION_WITHOUT_ARGUMENTS,
                 enumerable: collection_node.source)
        end
      end

      def offending_range
        if block_node.arguments?
          replacement_range(argument_node.loc.expression.end_pos)
        else
          replacement_range(block_node.loc.begin.end_pos)
        end
      end

      def replacement_range(end_pos)
        Parser::Source::Range.new(block_node.loc.expression.source_buffer,
                                  block_node.loc.expression.begin_pos,
                                  end_pos)
      end
    end
  end
end

Version data entries

6,770 entries across 6,766 versions & 23 rubygems

Version Path
cybrid_api_bank_ruby-0.123.149 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_id_ruby-0.123.149 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_organization_ruby-0.123.149 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_bank_ruby-0.123.148 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_id_ruby-0.123.148 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_organization_ruby-0.123.148 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_bank_ruby-0.123.147 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_organization_ruby-0.123.147 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_id_ruby-0.123.147 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
ory-client-1.16.2 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
avalara_sdk-24.12.2 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_bank_ruby-0.123.145 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_organization_ruby-0.123.145 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_id_ruby-0.123.145 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_bank_ruby-0.123.144 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_id_ruby-0.123.144 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_organization_ruby-0.123.144 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
ory-client-1.16.1 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
ory-client-1.16.0 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cybrid_api_bank_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb