-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
87 lines (73 loc) · 2.19 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Parser } from "acorn";
import fs from "fs";
import path from "path";
import YAML from "yaml";
import traverse from "traverse";
async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
for await (const p of walk("./test262/test")) {
if (p.includes("_FIXTURE") || p.includes("staging")) {
continue;
}
const code = await fs.promises.readFile(path.join("./", p), "utf8");
const start = code.indexOf("/*---");
const end = code.indexOf("---*/");
const yaml = code.substring(start + 5, end);
let preamble;
try {
preamble = YAML.parse(yaml);
} catch(err) {
continue
}
const negative =
preamble.negative?.phase === "parse" &&
preamble.negative?.type === "SyntaxError";
if (negative) {
continue;
}
const module = preamble.flags?.includes("module");
const writePath = path.parse(path.join("./", p.replace(/^test262\//, "")));
const writeFile = writePath.dir + "/" + writePath.name + ".json";
if (!fs.existsSync(writePath.dir)) {
fs.mkdirSync(writePath.dir, {
recursive: true,
});
}
try {
let astJson = Parser.parse(code, {
ecmaVersion: "latest",
sourceType: module ? "module" : "script",
preserveParens: true,
allowHashBang: true,
allowReturnOutsideFunction: true,
allowAwaitOutsideFunction: true,
});
const bigIntSerializer = (_key, value) => {
return typeof value === "bigint" ? value.toString() + "n" : value;
};
// remove references
astJson = JSON.parse(JSON.stringify(astJson, bigIntSerializer));
// omit the raw field, which is useless for test comparisons
traverse(astJson).forEach((node) => {
if (node && node.type === "Literal") {
if (node.bigint) {
delete node.bigint;
}
}
});
await fs.promises.writeFile(writeFile, JSON.stringify(astJson, null, 2));
} catch (err) {
if (fs.existsSync(writeFile)) {
fs.unlinkSync(writeFile);
console.log("Removed: ", writeFile);
}
console.log(p);
console.log(err.message);
}
}
console.log("Done.");