Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"mdast-util-from-markdown": "^2.0.2",
"mocha": "^11.3.0",
"prettier": "^3.4.1",
"rollup": "^4.41.0",
"rollup": "4.51.0",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-delete": "^3.0.1",
"typescript": "^5.9.2",
Expand Down
9 changes: 9 additions & 0 deletions src/rules/no-unnormalized-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const rule = {
meta: {
type: "problem",

fixable: "code",

docs: {
recommended: true,
description: "Disallow JSON keys that are not normalized",
Expand Down Expand Up @@ -55,6 +57,7 @@ const rule = {

create(context) {
const [{ form }] = context.options;
const { sourceCode } = context;

return {
Member(node) {
Expand All @@ -70,6 +73,12 @@ const rule = {
data: {
key,
},
fix(fixer) {
return fixer.replaceTextRange(
node.name.range,
sourceCode.getText(node.name).normalize(form), // Quotes cannot be normalized, so it's safe.
);
},
});
}
},
Expand Down
98 changes: 98 additions & 0 deletions tests/rules/no-unnormalized-keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ruleTester.run("no-unnormalized-keys", rule, {
invalid: [
{
code: `{"${o.normalize("NFD")}":"NFD"}`,
output: `{"${o.normalize("NFC")}":"NFD"}`,
errors: [
{
messageId: "unnormalizedKey",
Expand All @@ -60,6 +61,7 @@ ruleTester.run("no-unnormalized-keys", rule, {
},
{
code: `{"${o.normalize("NFD")}":"NFD"}`,
output: `{"${o.normalize("NFC")}":"NFD"}`,
language: "json/jsonc",
errors: [
{
Expand All @@ -72,8 +74,39 @@ ruleTester.run("no-unnormalized-keys", rule, {
},
],
},
{
code: `{"${o.normalize("NFD")}":"NFD"}`,
output: `{"${o.normalize("NFC")}":"NFD"}`,
language: "json/json5",
errors: [
{
messageId: "unnormalizedKey",
data: { key: o.normalize("NFD") },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{'${o.normalize("NFD")}':'NFD'}`,
output: `{'${o.normalize("NFC")}':'NFD'}`,
language: "json/json5",
errors: [
{
messageId: "unnormalizedKey",
data: { key: o.normalize("NFD") },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{${o.normalize("NFD")}:"NFD"}`,
output: `{${o.normalize("NFC")}:"NFD"}`,
language: "json/json5",
errors: [
{
Expand All @@ -88,6 +121,55 @@ ruleTester.run("no-unnormalized-keys", rule, {
},
{
code: `{"${o.normalize("NFKC")}":"NFKC"}`,
output: `{"${o.normalize("NFKD")}":"NFKC"}`,
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: o.normalize("NFKC") },
line: 1,
column: 2,
endLine: 1,
endColumn: 5,
},
],
},
{
code: `{"${o.normalize("NFKC")}":"NFKC"}`,
output: `{"${o.normalize("NFKD")}":"NFKC"}`,
language: "json/jsonc",
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: o.normalize("NFKC") },
line: 1,
column: 2,
endLine: 1,
endColumn: 5,
},
],
},
{
code: `{"${o.normalize("NFKC")}":"NFKC"}`,
output: `{"${o.normalize("NFKD")}":"NFKC"}`,
language: "json/json5",
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: o.normalize("NFKC") },
line: 1,
column: 2,
endLine: 1,
endColumn: 5,
},
],
},
{
code: `{'${o.normalize("NFKC")}':"NFKC"}`,
output: `{'${o.normalize("NFKD")}':"NFKC"}`,
language: "json/json5",
options: [{ form: "NFKD" }],
errors: [
{
Expand All @@ -100,5 +182,21 @@ ruleTester.run("no-unnormalized-keys", rule, {
},
],
},
{
code: `{${o.normalize("NFKC")}:"NFKC"}`,
output: `{${o.normalize("NFKD")}:"NFKC"}`,
language: "json/json5",
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: o.normalize("NFKC") },
line: 1,
column: 2,
endLine: 1,
endColumn: 3,
},
],
},
],
});
Loading