-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
440 lines (413 loc) · 13 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
const babelParser = require("@babel/parser");
const tsc = require("typescript");
const path = require("path");
const {join} = require("path");
const fs = require("fs");
const IGNORE_DIRS = [
"node_modules",
"venv",
"docs",
"test",
"tests",
"e2e",
"e2e-beta",
"examples",
"cypress",
"jest-cache",
"eslint-rules",
"codemods",
"flow-typed",
"i18n",
"vendor",
"www",
"dist",
"build",
];
const IGNORE_FILE_PATTERN = new RegExp("(conf|test|spec|[.-]min|\\.d)\\.(js|ts|jsx|tsx)$", "i");
const MAX_LOC_IN_FILE = 50000;
function countFileLines(filePath){
return new Promise((resolve, reject) => {
let lineCount = 0;
fs.createReadStream(filePath)
.on("data", (buffer) => {
let idx = -1;
lineCount--; // Because the loop will run once for idx=-1
do {
idx = buffer.indexOf(10, idx+1);
lineCount++;
} while (idx !== -1);
}).on("end", () => {
resolve(lineCount);
}).on("error", reject);
});
};
async function getAllFiles(dir, extn, files, result, regex) {
var allFiles = files || fs.readdirSync(dir);
var allResults = result || [];
var extRegex = regex || new RegExp(`\\${extn}$`);
for (let i = 0; i < allFiles.length; i++) {
const file = allFiles[i];
if (
file.startsWith(".") ||
file.startsWith("__") ||
IGNORE_FILE_PATTERN.test(file)
) {
continue;
}
const fileWithDir = join(dir, file);
if (fs.statSync(fileWithDir).isDirectory()) {
// Ignore directories
const dirName = path.basename(fileWithDir);
if (
dirName.startsWith(".") ||
dirName.startsWith("__") ||
IGNORE_DIRS.includes(dirName.toLowerCase())
) {
continue;
}
try {
allResults = await getAllFiles(fileWithDir, extn, fs.readdirSync(fileWithDir), allResults, extRegex);
} catch (error) {
}
} else {
let lines = await countFileLines(fileWithDir);
if (extRegex.test(fileWithDir)) {
if (lines > MAX_LOC_IN_FILE) {
console.warn(fileWithDir, "more than", MAX_LOC_IN_FILE, "lines of code");
} else allResults.push(fileWithDir);
}
}
}
return allResults;
};
const babelParserOptions = {
sourceType: "unambiguous",
allowImportExportEverywhere: true,
allowAwaitOutsideFunction: true,
allowNewTargetOutsideFunction: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
allowUndeclaredExports: true,
errorRecovery: true,
plugins: [
"optionalChaining",
"classProperties",
"decorators-legacy",
"exportDefaultFrom",
"doExpressions",
"numericSeparator",
"dynamicImport",
"jsx",
"typescript",
],
};
const babelSafeParserOptions = {
sourceType: "module",
allowImportExportEverywhere: true,
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true,
errorRecovery: true,
plugins: [
"optionalChaining",
"classProperties",
"decorators-legacy",
"exportDefaultFrom",
"doExpressions",
"numericSeparator",
"dynamicImport",
"typescript",
],
};
/**
* Return paths to all (j|tsx?) files.
*/
const getAllSrcJSAndTSFiles = (src) =>
Promise.all([
getAllFiles(src, ".js"),
getAllFiles(src, ".jsx"),
getAllFiles(src, ".cjs"),
getAllFiles(src, ".mjs"),
getAllFiles(src, ".ts"),
getAllFiles(src, ".tsx"),
]);
/**
* Convert a single JS/TS file to AST
*/
const fileToJsAst = (file) => {
try {
return babelParser.parse(
fs.readFileSync(file, "utf-8"),
babelParserOptions
);
} catch {
return babelParser.parse(
fs.readFileSync(file, "utf-8"),
babelSafeParserOptions
);
}
};
/**
* Convert a single JS/TS code snippet to AST
*/
const codeToJsAst = (code) => {
try {
return babelParser.parse(
code,
babelParserOptions
);
} catch {
return babelParser.parse(
code,
babelSafeParserOptions
);
}
};
const vueCleaningRegex = /<\/*script.*>|<style[\s\S]*style>|<\/*br>/ig;
const vueTemplateRegex = /(<template.*>)([\s\S]*)(<\/template>)/ig;
const vueCommentRegex = /<\!--[\s\S]*?-->/ig;
const vueBindRegex = /(:\[)([\S]*?)(\])/ig;
const vuePropRegex = /\s([.:@])([\S]*?=)/ig;
const vueOpenImgTag = /(<img)((?!>)[\s\S]+?)( [^\/]>)/ig;
/**
* Convert a single vue file to AST
*/
const toVueAst = (file) => {
const code = fs.readFileSync(file, "utf-8");
const cleanedCode = code
.replace(vueCommentRegex, function (match) {
return match.replaceAll(/\S/g, " ")
})
.replace(vueCleaningRegex, function (match) {
return match.replaceAll(/\S/g, " ").substring(1) + ";"
})
.replace(vueBindRegex, function (match, grA, grB, grC) {
return grA.replaceAll(/\S/g, " ") +
grB +
grC.replaceAll(/\S/g, " ")
})
.replace(vueTemplateRegex, function (match, grA, grB, grC) {
return grA +
grB.replace(vuePropRegex, function (match, grA, grB) {
return " " + grA.replace(/[.:@]/g, " ") + grB.replaceAll(".", "-")
})
.replace(vueOpenImgTag, function (match, grA, grB, grC) {
return grA + grB + grC.replace(" >", "/>")
})
.replaceAll("{{", "{ ")
.replaceAll("}}", " }") +
grC
});
return codeToJsAst(cleanedCode);
};
function createTsc(srcFiles) {
try {
const program = tsc.createProgram(srcFiles, {
target: tsc.ScriptTarget.ES2020,
module: tsc.ModuleKind.CommonJS,
allowJs: true,
allowUnreachableCode: true,
allowUnusedLabels: true,
alwaysStrict: false,
emitDecoratorMetadata: true,
exactOptionalPropertyTypes: true,
experimentalDecorators: false,
ignoreDeprecations: true,
noStrictGenericChecks: true,
noUncheckedIndexedAccess: false,
noPropertyAccessFromIndexSignature: false,
removeComments: true
});
const typeChecker = program.getTypeChecker();
const seenTypes = new Map();
function safeTypeToString(node) {
try {
return typeChecker.typeToString(node,
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
);
} catch (err) {
return "any";
}
}
function safeTypeWithContextToString(node, context) {
try {
return typeChecker.typeToString(node, context,
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
);
} catch (err) {
return "any";
}
}
function addType(node) {
let typeStr;
if (tsc.isSetAccessor(node) ||
tsc.isGetAccessor(node) ||
tsc.isConstructSignatureDeclaration(node) ||
tsc.isMethodDeclaration(node) ||
tsc.isFunctionDeclaration(node) ||
tsc.isConstructorDeclaration(node)) {
const signature = typeChecker.getSignatureFromDeclaration(node);
const returnType = typeChecker.getReturnTypeOfSignature(signature);
typeStr = safeTypeToString(returnType);
} else if (tsc.isFunctionLike(node)) {
const funcType = typeChecker.getTypeAtLocation(node);
const funcSignature = typeChecker.getSignaturesOfType(funcType, tsc.SignatureKind.Call)[0];
if (funcSignature) {
typeStr = safeTypeToString(funcSignature.getReturnType());
} else {
typeStr = safeTypeWithContextToString(typeChecker.getTypeAtLocation(node), node);
}
} else {
typeStr = safeTypeWithContextToString(typeChecker.getTypeAtLocation(node), node);
}
if (!["any", "unknown", "any[]", "unknown[]"].includes(typeStr)) seenTypes.set(node.getStart(), typeStr);
tsc.forEachChild(node, addType);
}
return {
program: program,
typeChecker: typeChecker,
addType: addType,
seenTypes: seenTypes
};
} catch (err) {
console.warn("Retrieving types", err.message);
return undefined;
}
}
/**
* Generate AST for JavaScript or TypeScript
*/
const createJSAst = async (options) => {
try {
const promiseMap = await getAllSrcJSAndTSFiles(options.src);
const srcFiles = promiseMap.flatMap((d) => d);
let ts;
if (options.tsTypes) {
ts = createTsc(srcFiles);
}
for (const file of srcFiles) {
if (fs.readFileSync(file, "utf-8").toString().includes("// EMSCRIPTEN_START_ASM")) {
console.warn("Parsing", file, ":", "File skipped as it contains EMSCRIPTEN code");
continue;
}
try {
const ast = fileToJsAst(file);
writeAstFile(file, ast, options);
if (ts) {
try {
const tsAst = ts.program.getSourceFile(file);
tsc.forEachChild(tsAst, ts.addType);
writeTypesFile(file, ts.seenTypes, options);
ts.seenTypes.clear();
} catch (err) {
console.warn("Retrieving types", file, ":", err.message);
}
}
} catch (err) {
console.error("Parsing", file, ":", err.message);
}
}
} catch (err) {
console.error(err);
}
};
/**
* Generate AST for .vue files
*/
const createVueAst = async (options) => {
const srcFiles = await getAllFiles(options.src, ".vue");
for (const file of srcFiles) {
try {
const ast = toVueAst(file);
if (ast) {
writeAstFile(file, ast, options);
}
} catch (err) {
console.error(file, err.message);
}
}
};
/**
* Deal with cyclic reference in json
*/
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
/**
* Write AST data to a json file
*/
const writeAstFile = (file, ast, options) => {
const relativePath = file.replace(new RegExp("^" + options.src + "/"), "");
const outAstFile = path.join(options.output, relativePath + ".json");
const data = {
fullName: file,
relativeName: relativePath,
ast: ast,
};
fs.mkdirSync(path.dirname(outAstFile), {recursive: true});
fs.writeFileSync(
outAstFile,
JSON.stringify(data, getCircularReplacer(), " ")
);
console.log("Converted AST for", relativePath, "to", outAstFile);
};
const writeTypesFile = (file, seenTypes, options) => {
const relativePath = file.replace(new RegExp("^" + options.src + "/"), "");
const outTypeFile = path.join(options.output, relativePath + ".typemap");
fs.mkdirSync(path.dirname(outTypeFile), {recursive: true});
fs.writeFileSync(
outTypeFile,
JSON.stringify(Object.fromEntries(seenTypes), undefined, " ")
);
console.log("Converted types for", relativePath, "to", outTypeFile);
};
const createXAst = async (options) => {
const src_dir = options.src;
try {
fs.accessSync(src_dir, fs.constants.R_OK);
} catch (err) {
console.error(src_dir, "is invalid");
process.exit(1);
}
if (
fs.existsSync(path.join(src_dir, "package.json")) ||
fs.existsSync(path.join(src_dir, "rush.json"))
) {
return await createJSAst(options);
}
console.error(src_dir, "unknown project type");
process.exit(1);
};
/**
* Method to start the ast generation process
*
* @args options CLI arguments
*/
const start = async (options) => {
let {type} = options;
if (!type) {
type = "";
}
type = type.toLowerCase();
switch (type) {
case "nodejs":
case "js":
case "javascript":
case "typescript":
case "ts":
return await createJSAst(options);
case "vue":
return await createVueAst(options);
default:
return await createXAst(options);
}
};
module.exports = {start: start}