Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote numeric keys for json-stringify parser #14083

Merged
merged 3 commits into from Jan 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions changelog_unreleased/json/14083.md
@@ -0,0 +1,17 @@
#### Quote numeric keys for json-stringify parser (#14083 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
{0: 'value'}

// Prettier stable
{
0: "value"
}

// Prettier main
{
"0": "value"
}
```
29 changes: 19 additions & 10 deletions src/language-js/printer-estree-json.js
Expand Up @@ -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]);
Expand All @@ -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",
Expand All @@ -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 === "+") {
Expand Down
230 changes: 220 additions & 10 deletions tests/format/json/json/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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: ""
}

================================================================================
Expand All @@ -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: "",
}

================================================================================
Expand All @@ -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: ""
}

================================================================================
Expand All @@ -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: "",
}

================================================================================
Expand All @@ -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": ""
}

================================================================================
Expand Down
23 changes: 22 additions & 1 deletion 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: '',
}