Sha256: 9e9a66f6582b0e4cf7b29dd4eddf369cf6320032241736d5b4bd8ce355650714

Contents?: true

Size: 924 Bytes

Versions: 5

Compression:

Stored size: 924 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  # Find duplicated keys from YAML.
  module YAMLDuplicationChecker
    def self.check(yaml_string, filename, &on_duplicated)
      # Specify filename to display helpful message when it raises an error.
      tree = YAML.parse(yaml_string, filename)
      return unless tree

      traverse(tree, &on_duplicated)
    end

    def self.traverse(tree, &on_duplicated)
      case tree
      when Psych::Nodes::Mapping
        tree.children.each_slice(2).with_object([]) do |(key, value), keys|
          exist = keys.find { |key2| key2.value == key.value }
          on_duplicated.call(exist, key) if exist
          keys << key
          traverse(value, &on_duplicated)
        end
      else
        children = tree.children
        return unless children

        children.each { |c| traverse(c, &on_duplicated) }
      end
    end

    private_class_method :traverse
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.71.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-0.70.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-0.69.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-0.68.1 lib/rubocop/yaml_duplication_checker.rb
rubocop-0.68.0 lib/rubocop/yaml_duplication_checker.rb