Sha256: a47c43dacb20daa3e9538509b1a3a4750e06b18fbc71df898bfcb920fa9b9b4c

Contents?: true

Size: 1.26 KB

Versions: 23

Compression:

Stored size: 1.26 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
        begin_range = block_node.source_range.begin

        if block_node.arguments?
          begin_range.join(argument_node.source_range.end)
        else
          begin_range.join(block_node.loc.begin.end)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 5 rubygems

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