-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rule no-unnormalized-keys (#63)
* feat: rule no-unormalized-keys Fixes #32 * Add no-unnormalized-keys to plugin exports * Cleanup tests and refactor for clarity * Output key into error message and update tests * Update README * Switch to object option * Re-add eslint.test.js * Update src/rules/no-unnormalized-keys.js Co-authored-by: Milos Djermanovic <[email protected]> * Update src/rules/no-unnormalized-keys.js Co-authored-by: Milos Djermanovic <[email protected]> --------- Co-authored-by: Bradley Meck Farias <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]>
- Loading branch information
1 parent
ede448d
commit c57882e
Showing
4 changed files
with
148 additions
and
0 deletions.
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
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
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,54 @@ | ||
/** | ||
* @fileoverview Rule to detect unnormalized keys in JSON. | ||
* @author Bradley Meck Farias | ||
*/ | ||
|
||
export default { | ||
meta: { | ||
type: /** @type {const} */ ("problem"), | ||
|
||
docs: { | ||
description: "Disallow JSON keys that are not normalized", | ||
}, | ||
|
||
messages: { | ||
unnormalizedKey: "Unnormalized key '{{key}}' found.", | ||
}, | ||
|
||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
form: { | ||
enum: ["NFC", "NFD", "NFKC", "NFKD"], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
|
||
create(context) { | ||
const normalization = context.options.length | ||
? text => text.normalize(context.options[0].form) | ||
: text => text.normalize(); | ||
return { | ||
Member(node) { | ||
const key = | ||
node.name.type === "String" | ||
? node.name.value | ||
: node.name.name; | ||
|
||
if (normalization(key) !== key) { | ||
context.report({ | ||
loc: node.name.loc, | ||
messageId: "unnormalizedKey", | ||
data: { | ||
key, | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,90 @@ | ||
/** | ||
* @fileoverview Tests for no-unnormalized-keys rule. | ||
* @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: [{ form: "NFC" }], | ||
}, | ||
{ | ||
code: `{"${o.normalize("NFD")}":"NFD"}`, | ||
options: [{ form: "NFD" }], | ||
}, | ||
{ | ||
code: `{"${o.normalize("NFKC")}":"NFKC"}`, | ||
options: [{ form: "NFKC" }], | ||
}, | ||
{ | ||
code: `{"${o.normalize("NFKD")}":"NFKD"}`, | ||
options: [{ form: "NFKD" }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `{"${o.normalize("NFD")}":"NFD"}`, | ||
errors: [ | ||
{ | ||
messageId: "unnormalizedKey", | ||
data: { key: o.normalize("NFD") }, | ||
line: 1, | ||
column: 2, | ||
endLine: 1, | ||
endColumn: 7, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `{${o.normalize("NFD")}:"NFD"}`, | ||
language: "json/json5", | ||
errors: [ | ||
{ | ||
messageId: "unnormalizedKey", | ||
data: { key: o.normalize("NFD") }, | ||
line: 1, | ||
column: 2, | ||
endLine: 1, | ||
endColumn: 5, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `{"${o.normalize("NFKC")}":"NFKC"}`, | ||
options: [{ form: "NFKD" }], | ||
errors: [ | ||
{ | ||
messageId: "unnormalizedKey", | ||
data: { key: o.normalize("NFKC") }, | ||
line: 1, | ||
column: 2, | ||
endLine: 1, | ||
endColumn: 5, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |