Module: RuboCop::YAMLDuplicationChecker Private
Do not use. This module is for internal use only.
Relationships & Source Files | |
Defined in: | lib/rubocop/yaml_duplication_checker.rb |
Overview
Find duplicated keys from YAML.
Class Method Summary
- .check(yaml_string, filename, &on_duplicated) Internal use only
- .traverse(tree, &on_duplicated) private Internal use only
Class Method Details
.check(yaml_string, filename, &on_duplicated)
[ GitHub ]# File 'lib/rubocop/yaml_duplication_checker.rb', line 7
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
.traverse(tree, &on_duplicated) (private)
[ GitHub ]# File 'lib/rubocop/yaml_duplication_checker.rb', line 21
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 } yield(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