Sha256: 193e5123b147b094f89d6969e5de98c1be6adc7027f414ad6d19b39ae5560c59

Contents?: true

Size: 1.77 KB

Versions: 132

Compression:

Stored size: 1.77 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.source_range
        end
      end

      def offending_range
        replacement_range(end_position)
      end

      def replacement_range(end_pos)
        Parser::Source::Range.new(for_node.source_range.source_buffer,
                                  for_node.source_range.begin_pos,
                                  end_pos)
      end
    end
  end
end

Version data entries

132 entries across 131 versions & 12 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.60.2 lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.60.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.60.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/for_to_each_corrector.rb