From c74b7310d38c6598ed0f306e70bbf3d439f1afb0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Dec 2022 12:37:50 +0800 Subject: [PATCH] Quote numeric keys in JSON --- src/language-js/printer-estree-json.js | 26 +++-- .../json/__snapshots__/jsfmt.spec.js.snap | 110 ++++++++++++++++-- tests/format/json/json/propertyKey.json | 11 +- 3 files changed, 128 insertions(+), 19 deletions(-) diff --git a/src/language-js/printer-estree-json.js b/src/language-js/printer-estree-json.js index d3bc27c510c3..6904a17a2ba5 100644 --- a/src/language-js/printer-estree-json.js +++ b/src/language-js/printer-estree-json.js @@ -48,14 +48,13 @@ function genericPrint(path, options, print) { case "BooleanLiteral": return node.value ? "true" : "false"; case "StringLiteral": - case "NumericLiteral": return JSON.stringify(node.value); + case "NumericLiteral": + return isObjectKey(path) + ? JSON.stringify(String(node.value)) + : 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; + return isObjectKey(path) ? JSON.stringify(node.name) : node.name; } case "TemplateLiteral": // There is only one `TemplateElement` @@ -68,6 +67,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 +90,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: '', }