Sha256: 5a9104c01da764b88ab255c335bb098aa21170d0f88142433684e5d47c78325a

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for redundant `with_index`.
      #
      # @example
      #   # bad
      #   ary.each_with_index do |v|
      #     v
      #   end
      #
      #   # good
      #   ary.each do |v|
      #     v
      #   end
      #
      #   # bad
      #   ary.each.with_index do |v|
      #     v
      #   end
      #
      #   # good
      #   ary.each do |v|
      #     v
      #   end
      #
      class RedundantWithIndex < Cop
        include RangeHelp

        MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'
        MSG_WITH_INDEX = 'Remove redundant `with_index`.'

        def_node_matcher :redundant_with_index?, <<-PATTERN
          (block
            $(send
              _ {:each_with_index :with_index} ...)
            (args
              (arg _))
            ...)
        PATTERN

        def on_block(node)
          redundant_with_index?(node) do |send|
            add_offense(node, location: with_index_range(send))
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            redundant_with_index?(node) do |send|
              if send.method_name == :each_with_index
                corrector.replace(send.loc.selector, 'each')
              else
                corrector.remove(with_index_range(send))
                corrector.remove(send.loc.dot)
              end
            end
          end
        end

        private

        def message(node)
          if node.method_name == :each_with_index
            MSG_EACH_WITH_INDEX
          else
            MSG_WITH_INDEX
          end
        end

        def with_index_range(send)
          range_between(
            send.loc.selector.begin_pos,
            send.loc.expression.end_pos
          )
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-0.72.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-0.71.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-0.70.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-0.69.0 lib/rubocop/cop/lint/redundant_with_index.rb