-
-
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.
Fixes #32
- Loading branch information
Showing
2 changed files
with
109 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
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; | ||
|
||
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. | ||
* @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, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |