Sha256: 687de022f1e03a84f396b980c11f31353f07d8ffb6aae8155d3575a6ae48194b

Contents?: true

Size: 1.46 KB

Versions: 30

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Sometimes using dig method ends up with just a single
      # argument. In such cases, dig should be replaced with [].
      #
      # @example
      #   # bad
      #   { key: 'value' }.dig(:key)
      #   [1, 2, 3].dig(0)
      #
      #   # good
      #   { key: 'value' }[:key]
      #   [1, 2, 3][0]
      #
      #   # good
      #   { key1: { key2: 'value' } }.dig(:key1, :key2)
      #   [1, [2, [3]]].dig(1, 1)
      #
      #   # good
      #   keys = %i[key1 key2]
      #   { key1: { key2: 'value' } }.dig(*keys)
      #
      class SingleArgumentDig < Base
        extend AutoCorrector

        MSG = 'Use `%<receiver>s[%<argument>s]` instead of `%<original>s`.'
        RESTRICT_ON_SEND = %i[dig].freeze

        def_node_matcher :single_argument_dig?, <<~PATTERN
          (send _ :dig $!splat)
        PATTERN

        def on_send(node)
          return unless node.receiver

          expression = single_argument_dig?(node)
          return unless expression

          receiver = node.receiver.source
          argument = expression.source

          message = format(MSG, receiver: receiver, argument: argument,
                                original: node.source)
          add_offense(node, message: message) do |corrector|
            correct_access = "#{receiver}[#{argument}]"
            corrector.replace(node, correct_access)
          end
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/single_argument_dig.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/single_argument_dig.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/single_argument_dig.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/single_argument_dig.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/single_argument_dig.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.10.0 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.9.1 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.9.0 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.8.1 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.8.0 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.7.0 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.6.1 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.6.0 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.5.2 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.5.1 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.5.0 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.4.2 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.4.1 lib/rubocop/cop/style/single_argument_dig.rb
rubocop-1.4.0 lib/rubocop/cop/style/single_argument_dig.rb