Sha256: 307b65d8828117f10801ed520d30ca301809f8d75c3addee38d8dac7f3890951

Contents?: true

Size: 1.78 KB

Versions: 36

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

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

      CORRECTION = '%<collection>s.each do |%<argument>s|'

      def initialize(for_node)
        @for_node        = for_node
        @variable_node   = for_node.variable
        @collection_node = for_node.collection
      end

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

      private

      attr_reader :for_node, :variable_node, :collection_node

      def correction
        format(CORRECTION, collection: collection_source, argument: variable_node.source)
      end

      def collection_source
        if requires_parentheses?
          "(#{collection_node.source})"
        else
          collection_node.source
        end
      end

      def requires_parentheses?
        return true if collection_node.send_type? && collection_node.operator_method?

        collection_node.range_type? || collection_node.or_type? || collection_node.and_type?
      end

      def end_position
        if for_node.do?
          keyword_begin.end_pos
        else
          collection_end.end_pos
        end
      end

      def keyword_begin
        for_node.loc.begin
      end

      def collection_end
        if collection_node.begin_type?
          collection_node.loc.end
        else
          collection_node.loc.expression
        end
      end

      def offending_range
        replacement_range(end_position)
      end

      def replacement_range(end_pos)
        Parser::Source::Range.new(for_node.loc.expression.source_buffer,
                                  for_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/for_to_each_corrector.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.46.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.45.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.45.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.44.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.44.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.43.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.42.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.41.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.41.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.40.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb