Sha256: 0ede5f9b4d47db5834b1e8e154a6341e72dde179e49b128bdb0b97c86adf9903

Contents?: true

Size: 1.53 KB

Versions: 19

Compression:

Stored size: 1.53 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)
        offending_range = for_node.source_range.begin.join(end_range)

        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_range
        if for_node.do?
          keyword_begin.end
        else
          collection_end.end
        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
    end
  end
end

Version data entries

19 entries across 19 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.68.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.67.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.66.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.66.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.65.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.65.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.64.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.63.4 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.63.3 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.63.2 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.63.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.63.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.62.1 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.62.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb
rubocop-1.61.0 lib/rubocop/cop/correctors/for_to_each_corrector.rb