-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (54 loc) · 1.81 KB
/
index.js
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
import { Parser } from "acorn";
import fs from "fs/promises";
import path from "path";
import YAML from "yaml";
import { jsonStringifyAcorn } from "./utils/json.js";
async function* walk(dir) {
for await (const d of await fs.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
await fs.rm("./test", { recursive: true, force: true });
for await (const p of walk("./test262/test")) {
if (p.includes("_FIXTURE") || p.includes("staging")) {
continue;
}
const code = await fs.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) {
console.log(p);
console.log("Cannot parse YAML config");
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";
await fs.mkdir(writePath.dir, { recursive: true });
try {
let ast = Parser.parse(code, {
ecmaVersion: "latest",
sourceType: module ? "module" : "script",
preserveParens: true,
allowHashBang: true,
allowReturnOutsideFunction: true,
// Note: Do not specify `allowAwaitOutsideFunction` option.
// It defaults to `true` for modules, `false` for scripts, which is what we want.
});
const json = jsonStringifyAcorn(ast);
await fs.writeFile(writeFile, json);
} catch (err) {
console.log(p);
console.log(err.message);
}
}
console.log("Done.");