Skip to content

Commit

Permalink
fix: PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezcuesta committed Nov 19, 2024
1 parent 00330c0 commit b2f15a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions tests/rules/test_key_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
20 changes: 12 additions & 8 deletions yamllint/rules/key_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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**:
::
Expand All @@ -89,7 +90,7 @@
age: 30
city: New York
"""

import re
from locale import strcoll

import yaml
Expand All @@ -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)


Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit b2f15a2

Please sign in to comment.