Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 29 additions & 7 deletions src/rules/no-unnormalized-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ import { getKey, getRawKey } from "../util.js";
* @typedef {JSONRuleDefinition<{ RuleOptions: [NoUnnormalizedKeysOptions], MessageIds: NoUnnormalizedKeysMessageIds }>} NoUnnormalizedKeysRuleDefinition
*/

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------

/**
* Escapes a normalized string key for use inside its original quotes.
* @param {string} normalizedKey The normalized key to escape.
* @param {string} quote The quote character used in the original key.
* @returns {string} The escaped key.
*/
function escapeKey(normalizedKey, quote) {
return normalizedKey
.replaceAll("\\", "\\\\")
.replaceAll(quote, `\\${quote}`);
}

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -62,19 +78,20 @@ export default /** @satisfies {NoUnnormalizedKeysRuleDefinition} */ ({
},

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

return /** @type {JSONRuleVisitor} */ ({
Member(node) {
const key = getKey(node);
const rawKey = getRawKey(node, context.sourceCode);
const rawKey = getRawKey(node, sourceCode);
const normalizedKey = key.normalize(form);

if (normalizedKey !== key) {
const { name } = node;
const { loc, range, type } = node.name;

context.report({
loc: name.loc,
loc,
messageId: "unnormalizedKey",
data: {
key: rawKey,
Expand All @@ -86,10 +103,15 @@ export default /** @satisfies {NoUnnormalizedKeysRuleDefinition} */ ({
}

return fixer.replaceTextRange(
name.type === "String"
? [name.range[0] + 1, name.range[1] - 1]
: name.range,
normalizedKey,
type === "String"
? [range[0] + 1, range[1] - 1]
: range,
type === "String"
? escapeKey(
normalizedKey,
sourceCode.text[range[0]],
)
: normalizedKey,
);
},
});
Expand Down
203 changes: 203 additions & 0 deletions tests/rules/no-unnormalized-keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,209 @@ ruleTester.run("no-unnormalized-keys", rule, {
},
],
},
{
code: `{"a\uff02b": 1}`,
output: String.raw`{"a\"b": 1}`,
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b" },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{"a\uff07b": 1}`,
output: String.raw`{"a'b": 1}`,
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff07b" },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{"a\uff3cb": 1}`,
output: String.raw`{"a\\b": 1}`,
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff3cb" },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{'a\uff07b': 1}`,
output: String.raw`{'a\'b': 1}`,
language: "json/json5",
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff07b" },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{'a\uff02b': 1}`,
output: String.raw`{'a"b': 1}`,
language: "json/json5",
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b" },
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
{
code: `{"a\uff02b\uff3cc\uff07\uff02\uff3c": 1}`,
output: String.raw`{"a\"b\\c'\"\\": 1}`,
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b\uff3cc\uff07\uff02\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{"a\uff02b\uff3cc\uff07\uff02\uff3c": 1}`,
output: String.raw`{"a\"b\\c'\"\\": 1}`,
language: "json/jsonc",
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b\uff3cc\uff07\uff02\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{"a\uff02b\uff3cc\uff07\uff02\uff3c": 1}`,
output: String.raw`{"a\"b\\c'\"\\": 1}`,
language: "json/json5",
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b\uff3cc\uff07\uff02\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{'a\uff07b\uff3cc\uff02\uff07\uff3c': 1}`,
output: String.raw`{'a\'b\\c"\'\\': 1}`,
language: "json/json5",
options: [{ form: "NFKC" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff07b\uff3cc\uff02\uff07\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{"a\uff02b\uff3cc\uff07\uff02\uff3c": 1}`,
output: String.raw`{"a\"b\\c'\"\\": 1}`,
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b\uff3cc\uff07\uff02\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{"a\uff02b\uff3cc\uff07\uff02\uff3c": 1}`,
output: String.raw`{"a\"b\\c'\"\\": 1}`,
language: "json/jsonc",
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b\uff3cc\uff07\uff02\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{"a\uff02b\uff3cc\uff07\uff02\uff3c": 1}`,
output: String.raw`{"a\"b\\c'\"\\": 1}`,
language: "json/json5",
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff02b\uff3cc\uff07\uff02\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
{
code: `{'a\uff07b\uff3cc\uff02\uff07\uff3c': 1}`,
output: String.raw`{'a\'b\\c"\'\\': 1}`,
language: "json/json5",
options: [{ form: "NFKD" }],
errors: [
{
messageId: "unnormalizedKey",
data: { key: "a\uff07b\uff3cc\uff02\uff07\uff3c" },
line: 1,
column: 2,
endLine: 1,
endColumn: 12,
},
],
},
// escaped form
{
code: `{"${escapedNfcO}":"NFC"}`,
Expand Down
Loading