Sha256: 35af23b641e782b04df57889c80c3f8d581aa0867508b6667705531080e15a3b

Contents?: true

Size: 1.73 KB

Versions: 38

Compression:

Stored size: 1.73 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 < Base
        include RangeHelp
        extend AutoCorrector

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

        # @!method redundant_with_index?(node)
        def_node_matcher :redundant_with_index?, <<~PATTERN
          (block
            $(send
              _ {:each_with_index :with_index} ...)
            (args
              (arg _))
            ...)
        PATTERN

        def on_block(node)
          return unless (send = redundant_with_index?(node))

          range = with_index_range(send)

          add_offense(range, message: message(send)) do |corrector|
            if send.method?(:each_with_index)
              corrector.replace(send.loc.selector, 'each')
            else
              corrector.remove(range)
              corrector.remove(send.loc.dot)
            end
          end
        end

        private

        def message(node)
          if node.method?(: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

38 entries across 38 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/redundant_with_index.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.29.1 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.29.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.28.2 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.28.1 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.28.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.27.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.26.1 lib/rubocop/cop/lint/redundant_with_index.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.26.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.25.1 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.25.0 lib/rubocop/cop/lint/redundant_with_index.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.24.1 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.24.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.23.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.22.3 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.22.2 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.22.1 lib/rubocop/cop/lint/redundant_with_index.rb