key-duplicates: Handle merge keys (<<)

Merge keys are described here: http://yaml.org/type/merge.html
They shouldn't be considered as duplicated keys.

Fixes https://github.com/adrienverge/yamllint/issues/88
pull/98/head
Nick Burke 7 years ago committed by Adrien Vergé
parent 6a842229fd
commit 1b379628d7

@ -78,6 +78,15 @@ class KeyDuplicatesTestCase(RuleTestCase):
' 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'
@ -147,6 +156,15 @@ class KeyDuplicatesTestCase(RuleTestCase):
': 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'

@ -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)

Loading…
Cancel
Save