Sha256: ef8fcbb64cd205a4dd0fd99ba7c83f395b7f54efb43ff218d1b6653df8c71601

Contents?: true

Size: 1.13 KB

Versions: 56

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RuboCop
  # Find duplicated keys from YAML.
  # @api private
  module YAMLDuplicationChecker
    def self.check(yaml_string, filename, &on_duplicated)
      # Ruby 2.6+
      tree = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
               # Specify filename to display helpful message when it raises
               # an error.
               YAML.parse(yaml_string, filename: filename)
             else
               YAML.parse(yaml_string, filename)
             end
      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

56 entries across 56 versions & 5 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/yaml_duplication_checker.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/yaml_duplication_checker.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/yaml_duplication_checker.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/yaml_duplication_checker.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/yaml_duplication_checker.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/yaml_duplication_checker.rb
rubocop-1.22.3 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.22.2 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.22.1 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.22.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.21.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.20.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.19.1 lib/rubocop/yaml_duplication_checker.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/yaml_duplication_checker.rb
rubocop-1.19.0 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.18.4 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.18.3 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.18.2 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.18.1 lib/rubocop/yaml_duplication_checker.rb
rubocop-1.18.0 lib/rubocop/yaml_duplication_checker.rb