Sha256: f765dccdffd2a5de06f5ad45b1002bef0e8485a3724725a881e60a67c081b232

Contents?: true

Size: 1.33 KB

Versions: 14

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Fusuma
  class Config
    # ref: https://github.com/rubocop-hq/rubocop/blob/97e4ffc8a71e9e5239a927c6a534dfc1e0da917f/lib/rubocop/yaml_duplication_checker.rb
    # Find duplicated keys from YAML.
    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
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
fusuma-2.4.1 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.4.0 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.3.0 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.2.0 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.1.0 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.5 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.4 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.3 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.2 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.1 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.0 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.0.pre2 lib/fusuma/config/yaml_duplication_checker.rb
fusuma-2.0.0.pre lib/fusuma/config/yaml_duplication_checker.rb
fusuma-1.11.1 lib/fusuma/config/yaml_duplication_checker.rb