From d2ab96fb487945b9aaf0dd23a29c0f04a76cfd2b Mon Sep 17 00:00:00 2001 From: Hansuk Hong Date: Fri, 17 Aug 2018 23:56:04 +0900 Subject: [PATCH 1/2] Retrieve aliases not for keys Signed-off-by: Hansuk Hong --- lib/yamllint/linter.rb | 2 ++ spec/data/alias_dup_keys.yaml | 6 ++++++ spec/data/alias_dup_values.yaml | 7 +++++++ spec/linter_spec.rb | 8 ++++++++ 4 files changed, 23 insertions(+) create mode 100644 spec/data/alias_dup_keys.yaml create mode 100644 spec/data/alias_dup_values.yaml diff --git a/lib/yamllint/linter.rb b/lib/yamllint/linter.rb index 9823e4d..da2efb9 100644 --- a/lib/yamllint/linter.rb +++ b/lib/yamllint/linter.rb @@ -173,6 +173,8 @@ def parse_recurse(psych_parse_data, is_sequence = false) hash_end(@last_key.last) is_key = false @last_key.pop + when 'Psych::Nodes::Alias' + is_key = false end end end diff --git a/spec/data/alias_dup_keys.yaml b/spec/data/alias_dup_keys.yaml new file mode 100644 index 0000000..c489c30 --- /dev/null +++ b/spec/data/alias_dup_keys.yaml @@ -0,0 +1,6 @@ +--- +foo: &foo +bar: + <<: *foo + key: val + key: val diff --git a/spec/data/alias_dup_values.yaml b/spec/data/alias_dup_values.yaml new file mode 100644 index 0000000..5f8f058 --- /dev/null +++ b/spec/data/alias_dup_values.yaml @@ -0,0 +1,7 @@ +--- +foo: &foo +bar: + <<: *foo + key1: val + key2: val + key3: val diff --git a/spec/linter_spec.rb b/spec/linter_spec.rb index 3442b59..90214f4 100644 --- a/spec/linter_spec.rb +++ b/spec/linter_spec.rb @@ -70,4 +70,12 @@ it 'should be unhapy with a YAML file full of spaces' do expect(linter.check(spec_data('spaces.yaml'))).to be(false) end + + it 'shoud be happy with a YAML file with duplicated values under an alias' do + expect(linter.check(spec_data('alias_dup_values.yaml'))).to be(true) + end + + it 'shoud be unhappy with a YAML file with duplicated keys under an alias' do + expect(linter.check(spec_data('alias_dup_keys.yaml'))).to be(false) + end end From aed1dcb555960bec72b827be3575b85d263ff739 Mon Sep 17 00:00:00 2001 From: Hansuk Hong Date: Fri, 17 Aug 2018 23:59:28 +0900 Subject: [PATCH 2/2] Refactor to compare the classes of nodes Signed-off-by: Hansuk Hong --- lib/yamllint/linter.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/yamllint/linter.rb b/lib/yamllint/linter.rb index da2efb9..99a8491 100644 --- a/lib/yamllint/linter.rb +++ b/lib/yamllint/linter.rb @@ -151,29 +151,29 @@ def parse(psych_parse_data) def parse_recurse(psych_parse_data, is_sequence = false) is_key = false psych_parse_data.children.each do |n| - case n.class.to_s - when 'Psych::Nodes::Scalar' + case n + when Psych::Nodes::Scalar is_key = !is_key unless is_sequence @last_key.push(n.value) if is_key add_value(n.value, @last_key.last) unless is_key msg = "Scalar: #{n.value}, key: #{is_key}, last_key: #{@last_key}" YamlLint.logger.debug { msg } @last_key.pop if !is_key && !is_sequence - when 'Psych::Nodes::Sequence' + when Psych::Nodes::Sequence YamlLint.logger.debug { "Sequence: #{n.children}" } array_start(@last_key.last) parse_recurse(n, true) array_end(@last_key.last) is_key = false @last_key.pop - when 'Psych::Nodes::Mapping' + when Psych::Nodes::Mapping YamlLint.logger.debug { "Mapping: #{n.children}" } hash_start(@last_key.last) parse_recurse(n) hash_end(@last_key.last) is_key = false @last_key.pop - when 'Psych::Nodes::Alias' + when Psych::Nodes::Alias is_key = false end end