-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: implement autofix for no-unnormalized-keys
#151
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
base: main
Are you sure you want to change the base?
Conversation
dccd118 to
42c637a
Compare
7177f2f to
78c0ce5
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.
LGTM. Would like @mdjermanovic to review before merging.
| fix(fixer) { | ||
| return fixer.replaceTextRange( | ||
| name.type === "String" | ||
| ? [name.range[0] + 1, name.range[1] - 1] | ||
| : name.range, | ||
| normalizedKey, | ||
| ); | ||
| }, |
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.
This would produce incorrect fix if there are escape sequences, e.g., \n:
/* eslint json/no-unnormalized-keys: [2, { form: "NFD" }] */
{
"\u1E9B\u0323\n": 42
}Fixed to invalid JSON:
/* eslint json/no-unnormalized-keys: [2, { form: "NFD" }] */
{
"ẛ̣
": 42
}Also, I'm not sure if autofixing \u1E9B\u0323 to ẛ̣ instead of a sequence with \uXXXX would be desirable.
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.
Your suggestion also makes sense to me, but if that's the case, I have two questions:
-
Should
data.keybe updated accordingly? -
Is there a JavaScript (or other) rule I can refer to?
I don't have much experience with JSON (and JavaScript) rules, so I think I'm missing some edge cases while implementing the rule. Do you have any recommendations I could refer to when implementing the rule? If so, that would be very helpful.
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.
- Should
data.keybe updated accordingly?
I think it would be better to show the raw key representation (i.e., as it appears in the linted source code) in the error message.
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.
2. Is there a JavaScript (or other) rule I can refer to?
I think the most similar thing we have in JS rules is a utility that parses string literals:
https://github.com/eslint/eslint/blob/main/lib/rules/utils/char-source.js
So we could try making something similar for JSON and use it to check how individual characters were represented in the original key and then try preserving the same form (a character directly inserted into the source code or an escape sequence) in the fixed key. A problem that might be difficult to solve in this particular rule is how to map characters from the fixed key to characters in the original key since the normalizations produce strings with different lengths.
Another option is to limit the autofix to keys that don't have escape sequences only.

Prerequisites checklist
What is the purpose of this pull request?
In this PR, I've implemented an autofix for
no-unnormalized-keys.This PR closes #141.
What changes did you make? (Give an overview)
In this PR, I've implemented an autofix for
no-unnormalized-keys.Related Issues
Closes: #141
Is there anything you'd like reviewers to focus on?
N/A