-
-
Notifications
You must be signed in to change notification settings - Fork 4
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 rule no-unnormalized-keys #38
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @fileoverview Rule to detect unnormalized keys in JSON. | ||
* @author Bradley Meck Farias | ||
*/ | ||
|
||
export default { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "Disallow JSON keys that are not normalized", | ||
}, | ||
|
||
messages: { | ||
unnormalizedKey: "Unnormalized key found.", | ||
}, | ||
|
||
schema: { | ||
type: "array", | ||
minItems: 0, | ||
maxItems: 1, | ||
items: { | ||
enum: ["NFC", "NFD", "NFKC", "NFKD"], | ||
}, | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const normalization = context.options.length | ||
? s => s.normalize(context.options[0]) | ||
: s => s.normalize(); | ||
return { | ||
Member(node) { | ||
const key = | ||
node.name.type === "String" | ||
? node.name.value | ||
: node.name.name; | ||
Comment on lines
+34
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an |
||
|
||
if (normalization(key) !== key) { | ||
context.report({ | ||
loc: node.name.loc, | ||
messageId: "unnormalizedKey", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||
/** | ||||||
* @fileoverview Tests for no-empty-keys rule. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @author Bradley Meck Farias | ||||||
*/ | ||||||
|
||||||
//------------------------------------------------------------------------------ | ||||||
// Imports | ||||||
//------------------------------------------------------------------------------ | ||||||
|
||||||
import rule from "../../src/rules/no-unnormalized-keys.js"; | ||||||
import json from "../../src/index.js"; | ||||||
import { RuleTester } from "eslint"; | ||||||
|
||||||
//------------------------------------------------------------------------------ | ||||||
// Tests | ||||||
//------------------------------------------------------------------------------ | ||||||
|
||||||
const ruleTester = new RuleTester({ | ||||||
plugins: { | ||||||
json, | ||||||
}, | ||||||
language: "json/json", | ||||||
}); | ||||||
|
||||||
const o = "\u1E9B\u0323"; | ||||||
|
||||||
ruleTester.run("no-unnormalized-keys", rule, { | ||||||
valid: [ | ||||||
`{"${o}":"NFC"}`, | ||||||
{ | ||||||
code: `{"${o}":"NFC"}`, | ||||||
options: ["NFC"], | ||||||
}, | ||||||
{ | ||||||
code: `{"${o.normalize("NFD")}":"NFD"}`, | ||||||
options: ["NFD"], | ||||||
}, | ||||||
{ | ||||||
code: `{"${o.normalize("NFKC")}":"NFKC"}`, | ||||||
options: ["NFKC"], | ||||||
}, | ||||||
{ | ||||||
code: `{"${o.normalize("NFKD")}":"NFKD"}`, | ||||||
options: ["NFKD"], | ||||||
}, | ||||||
], | ||||||
invalid: [ | ||||||
{ | ||||||
code: `{"${o.normalize("NFD")}":"NFD"}`, | ||||||
errors: [ | ||||||
{ | ||||||
messageId: "unnormalizedKey", | ||||||
line: 1, | ||||||
column: 2, | ||||||
endLine: 1, | ||||||
endColumn: 7, | ||||||
}, | ||||||
], | ||||||
}, | ||||||
], | ||||||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 can be simplified by using the shorthand form (https://eslint.org/docs/latest/extend/custom-rules#options-schemas, the first format).