Skip to content

Commit

Permalink
Quote numeric keys in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 30, 2022
1 parent e301b1f commit c74b731
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 19 deletions.
26 changes: 18 additions & 8 deletions src/language-js/printer-estree-json.js
Expand Up @@ -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`
Expand All @@ -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",
Expand All @@ -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 === "+") {
Expand Down
110 changes: 100 additions & 10 deletions tests/format/json/json/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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: ""
}
================================================================================
Expand All @@ -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: "",
}
================================================================================
Expand All @@ -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: ""
}
================================================================================
Expand All @@ -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: "",
}
================================================================================
Expand All @@ -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": ""
}
================================================================================
Expand Down
11 changes: 10 additions & 1 deletion 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: '',
}

0 comments on commit c74b731

Please sign in to comment.