Sha256: 828ca2125676473b36e636277056fd42ecd3a3ab3669fca7b5b39070d059e90e
Contents?: true
Size: 962 Bytes
Versions: 5
Compression:
Stored size: 962 Bytes
Contents
# encoding: utf-8 module RuboCop module Cop module Lint # This cop checks for duplicated keys in hash literals. # # This cop mirrors a warning in Ruby 2.2. # # @example # hash = { food: 'apple', food: 'orange' } class DuplicatedKey < Cop MSG = 'Duplicated key in hash literal.' LITERALS = [:sym, :str, :float, :int] def on_hash(node) keys = [] hash_pairs = *node hash_pairs.each do |pair| key, _value = *pair if keys.include?(key) && LITERALS.include?(key.type) add_offense(key, :expression) elsif keys.include?(key) && key.type == :array key.children.each do |child| return false unless LITERALS.include?(child.type) end add_offense(key, :expression) end keys << key end end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems