From b92fc9cb3133673f284ccdef76446673bd81c7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Tue, 4 Apr 2023 18:44:26 +0200 Subject: [PATCH] colons: Prevent error when space before is mandatory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the rare case when the key before `:` is an alias (e.g. `{*x : 4}`), the space before `:` is required (although this requirement is not enforced by PyYAML), the reason being that a colon can be part of an anchor name. Consequently, this commit adapts the `colons` rule to avoid failures when this happens. See this comment from Tina Müller for more details: https://github.com/adrienverge/yamllint/pull/550#discussion_r1155297373 --- tests/rules/test_colons.py | 16 ++++++++++++++++ yamllint/rules/colons.py | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) 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')