From b2f15a2c0b0d788b5bd757ac1aafd356151e2aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Fern=C3=A1ndez?= <7312236+fernandezcuesta@users.noreply.github.com> Date: Tue, 19 Nov 2024 19:58:06 +0100 Subject: [PATCH] fix: PR review --- tests/rules/test_key_ordering.py | 8 ++++---- yamllint/rules/key_ordering.py | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tests/rules/test_key_ordering.py b/tests/rules/test_key_ordering.py index 1c059570..379677ad 100644 --- a/tests/rules/test_key_ordering.py +++ b/tests/rules/test_key_ordering.py @@ -149,7 +149,7 @@ def test_locale_accents(self): problem=(3, 1)) def test_ignored_key(self): - conf = 'key-ordering:\n ignored: ["name"]' + conf = 'key-ordering:\n ignored-keys: ["n(am|omm)e"]' self.check('---\n' 'versions:\n' ' - name: v1alpha1\n' @@ -158,13 +158,13 @@ def test_ignored_key(self): ' baz: qux\n', conf) self.check('---\n' 'versions:\n' - ' - name: v1alpha1\n' + ' - nomme: v1alpha1\n' ' foo: bar\n' ' baz: qux\n' - ' - name: v2\n' + ' - nomme: v2\n' ' baz: qux\n' ' - baz: qux\n' - ' name: v3\n' + ' nomme: v3\n' ' value: whatever\n', conf, problem=(5, 5)) self.check('---\n' diff --git a/yamllint/rules/key_ordering.py b/yamllint/rules/key_ordering.py index 7cfcf335..3998253d 100644 --- a/yamllint/rules/key_ordering.py +++ b/yamllint/rules/key_ordering.py @@ -19,7 +19,8 @@ ordering is case-sensitive and not accent-friendly (see examples below). This can be changed by setting the global ``locale`` option. This allows one to sort case and accents properly. -It also allows one to ignore certain keys by setting the ``ignored`` option. +It also allows one to ignore certain keys by setting the ``ignored-keys`` +(PCRE regexes list) option. .. rubric:: Examples @@ -80,7 +81,7 @@ hais: true haïssable: true -#. With rule ``key-ordering: {ignored: ["name"]}`` +#. With rule ``key-ordering: {ignored-keys: ["name"]}`` the following code snippet would **PASS**: :: @@ -89,7 +90,7 @@ age: 30 city: New York """ - +import re from locale import strcoll import yaml @@ -98,8 +99,9 @@ ID = 'key-ordering' TYPE = 'token' -CONF = {'ignored': [str]} -DEFAULT = {'ignored': []} + +CONF = {'ignored-keys': [str]} +DEFAULT = {'ignored-keys': []} MAP, SEQ = range(2) @@ -128,9 +130,11 @@ 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 any(strcoll(next.value, key) < 0 - for key in context['stack'][-1].keys - if key not in conf['ignored']): + if any( + strcoll(next.value, key) < 0 + for key in context['stack'][-1].keys + if not any(re.search(r, key) for r in conf['ignored-keys']) + ): yield LintProblem( next.start_mark.line + 1, next.start_mark.column + 1, f'wrong ordering of key "{next.value}" in mapping')