From 9eadf8bd4baa1c40bca7369abc130138f02ce965 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Dec 2022 12:37:50 +0800 Subject: [PATCH 1/3] Quote numeric keys in JSON --- changelog_unreleased/json/14083.md | 17 +++ src/language-js/printer-estree-json.js | 29 +++-- .../json/__snapshots__/jsfmt.spec.js.snap | 110 ++++++++++++++++-- tests/format/json/json/propertyKey.json | 11 +- 4 files changed, 146 insertions(+), 21 deletions(-) create mode 100644 changelog_unreleased/json/14083.md diff --git a/changelog_unreleased/json/14083.md b/changelog_unreleased/json/14083.md new file mode 100644 index 000000000000..0ab6322f8317 --- /dev/null +++ b/changelog_unreleased/json/14083.md @@ -0,0 +1,17 @@ +#### Quote numeric keys for json-stringify parser (#14083 by @fisker) + + +```jsx +// Input +{0: 'value'} + +// Prettier stable +{ + 0: "value" +} + +// Prettier main +{ + "0": "value" +} +``` diff --git a/src/language-js/printer-estree-json.js b/src/language-js/printer-estree-json.js index d3bc27c510c3..b0f8cf2c05ab 100644 --- a/src/language-js/printer-estree-json.js +++ b/src/language-js/printer-estree-json.js @@ -48,15 +48,13 @@ function genericPrint(path, options, print) { case "BooleanLiteral": return node.value ? "true" : "false"; case "StringLiteral": - case "NumericLiteral": return JSON.stringify(node.value); - case "Identifier": { - const parent = path.getParentNode(); - if (parent && parent.type === "ObjectProperty" && parent.key === node) { - return JSON.stringify(node.name); - } - return node.name; - } + case "NumericLiteral": + return isObjectKey(path) + ? JSON.stringify(String(node.value)) + : JSON.stringify(node.value); + case "Identifier": + return isObjectKey(path) ? JSON.stringify(node.name) : node.name; case "TemplateLiteral": // There is only one `TemplateElement` return print(["quasis", 0]); @@ -68,6 +66,12 @@ function genericPrint(path, options, print) { } } +function isObjectKey(path) { + return ( + path.getName() === "key" && path.getParentNode().type === "ObjectProperty" + ); +} + const ignoredProperties = new Set([ "start", "end", @@ -85,8 +89,13 @@ const ignoredProperties = new Set([ function clean(node, newNode /*, parent*/) { const { type } = node; // We print quoted key - if (type === "ObjectProperty" && node.key.type === "Identifier") { - newNode.key = { type: "StringLiteral", value: node.key.name }; + if (type === "ObjectProperty") { + const { key } = node; + if (key.type === "Identifier") { + newNode.key = { type: "StringLiteral", value: key.name }; + } else if (key.type === "NumericLiteral") { + newNode.key = { type: "StringLiteral", value: String(key.value) }; + } return; } if (type === "UnaryExpression" && node.operator === "+") { diff --git a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap index 445b92e02841..7e22642120de 100644 --- a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap @@ -2175,12 +2175,30 @@ trailingComma: "all" | printWidth =====================================input====================================== { - a: 123 + a: '', + 0: '', + 1e2: '', + 1.0: '', + .1: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "0": "", + 1e2: "", + 1.0: "", + "0.1": "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "" } ================================================================================ @@ -2194,12 +2212,30 @@ trailingComma: "all" | printWidth =====================================input====================================== { - a: 123 + a: '', + 0: '', + 1e2: '', + 1.0: '', + .1: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - a: 123, + a: "", + 0: "", + 1e2: "", + 1.0: "", + 0.1: "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "", } ================================================================================ @@ -2212,12 +2248,30 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + 0: '', + 1e2: '', + 1.0: '', + .1: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "0": "", + 1e2: "", + 1.0: "", + "0.1": "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "" } ================================================================================ @@ -2230,12 +2284,30 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + 0: '', + 1e2: '', + 1.0: '', + .1: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - a: 123, + a: "", + 0: "", + 1e2: "", + 1.0: "", + 0.1: "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "", } ================================================================================ @@ -2248,12 +2320,30 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + 0: '', + 1e2: '', + 1.0: '', + .1: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "0": "", + "100": "", + "1": "", + "0.1": "", + "1e+30": "", + "1e-30": "", + "1e-30": "", + "Infinity": "", + "123": "" } ================================================================================ diff --git a/tests/format/json/json/propertyKey.json b/tests/format/json/json/propertyKey.json index 399e7b9a6531..c27769aaef84 100644 --- a/tests/format/json/json/propertyKey.json +++ b/tests/format/json/json/propertyKey.json @@ -1,3 +1,12 @@ { - a: 123 + a: '', + 0: '', + 1e2: '', + 1.0: '', + .1: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } From 87bb5b7bd0a03c29ebaf535aeb838bff7b621644 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Dec 2022 12:45:34 +0800 Subject: [PATCH 2/3] More tests --- .../json/__snapshots__/jsfmt.spec.js.snap | 90 +++++++++++++++++++ tests/format/json/json/propertyKey.json | 9 ++ 2 files changed, 99 insertions(+) diff --git a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap index 7e22642120de..f40a236b1be6 100644 --- a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap @@ -2178,8 +2178,17 @@ trailingComma: "all" a: '', 0: '', 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', 1.0: '', + 1.00000: '', .1: '', + .100000: '', + 0.1: '', + 0.100000: '', 999999999999999999999999999999: '', .000000000000000000000000000001: '', 0.000000000000000000000000000001: '', @@ -2192,7 +2201,16 @@ trailingComma: "all" "a": "", "0": "", 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", 1.0: "", + 1.0: "", + "0.1": "", + "0.1": "", + "0.1": "", "0.1": "", 999999999999999999999999999999: "", 0.000000000000000000000000000001: "", @@ -2215,8 +2233,17 @@ trailingComma: "all" a: '', 0: '', 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', 1.0: '', + 1.00000: '', .1: '', + .100000: '', + 0.1: '', + 0.100000: '', 999999999999999999999999999999: '', .000000000000000000000000000001: '', 0.000000000000000000000000000001: '', @@ -2229,8 +2256,17 @@ trailingComma: "all" a: "", 0: "", 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", 1.0: "", 0.1: "", + 0.1: "", + 0.1: "", + 0.1: "", 999999999999999999999999999999: "", 0.000000000000000000000000000001: "", 0.000000000000000000000000000001: "", @@ -2251,8 +2287,17 @@ printWidth: 80 a: '', 0: '', 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', 1.0: '', + 1.00000: '', .1: '', + .100000: '', + 0.1: '', + 0.100000: '', 999999999999999999999999999999: '', .000000000000000000000000000001: '', 0.000000000000000000000000000001: '', @@ -2265,8 +2310,17 @@ printWidth: 80 "a": "", "0": "", 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", 1.0: "", "0.1": "", + "0.1": "", + "0.1": "", + "0.1": "", 999999999999999999999999999999: "", 0.000000000000000000000000000001: "", 0.000000000000000000000000000001: "", @@ -2287,8 +2341,17 @@ printWidth: 80 a: '', 0: '', 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', 1.0: '', + 1.00000: '', .1: '', + .100000: '', + 0.1: '', + 0.100000: '', 999999999999999999999999999999: '', .000000000000000000000000000001: '', 0.000000000000000000000000000001: '', @@ -2301,8 +2364,17 @@ printWidth: 80 a: "", 0: "", 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", 1.0: "", 0.1: "", + 0.1: "", + 0.1: "", + 0.1: "", 999999999999999999999999999999: "", 0.000000000000000000000000000001: "", 0.000000000000000000000000000001: "", @@ -2323,8 +2395,17 @@ printWidth: 80 a: '', 0: '', 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', 1.0: '', + 1.00000: '', .1: '', + .100000: '', + 0.1: '', + 0.100000: '', 999999999999999999999999999999: '', .000000000000000000000000000001: '', 0.000000000000000000000000000001: '', @@ -2337,8 +2418,17 @@ printWidth: 80 "a": "", "0": "", "100": "", + "100": "", + "10": "", + "0.01": "", + "0.001": "", + "10": "", + "1": "", "1": "", "0.1": "", + "0.1": "", + "0.1": "", + "0.1": "", "1e+30": "", "1e-30": "", "1e-30": "", diff --git a/tests/format/json/json/propertyKey.json b/tests/format/json/json/propertyKey.json index c27769aaef84..284bd764529b 100644 --- a/tests/format/json/json/propertyKey.json +++ b/tests/format/json/json/propertyKey.json @@ -2,8 +2,17 @@ a: '', 0: '', 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', 1.0: '', + 1.00000: '', .1: '', + .100000: '', + 0.1: '', + 0.100000: '', 999999999999999999999999999999: '', .000000000000000000000000000001: '', 0.000000000000000000000000000001: '', From a13894fe67e8a4fcdc3171a0fa74db70631360f0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Dec 2022 12:51:32 +0800 Subject: [PATCH 3/3] More tests --- .../json/__snapshots__/jsfmt.spec.js.snap | 30 +++++++++++++++++++ tests/format/json/json/propertyKey.json | 3 ++ 2 files changed, 33 insertions(+) diff --git a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap index f40a236b1be6..036112b18023 100644 --- a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap @@ -2176,6 +2176,9 @@ trailingComma: "all" =====================================input====================================== { a: '', + null: '', + true: '', + "string": "", 0: '', 1e2: '', 1.0e+2: '', @@ -2199,6 +2202,9 @@ trailingComma: "all" =====================================output===================================== { "a": "", + "null": "", + "true": "", + "string": "", "0": "", 1e2: "", 1.0e2: "", @@ -2231,6 +2237,9 @@ trailingComma: "all" =====================================input====================================== { a: '', + null: '', + true: '', + "string": "", 0: '', 1e2: '', 1.0e+2: '', @@ -2254,6 +2263,9 @@ trailingComma: "all" =====================================output===================================== { a: "", + null: "", + true: "", + string: "", 0: "", 1e2: "", 1.0e2: "", @@ -2285,6 +2297,9 @@ printWidth: 80 =====================================input====================================== { a: '', + null: '', + true: '', + "string": "", 0: '', 1e2: '', 1.0e+2: '', @@ -2308,6 +2323,9 @@ printWidth: 80 =====================================output===================================== { "a": "", + "null": "", + "true": "", + "string": "", "0": "", 1e2: "", 1.0e2: "", @@ -2339,6 +2357,9 @@ printWidth: 80 =====================================input====================================== { a: '', + null: '', + true: '', + "string": "", 0: '', 1e2: '', 1.0e+2: '', @@ -2362,6 +2383,9 @@ printWidth: 80 =====================================output===================================== { a: "", + null: "", + true: "", + string: "", 0: "", 1e2: "", 1.0e2: "", @@ -2393,6 +2417,9 @@ printWidth: 80 =====================================input====================================== { a: '', + null: '', + true: '', + "string": "", 0: '', 1e2: '', 1.0e+2: '', @@ -2416,6 +2443,9 @@ printWidth: 80 =====================================output===================================== { "a": "", + "null": "", + "true": "", + "string": "", "0": "", "100": "", "100": "", diff --git a/tests/format/json/json/propertyKey.json b/tests/format/json/json/propertyKey.json index 284bd764529b..12d3f130e6ac 100644 --- a/tests/format/json/json/propertyKey.json +++ b/tests/format/json/json/propertyKey.json @@ -1,5 +1,8 @@ { a: '', + null: '', + true: '', + "string": "", 0: '', 1e2: '', 1.0e+2: '',