Sha256: 71767c7ea9fc7109076e1a7257b7d392ab8ad3e171e1a49d9e90a8dde81fe35a

Contents?: true

Size: 949 Bytes

Versions: 6

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  # Find duplicated keys from YAML.
  # @api private
  module YAMLDuplicationChecker
    def self.check(yaml_string, filename, &on_duplicated)
      handler = DuplicationCheckHandler.new(&on_duplicated)
      parser = Psych::Parser.new(handler)
      parser.parse(yaml_string, filename)
      parser.handler.root.children[0]
    end

    class DuplicationCheckHandler < Psych::TreeBuilder # :nodoc:
      def initialize(&block)
        super()
        @block = block
      end

      def end_mapping
        mapping_node = super
        # OPTIMIZE: Use a hash for faster lookup since there can
        # be quite a few keys at the top-level.
        keys = {}
        mapping_node.children.each_slice(2) do |key, _value|
          duplicate = keys[key.value]
          @block.call(duplicate, key) if duplicate
          keys[key.value] = key
        end
        mapping_node
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.69.2 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.69.1 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.69.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.68.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.67.0 lib/rubocop/yaml_duplication_checker.rb