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

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

/**
* Escapes a normalized string key and wraps it in 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 and quoted key.
*/
function escapeKey(normalizedKey, quote) {
const escapedKey = normalizedKey
.replaceAll("\\", "\\\\")
.replaceAll(quote, `\\${quote}`);

return `${quote}${escapedKey}${quote}`;
}

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -62,12 +80,13 @@ 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) {
Expand All @@ -85,12 +104,15 @@ export default /** @satisfies {NoUnnormalizedKeysRuleDefinition} */ ({
return null;
}

return fixer.replaceTextRange(
const fixedKey =
name.type === "String"
? [name.range[0] + 1, name.range[1] - 1]
: name.range,
normalizedKey,
);
? escapeKey(
normalizedKey,
sourceCode.text[name.range[0]],
)
: normalizedKey;

return fixer.replaceText(name, fixedKey);
Comment on lines +107 to +115

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior would be the same, but rather than reconstructing the key by concatenating strings based on the quote information, I think we can simply escape the normalized key here.

To me, the original type === "String" ? [range[0] + 1, range[1] - 1] : range more clearly represents the original intent and makes it clear exactly where this fix applies.

/**
 * 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 and quoted key.
 */
function escapeKey(normalizedKey, quote) {
	return normalizedKey
		.replaceAll("\\", "\\\\")
		.replaceAll(quote, `\\${quote}`);
}
const { loc, range, type } = node.name;
							return fixer.replaceTextRange(
								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