Sha256: 49af9770b7b4096d1fc964df4668af0bbb5802f0bc22c60324f33ef5faa878e0

Contents?: true

Size: 875 Bytes

Versions: 130

Compression:

Stored size: 875 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for duplicated keys in hash literals.
      # This cop considers both primitive types and constants for the hash keys.
      #
      # This cop mirrors a warning in Ruby 2.2.
      #
      # @example
      #
      #   # bad
      #
      #   hash = { food: 'apple', food: 'orange' }
      #
      # @example
      #
      #   # good
      #
      #   hash = { food: 'apple', other_food: 'orange' }
      class DuplicateHashKey < Base
        include Duplication

        MSG = 'Duplicated key in hash literal.'

        def on_hash(node)
          keys = node.keys.select { |key| key.recursive_basic_literal? || key.const_type? }

          return unless duplicates?(keys)

          consecutive_duplicates(keys).each { |key| add_offense(key) }
        end
      end
    end
  end
end

Version data entries

130 entries across 130 versions & 13 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.65.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.64.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.63.4 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.63.3 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.63.2 lib/rubocop/cop/lint/duplicate_hash_key.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.63.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.63.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.62.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.62.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.61.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.60.2 lib/rubocop/cop/lint/duplicate_hash_key.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_hash_key.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_hash_key.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_hash_key.rb