Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the ability to ignored keys while sorting #689

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/rules/test_key_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,40 @@ def test_locale_accents(self):
'hais: true\n'
'haïr: true\n', conf,
problem=(3, 1))

def test_ignored_keys(self):
conf = 'key-ordering:\n ignored-keys: ["n(am|omm)e", "^b.*$"]'
self.check('---\n'
'versions:\n'
' - name: v1alpha1\n'
' foo: bar\n'
' - name: v2\n'
' baz: qux\n', conf)
self.check('---\n'
'versions:\n'
' - nomme: v1alpha1\n'
' foo: bar\n'
' baz: qux\n'
' - nomme: v2\n'
' baz: qux\n'
' - baz: qux\n'
' nomme: v3\n'
' value: whatever\n', conf,
problem=(5, 5))
self.check('---\n'
'versions:\n'
' - name: v1alpha1\n'
' baz: qux\n'
' zzz: bad\n'
' foo: bar\n'
' - name: v2\n'
' bar: baz\n'
' - baz: qux\n'
' name: v3\n'
' value: whatever\n', conf,
problem=(6, 5))
self.check('---\n'
'age: 30\n'
'name: John\n'
'address: 123 Street\n', conf,
problem=(4, 1))
35 changes: 32 additions & 3 deletions yamllint/rules/key_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
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-keys``
(PCRE regexes list) option.

.. rubric:: Options

* ``ignored-keys`` is a list of PCRE regexes defining a set of keys to be
ignored while ordering, if they match any regex. Default is an empty list.

.. rubric:: Default values (when enabled)

.. code-block:: yaml

rules:
key-ordering:
ignored-keys: []

.. rubric:: Examples

Expand Down Expand Up @@ -78,8 +93,17 @@
haïr: true
hais: true
haïssable: true
"""

#. With rule ``key-ordering: {ignored-keys: ["name"]}``

the following code snippet would **PASS**:
::

- name: John
age: 30
city: New York
"""
import re
from locale import strcoll

import yaml
Expand All @@ -89,6 +113,8 @@
ID = 'key-ordering'
TYPE = 'token'

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't delete this line

CONF = {'ignored-keys': [str]}
DEFAULT = {'ignored-keys': []}
MAP, SEQ = range(2)


Expand Down Expand Up @@ -117,8 +143,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 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
Loading