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..036112b18023 100644 --- a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap @@ -2175,12 +2175,54 @@ trailingComma: "all" | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 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: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "null": "", + "true": "", + "string": "", + "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: "", + 1e999999999999999999999999999999: "", + 1_2_3: "" } ================================================================================ @@ -2194,12 +2236,54 @@ trailingComma: "all" | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 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: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - a: 123, + a: "", + null: "", + true: "", + string: "", + 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: "", + 1e999999999999999999999999999999: "", + 1_2_3: "", } ================================================================================ @@ -2212,12 +2296,54 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 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: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "null": "", + "true": "", + "string": "", + "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: "", + 1e999999999999999999999999999999: "", + 1_2_3: "" } ================================================================================ @@ -2230,12 +2356,54 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 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: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - a: 123, + a: "", + null: "", + true: "", + string: "", + 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: "", + 1e999999999999999999999999999999: "", + 1_2_3: "", } ================================================================================ @@ -2248,12 +2416,54 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 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: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "null": "", + "true": "", + "string": "", + "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": "", + "Infinity": "", + "123": "" } ================================================================================ diff --git a/tests/format/json/json/propertyKey.json b/tests/format/json/json/propertyKey.json index 399e7b9a6531..12d3f130e6ac 100644 --- a/tests/format/json/json/propertyKey.json +++ b/tests/format/json/json/propertyKey.json @@ -1,3 +1,24 @@ { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 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: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', }