Skip to content

Commit 1a4ec48

Browse files
committed
simpler serialization
1 parent 667755a commit 1a4ec48

File tree

3 files changed

+9
-29
lines changed

3 files changed

+9
-29
lines changed

test/output/bigint-zero.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "Literal",
88
"start": 0,
99
"end": 2,
10-
"value": 0,
10+
"value": "0",
1111
"raw": "0n",
1212
"bigint": "0"
1313
},

test/output/bigint.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"type": "Literal",
1919
"start": 6,
2020
"end": 9,
21-
"value": 42,
21+
"value": "42",
2222
"raw": "42n",
2323
"bigint": "42"
2424
}

test/parse-test.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {simple} from "acorn-walk";
21
import assert from "assert";
32
import * as fs from "fs";
43
import * as path from "path";
5-
import {parseCell, walk} from "@observablehq/parser";
4+
import {parseCell} from "@observablehq/parser";
65

76
(async () => {
87
for (const file of fs.readdirSync(path.join("test", "input"))) {
@@ -53,23 +52,7 @@ import {parseCell, walk} from "@observablehq/parser";
5352
}
5453
}
5554

56-
// Treat BigInt as Number for test purposes.
57-
if (cell.body) {
58-
simple(
59-
cell.body,
60-
{
61-
Literal(node) {
62-
if (node.bigint) {
63-
node.value = Number(node.value);
64-
}
65-
}
66-
},
67-
walk
68-
);
69-
}
70-
71-
// Convert to a suitable JSON representation.
72-
const actual = JSON.stringify(normalizeFeatures(cell), null, 2);
55+
const actual = JSON.stringify(cell, stringify, 2);
7356

7457
let expected;
7558
try {
@@ -89,12 +72,9 @@ import {parseCell, walk} from "@observablehq/parser";
8972
}
9073
})();
9174

92-
function normalizeFeatures(node) {
93-
return {
94-
...node,
95-
databaseClients: node.databaseClients && [...node.databaseClients],
96-
fileAttachments: node.fileAttachments && [...node.fileAttachments],
97-
secrets: node.secrets && [...node.secrets],
98-
tag: node.tag && normalizeFeatures(node.tag)
99-
};
75+
// Convert to a serializable representation.
76+
function stringify(key, value) {
77+
return typeof value === "bigint" ? value.toString()
78+
: value instanceof Map ? [...value]
79+
: value;
10080
}

0 commit comments

Comments
 (0)