diff --git a/tests/rules/test_colons.py b/tests/rules/test_colons.py index 98f92c5a..5467c8be 100644 --- a/tests/rules/test_colons.py +++ b/tests/rules/test_colons.py @@ -256,3 +256,19 @@ def test_both_before_and_after(self): ' property: {a: 1, b: 2, c : 3}\n', conf, problem1=(3, 11), problem2=(4, 4), problem3=(8, 23), problem4=(8, 28)) + + # Although accepted by PyYAML, `{*x: 4}` is not valid YAML: it should be + # noted `{*x : 4}`. The reason is that a colon can be part of an anchor + # name. See commit message for more details. + def test_with_alias_as_key(self): + conf = 'colons: {max-spaces-before: 0, max-spaces-after: 1}' + self.check('---\n' + '- anchor: &a key\n' + '- *a: 42\n' + '- {*a: 42}\n' + '- *a : 42\n' + '- {*a : 42}\n' + '- *a : 42\n' + '- {*a : 42}\n', + conf, + problem1=(7, 6), problem2=(8, 7)) diff --git a/yamllint/rules/colons.py b/yamllint/rules/colons.py index 486746fa..7390e51b 100644 --- a/yamllint/rules/colons.py +++ b/yamllint/rules/colons.py @@ -92,7 +92,9 @@ def check(conf, token, prev, next, nextnext, context): - if isinstance(token, yaml.ValueToken): + if isinstance(token, yaml.ValueToken) and not ( + isinstance(prev, yaml.AliasToken) and + token.start_mark.pointer - prev.end_mark.pointer == 1): problem = spaces_before(token, prev, next, max=conf['max-spaces-before'], max_desc='too many spaces before colon')