Skip to content

Commit

Permalink
key-duplicates: Handle merge keys (<<)
Browse files Browse the repository at this point in the history
Merge keys are described here: http://yaml.org/type/merge.html
They shouldn't be considered as duplicated keys.

Fixes #88
  • Loading branch information
Nick Burke authored and adrienverge committed Feb 28, 2018
1 parent 6a84222 commit 1b37962
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/rules/test_key_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ def test_disabled(self):
' duplicates with\n'
' many styles\n'
': 1\n', conf)
self.check('---\n'
'Merge Keys are OK:\n'
'anchor_one: &anchor_one\n'
' one: one\n'
'anchor_two: &anchor_two\n'
' two: two\n'
'anchor_reference:\n'
' <<: *anchor_one\n'
' <<: *anchor_two\n', conf)

def test_enabled(self):
conf = 'key-duplicates: enable'
Expand Down Expand Up @@ -147,6 +156,15 @@ def test_enabled(self):
': 1\n', conf,
problem1=(3, 1), problem2=(4, 1), problem3=(5, 3),
problem4=(7, 3))
self.check('---\n'
'Merge Keys are OK:\n'
'anchor_one: &anchor_one\n'
' one: one\n'
'anchor_two: &anchor_two\n'
' two: two\n'
'anchor_reference:\n'
' <<: *anchor_one\n'
' <<: *anchor_two\n', conf)

def test_key_tokens_in_flow_sequences(self):
conf = 'key-duplicates: enable'
Expand Down
4 changes: 3 additions & 1 deletion yamllint/rules/key_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def check(conf, token, prev, next, nextnext, context):
# This check is done because KeyTokens can be found inside flow
# sequences... strange, but allowed.
if len(context['stack']) > 0 and context['stack'][-1].type == MAP:
if next.value in context['stack'][-1].keys:
if (next.value in context['stack'][-1].keys and
# `<<` is "merge key", see http://yaml.org/type/merge.html
next.value != '<<'):
yield LintProblem(
next.start_mark.line + 1, next.start_mark.column + 1,
'duplication of key "%s" in mapping' % next.value)
Expand Down

0 comments on commit 1b37962

Please sign in to comment.