Skip to content

Commit b2f15a2

Browse files
fix: PR review
1 parent 00330c0 commit b2f15a2

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

tests/rules/test_key_ordering.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def test_locale_accents(self):
149149
problem=(3, 1))
150150

151151
def test_ignored_key(self):
152-
conf = 'key-ordering:\n ignored: ["name"]'
152+
conf = 'key-ordering:\n ignored-keys: ["n(am|omm)e"]'
153153
self.check('---\n'
154154
'versions:\n'
155155
' - name: v1alpha1\n'
@@ -158,13 +158,13 @@ def test_ignored_key(self):
158158
' baz: qux\n', conf)
159159
self.check('---\n'
160160
'versions:\n'
161-
' - name: v1alpha1\n'
161+
' - nomme: v1alpha1\n'
162162
' foo: bar\n'
163163
' baz: qux\n'
164-
' - name: v2\n'
164+
' - nomme: v2\n'
165165
' baz: qux\n'
166166
' - baz: qux\n'
167-
' name: v3\n'
167+
' nomme: v3\n'
168168
' value: whatever\n', conf,
169169
problem=(5, 5))
170170
self.check('---\n'

yamllint/rules/key_ordering.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
ordering is case-sensitive and not accent-friendly (see examples below).
2020
This can be changed by setting the global ``locale`` option. This allows one
2121
to sort case and accents properly.
22-
It also allows one to ignore certain keys by setting the ``ignored`` option.
22+
It also allows one to ignore certain keys by setting the ``ignored-keys``
23+
(PCRE regexes list) option.
2324
2425
.. rubric:: Examples
2526
@@ -80,7 +81,7 @@
8081
hais: true
8182
haïssable: true
8283
83-
#. With rule ``key-ordering: {ignored: ["name"]}``
84+
#. With rule ``key-ordering: {ignored-keys: ["name"]}``
8485
8586
the following code snippet would **PASS**:
8687
::
@@ -89,7 +90,7 @@
8990
age: 30
9091
city: New York
9192
"""
92-
93+
import re
9394
from locale import strcoll
9495

9596
import yaml
@@ -98,8 +99,9 @@
9899

99100
ID = 'key-ordering'
100101
TYPE = 'token'
101-
CONF = {'ignored': [str]}
102-
DEFAULT = {'ignored': []}
102+
103+
CONF = {'ignored-keys': [str]}
104+
DEFAULT = {'ignored-keys': []}
103105
MAP, SEQ = range(2)
104106

105107

@@ -128,9 +130,11 @@ def check(conf, token, prev, next, nextnext, context):
128130
# This check is done because KeyTokens can be found inside flow
129131
# sequences... strange, but allowed.
130132
if len(context['stack']) > 0 and context['stack'][-1].type == MAP:
131-
if any(strcoll(next.value, key) < 0
132-
for key in context['stack'][-1].keys
133-
if key not in conf['ignored']):
133+
if any(
134+
strcoll(next.value, key) < 0
135+
for key in context['stack'][-1].keys
136+
if not any(re.search(r, key) for r in conf['ignored-keys'])
137+
):
134138
yield LintProblem(
135139
next.start_mark.line + 1, next.start_mark.column + 1,
136140
f'wrong ordering of key "{next.value}" in mapping')

0 commit comments

Comments
 (0)