Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve aliases not for keys #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/yamllint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,30 @@ 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be n.class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine, though it's a mildly surprising idiom. It's designed to work this way: case...when in Ruby compares with ===, and Module#=== is defined to check whether the object is an instance of the class.

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
is_key = false
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/data/alias_dup_keys.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs to be added to the rakefile under the yamllint_exclude_paths test.

foo: &foo
bar:
<<: *foo
key: val
key: val
7 changes: 7 additions & 0 deletions spec/data/alias_dup_values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
foo: &foo
bar:
<<: *foo
key1: val
key2: val
key3: val
8 changes: 8 additions & 0 deletions spec/linter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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