-
Notifications
You must be signed in to change notification settings - Fork 278
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello J.M., thanks for contributing!
The idea looks good. I propose these modifications:
- Name the new option
ignored-keys
for explicitness (if we add other "ignored" options in the future). - Accept regexes instead of fixed strings, for more powerful usage. See for example the rule
quoted-strings
with its optionsextra-required
andextra-allowed
.
Also, can you include more tests? Especially for more than 1 ignored-keys
, and with the regex feature we should also check it, e.g. name
, aaa name
and name zzz
for ignored-keys: ['name']
vs. ignored-keys: ['^name$']
.
Finally, you need to add a .. rubric:: Options
in documentation (see other rules for examples).
@@ -88,7 +98,8 @@ | |||
|
|||
ID = 'key-ordering' | |||
TYPE = 'token' | |||
|
There was a problem hiding this comment.
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
I'm very motivated to get this merged and I see @fernandezcuesta hasn't responded in over a month. Would any be opposed to me making the reviewer comment changes in a new PR? /cc @adrienverge FYI my use case is GitHub Actions. Sorting some of the keys there makes things more confusing/harder to maintain for humans (read: bad). |
I'll do it, apologies for the long delay |
1a49252
to
b2f15a2
Compare
f7a0867
to
ffafaed
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Jesús,
Thanks for the changes.
While trying to improve test coverage (see my comment below) I found a bug with this implementation: with ignored-keys: ["n(a|o)me", "^b"]
, it reports the line boat
as an error, while it shouldn't:
a:
b:
c:
name: ignored
first-name: ignored
nome: ignored
gnomes: ignored
d:
e:
boat: ignored
.boat: ERROR
call: ERROR
f:
g:
This is because you keep adding the ignored keys to the list of previous keys to compare (context['stack'][-1].keys
).
Instead of, I think your commit should simply do:
@@ -116,7 +142,9 @@ def check(conf, token, prev, next, nextnext, context):
isinstance(next, yaml.ScalarToken)):
# 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 (len(context['stack']) > 0 and context['stack'][-1].type == MAP and
+ not any(re.search(r, next.value)
+ for r in conf['ignored-keys'])):
if any(strcoll(next.value, key) < 0
for key in context['stack'][-1].keys):
yield LintProblem(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fernandezcuesta it looks like you didn't check all the comments from my last review.
Would any be opposed to me making the reviewer comment changes in a new PR? /cc @adrienverge
@fearphage feel free if needed!
yamllint/rules/key_ordering.py
Outdated
if any( | ||
strcoll(next.value, key) < 0 | ||
for key in context['stack'][-1].keys | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change this; and also please don't change my code proposals to use Black style: the whole codebase doesn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for that
Hi, the use case here is to allow nested maps such as:
not to be sorted, since there are times when we consider a primary/merge-key useful to remain wherever we define it (usually in 1st place)