Skip to content

Commit af3d6fa

Browse files
authored
Quote numeric keys for json-stringify parser (#14083)
1 parent efa6aa6 commit af3d6fa

4 files changed

Lines changed: 278 additions & 21 deletions

File tree

changelog_unreleased/json/14083.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#### Quote numeric keys for json-stringify parser (#14083 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
{0: 'value'}
7+
8+
// Prettier stable
9+
{
10+
0: "value"
11+
}
12+
13+
// Prettier main
14+
{
15+
"0": "value"
16+
}
17+
```

src/language-js/printer-estree-json.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ function genericPrint(path, options, print) {
4848
case "BooleanLiteral":
4949
return node.value ? "true" : "false";
5050
case "StringLiteral":
51-
case "NumericLiteral":
5251
return JSON.stringify(node.value);
53-
case "Identifier": {
54-
const parent = path.getParentNode();
55-
if (parent && parent.type === "ObjectProperty" && parent.key === node) {
56-
return JSON.stringify(node.name);
57-
}
58-
return node.name;
59-
}
52+
case "NumericLiteral":
53+
return isObjectKey(path)
54+
? JSON.stringify(String(node.value))
55+
: JSON.stringify(node.value);
56+
case "Identifier":
57+
return isObjectKey(path) ? JSON.stringify(node.name) : node.name;
6058
case "TemplateLiteral":
6159
// There is only one `TemplateElement`
6260
return print(["quasis", 0]);
@@ -68,6 +66,12 @@ function genericPrint(path, options, print) {
6866
}
6967
}
7068

69+
function isObjectKey(path) {
70+
return (
71+
path.getName() === "key" && path.getParentNode().type === "ObjectProperty"
72+
);
73+
}
74+
7175
const ignoredProperties = new Set([
7276
"start",
7377
"end",
@@ -85,8 +89,13 @@ const ignoredProperties = new Set([
8589
function clean(node, newNode /*, parent*/) {
8690
const { type } = node;
8791
// We print quoted key
88-
if (type === "ObjectProperty" && node.key.type === "Identifier") {
89-
newNode.key = { type: "StringLiteral", value: node.key.name };
92+
if (type === "ObjectProperty") {
93+
const { key } = node;
94+
if (key.type === "Identifier") {
95+
newNode.key = { type: "StringLiteral", value: key.name };
96+
} else if (key.type === "NumericLiteral") {
97+
newNode.key = { type: "StringLiteral", value: String(key.value) };
98+
}
9099
return;
91100
}
92101
if (type === "UnaryExpression" && node.operator === "+") {

tests/format/json/json/__snapshots__/jsfmt.spec.js.snap

Lines changed: 220 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,12 +2175,54 @@ trailingComma: "all"
21752175
| printWidth
21762176
=====================================input======================================
21772177
{
2178-
a: 123
2178+
a: '',
2179+
null: '',
2180+
true: '',
2181+
"string": "",
2182+
0: '',
2183+
1e2: '',
2184+
1.0e+2: '',
2185+
.10e+2: '',
2186+
1e-2: '',
2187+
.1e-2: '',
2188+
0.1e+2: '',
2189+
1.0: '',
2190+
1.00000: '',
2191+
.1: '',
2192+
.100000: '',
2193+
0.1: '',
2194+
0.100000: '',
2195+
999999999999999999999999999999: '',
2196+
.000000000000000000000000000001: '',
2197+
0.000000000000000000000000000001: '',
2198+
1e999999999999999999999999999999: '',
2199+
1_2_3: '',
21792200
}
21802201
21812202
=====================================output=====================================
21822203
{
2183-
"a": 123
2204+
"a": "",
2205+
"null": "",
2206+
"true": "",
2207+
"string": "",
2208+
"0": "",
2209+
1e2: "",
2210+
1.0e2: "",
2211+
0.1e2: "",
2212+
1e-2: "",
2213+
0.1e-2: "",
2214+
0.1e2: "",
2215+
1.0: "",
2216+
1.0: "",
2217+
"0.1": "",
2218+
"0.1": "",
2219+
"0.1": "",
2220+
"0.1": "",
2221+
999999999999999999999999999999: "",
2222+
0.000000000000000000000000000001: "",
2223+
0.000000000000000000000000000001: "",
2224+
1e999999999999999999999999999999: "",
2225+
1_2_3: ""
21842226
}
21852227
21862228
================================================================================
@@ -2194,12 +2236,54 @@ trailingComma: "all"
21942236
| printWidth
21952237
=====================================input======================================
21962238
{
2197-
a: 123
2239+
a: '',
2240+
null: '',
2241+
true: '',
2242+
"string": "",
2243+
0: '',
2244+
1e2: '',
2245+
1.0e+2: '',
2246+
.10e+2: '',
2247+
1e-2: '',
2248+
.1e-2: '',
2249+
0.1e+2: '',
2250+
1.0: '',
2251+
1.00000: '',
2252+
.1: '',
2253+
.100000: '',
2254+
0.1: '',
2255+
0.100000: '',
2256+
999999999999999999999999999999: '',
2257+
.000000000000000000000000000001: '',
2258+
0.000000000000000000000000000001: '',
2259+
1e999999999999999999999999999999: '',
2260+
1_2_3: '',
21982261
}
21992262
22002263
=====================================output=====================================
22012264
{
2202-
a: 123,
2265+
a: "",
2266+
null: "",
2267+
true: "",
2268+
string: "",
2269+
0: "",
2270+
1e2: "",
2271+
1.0e2: "",
2272+
0.1e2: "",
2273+
1e-2: "",
2274+
0.1e-2: "",
2275+
0.1e2: "",
2276+
1.0: "",
2277+
1.0: "",
2278+
0.1: "",
2279+
0.1: "",
2280+
0.1: "",
2281+
0.1: "",
2282+
999999999999999999999999999999: "",
2283+
0.000000000000000000000000000001: "",
2284+
0.000000000000000000000000000001: "",
2285+
1e999999999999999999999999999999: "",
2286+
1_2_3: "",
22032287
}
22042288
22052289
================================================================================
@@ -2212,12 +2296,54 @@ printWidth: 80
22122296
| printWidth
22132297
=====================================input======================================
22142298
{
2215-
a: 123
2299+
a: '',
2300+
null: '',
2301+
true: '',
2302+
"string": "",
2303+
0: '',
2304+
1e2: '',
2305+
1.0e+2: '',
2306+
.10e+2: '',
2307+
1e-2: '',
2308+
.1e-2: '',
2309+
0.1e+2: '',
2310+
1.0: '',
2311+
1.00000: '',
2312+
.1: '',
2313+
.100000: '',
2314+
0.1: '',
2315+
0.100000: '',
2316+
999999999999999999999999999999: '',
2317+
.000000000000000000000000000001: '',
2318+
0.000000000000000000000000000001: '',
2319+
1e999999999999999999999999999999: '',
2320+
1_2_3: '',
22162321
}
22172322
22182323
=====================================output=====================================
22192324
{
2220-
"a": 123
2325+
"a": "",
2326+
"null": "",
2327+
"true": "",
2328+
"string": "",
2329+
"0": "",
2330+
1e2: "",
2331+
1.0e2: "",
2332+
0.1e2: "",
2333+
1e-2: "",
2334+
0.1e-2: "",
2335+
0.1e2: "",
2336+
1.0: "",
2337+
1.0: "",
2338+
"0.1": "",
2339+
"0.1": "",
2340+
"0.1": "",
2341+
"0.1": "",
2342+
999999999999999999999999999999: "",
2343+
0.000000000000000000000000000001: "",
2344+
0.000000000000000000000000000001: "",
2345+
1e999999999999999999999999999999: "",
2346+
1_2_3: ""
22212347
}
22222348
22232349
================================================================================
@@ -2230,12 +2356,54 @@ printWidth: 80
22302356
| printWidth
22312357
=====================================input======================================
22322358
{
2233-
a: 123
2359+
a: '',
2360+
null: '',
2361+
true: '',
2362+
"string": "",
2363+
0: '',
2364+
1e2: '',
2365+
1.0e+2: '',
2366+
.10e+2: '',
2367+
1e-2: '',
2368+
.1e-2: '',
2369+
0.1e+2: '',
2370+
1.0: '',
2371+
1.00000: '',
2372+
.1: '',
2373+
.100000: '',
2374+
0.1: '',
2375+
0.100000: '',
2376+
999999999999999999999999999999: '',
2377+
.000000000000000000000000000001: '',
2378+
0.000000000000000000000000000001: '',
2379+
1e999999999999999999999999999999: '',
2380+
1_2_3: '',
22342381
}
22352382
22362383
=====================================output=====================================
22372384
{
2238-
a: 123,
2385+
a: "",
2386+
null: "",
2387+
true: "",
2388+
string: "",
2389+
0: "",
2390+
1e2: "",
2391+
1.0e2: "",
2392+
0.1e2: "",
2393+
1e-2: "",
2394+
0.1e-2: "",
2395+
0.1e2: "",
2396+
1.0: "",
2397+
1.0: "",
2398+
0.1: "",
2399+
0.1: "",
2400+
0.1: "",
2401+
0.1: "",
2402+
999999999999999999999999999999: "",
2403+
0.000000000000000000000000000001: "",
2404+
0.000000000000000000000000000001: "",
2405+
1e999999999999999999999999999999: "",
2406+
1_2_3: "",
22392407
}
22402408
22412409
================================================================================
@@ -2248,12 +2416,54 @@ printWidth: 80
22482416
| printWidth
22492417
=====================================input======================================
22502418
{
2251-
a: 123
2419+
a: '',
2420+
null: '',
2421+
true: '',
2422+
"string": "",
2423+
0: '',
2424+
1e2: '',
2425+
1.0e+2: '',
2426+
.10e+2: '',
2427+
1e-2: '',
2428+
.1e-2: '',
2429+
0.1e+2: '',
2430+
1.0: '',
2431+
1.00000: '',
2432+
.1: '',
2433+
.100000: '',
2434+
0.1: '',
2435+
0.100000: '',
2436+
999999999999999999999999999999: '',
2437+
.000000000000000000000000000001: '',
2438+
0.000000000000000000000000000001: '',
2439+
1e999999999999999999999999999999: '',
2440+
1_2_3: '',
22522441
}
22532442
22542443
=====================================output=====================================
22552444
{
2256-
"a": 123
2445+
"a": "",
2446+
"null": "",
2447+
"true": "",
2448+
"string": "",
2449+
"0": "",
2450+
"100": "",
2451+
"100": "",
2452+
"10": "",
2453+
"0.01": "",
2454+
"0.001": "",
2455+
"10": "",
2456+
"1": "",
2457+
"1": "",
2458+
"0.1": "",
2459+
"0.1": "",
2460+
"0.1": "",
2461+
"0.1": "",
2462+
"1e+30": "",
2463+
"1e-30": "",
2464+
"1e-30": "",
2465+
"Infinity": "",
2466+
"123": ""
22572467
}
22582468
22592469
================================================================================
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
{
2-
a: 123
2+
a: '',
3+
null: '',
4+
true: '',
5+
"string": "",
6+
0: '',
7+
1e2: '',
8+
1.0e+2: '',
9+
.10e+2: '',
10+
1e-2: '',
11+
.1e-2: '',
12+
0.1e+2: '',
13+
1.0: '',
14+
1.00000: '',
15+
.1: '',
16+
.100000: '',
17+
0.1: '',
18+
0.100000: '',
19+
999999999999999999999999999999: '',
20+
.000000000000000000000000000001: '',
21+
0.000000000000000000000000000001: '',
22+
1e999999999999999999999999999999: '',
23+
1_2_3: '',
324
}

0 commit comments

Comments
 (0)