-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescript-eslint.js
104 lines (94 loc) · 3.27 KB
/
typescript-eslint.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as parser from "@typescript-eslint/parser";
import { walk } from "estree-walker";
import fs from "node:fs";
import path from "node:path";
import { jsonStringifyTs } from "./utils/json.js";
import { makeUnitsFromTest } from "./utils/typescript-make-units-from-test.cjs";
const EXIT = {};
async function main() {
const srcDir = "typescript";
const srcFiles = fs.globSync([
"tests/cases/compiler/**/*",
"tests/cases/conformance/**/*",
], {
cwd: srcDir,
withFileTypes: true,
}).filter(e => !e.isDirectory()).map(e => path.join(e.parentPath, e.name));
const destDir = "test-typescript";
fs.rmSync(destDir, { recursive: true, force: true });
for (let i = 0; i < srcFiles.length; i++) {
// progress
process.stdout.write(`\r${i + 1} / ${srcFiles.length}`);
const srcFile = srcFiles[i];
let code = fs.readFileSync(srcFile, "utf-8");
// Trim off UTF-8 BOM
if (code.charCodeAt(0) === 0xFEFF) code = code.slice(1);
const { tests } = makeUnitsFromTest(srcFile, code);
if (tests.length === 0) {
continue;
}
let output = ``;
let hasError = false;
for (const test of tests) {
try {
const result = parser.parseForESLint(test.content, {
filePath: srcFile,
sourceType: test.sourceType.module ? "module" : "script",
ecmaFeatures: {
jsx: test.sourceType.jsx,
},
});
const { comments, tokens, ...program } = result.ast;
// TS-ESLint parser has no `unambiguous` option, so emulate it here
if (program.sourceType === "script") {
for (const { type } of program.body) {
if (
[
"ImportDeclaration",
"ExportAllDeclaration",
"ExportDefaultDeclaration",
"ExportNamedDeclaration",
"TSExportAssignment",
"TSNamespaceExportDeclaration",
].includes(type)
) {
program.sourceType = "module";
break;
}
}
if (program.sourceType === "script") {
// Didn't find any module declarations. Need to walk whole AST to search for `import.meta`.
try {
walk(program, {
enter(node) {
if (
node.type === "MetaProperty" && node.meta.type === "Identifier" && node.meta.name === "import" &&
node.property.type === "Identifier" && node.property.name === "meta"
) {
program.sourceType = "module";
throw EXIT; // Stop walking
}
},
});
} catch (err) {
if (err !== EXIT) throw err;
}
}
}
const astJson = jsonStringifyTs(program);
output += `__ESTREE_TEST__:PASS:` + "\n```json\n" + astJson + "\n```\n";
} catch (e) {
output += `__ESTREE_TEST__:FAIL:` + "\n```json\n" + e.message + "\n```\n";
hasError = true;
break;
}
}
if (hasError) {
continue;
}
const destFile = path.join(destDir, path.relative(srcDir, srcFile) + ".md");
fs.mkdirSync(path.dirname(destFile), { recursive: true });
fs.writeFileSync(destFile, output);
}
}
main();