Quote numeric keys for json-stringify parser (#14083)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-01-03 10:47:30 +08:00
1 parent efa6aa68b6
commit af3d6fa80b
4 files changed
+278 -21

No files matched your search

+17
View File
@@ -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"
}
```
+19 -10
View File
@@ -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 === "+") {
@@ -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": ""
}
================================================================================
+22 -1
View File
@@ -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: '',
}