Sha256: a4e5a3e5167df9f62e3181ffab5104570d7d80e912b459b094e41edb10e608f4

Contents?: true

Size: 1.45 KB

Versions: 36

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

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

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

      def initialize(block_node)
        @block_node = block_node
        @collection_node = block_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

36 entries across 32 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/each_to_for_corrector.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/each_to_for_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/correctors/each_to_for_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/each_to_for_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/correctors/each_to_for_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/each_to_for_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.46.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.45.1 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.45.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.44.1 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.44.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.43.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.42.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.41.1 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.41.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb
rubocop-1.40.0 lib/rubocop/cop/correctors/each_to_for_corrector.rb