|
| 1 | +import ts from "typescript"; |
| 2 | +import { readdirSync, readFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import type { EntryPoint } from "./types.js"; |
| 5 | + |
| 6 | +function calleeName(expr: ts.Expression): string | null { |
| 7 | + if (ts.isIdentifier(expr)) return expr.text; |
| 8 | + if (ts.isPropertyAccessExpression(expr)) return expr.name.text; |
| 9 | + return null; |
| 10 | +} |
| 11 | + |
| 12 | +export function scanFile(fileName: string, source: string): EntryPoint | null { |
| 13 | + const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true); |
| 14 | + |
| 15 | + const ep: EntryPoint = { |
| 16 | + fileName, |
| 17 | + source, |
| 18 | + hasLoader: false, |
| 19 | + hasAction: false, |
| 20 | + loaderInitializerCallee: null, |
| 21 | + actionInitializerCallee: null, |
| 22 | + importedNames: [], |
| 23 | + calleeNames: [], |
| 24 | + hasTryCatch: false, |
| 25 | + statementCount: 0, |
| 26 | + }; |
| 27 | + |
| 28 | + const isExported = (n: ts.Node) => |
| 29 | + ts.canHaveModifiers(n) && |
| 30 | + ts.getModifiers(n)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) === true; |
| 31 | + |
| 32 | + const visit = (node: ts.Node) => { |
| 33 | + if (ts.isImportDeclaration(node) && node.importClause) { |
| 34 | + const bindings = node.importClause.namedBindings; |
| 35 | + if (bindings && ts.isNamedImports(bindings)) { |
| 36 | + for (const el of bindings.elements) ep.importedNames.push(el.name.text); |
| 37 | + } |
| 38 | + if (node.importClause.name) ep.importedNames.push(node.importClause.name.text); |
| 39 | + } |
| 40 | + |
| 41 | + if (ts.isVariableStatement(node) && isExported(node)) { |
| 42 | + for (const decl of node.declarationList.declarations) { |
| 43 | + const name = decl.name.getText(sf); |
| 44 | + if (name !== "loader" && name !== "action") continue; |
| 45 | + if (name === "loader") ep.hasLoader = true; |
| 46 | + if (name === "action") ep.hasAction = true; |
| 47 | + let init = decl.initializer; |
| 48 | + if (init && ts.isPropertyAccessExpression(init)) init = init.expression; |
| 49 | + if (init && ts.isCallExpression(init)) { |
| 50 | + const cn = calleeName(init.expression); |
| 51 | + if (name === "loader") ep.loaderInitializerCallee = cn; |
| 52 | + else ep.actionInitializerCallee = cn; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (ts.isFunctionDeclaration(node) && node.name && isExported(node)) { |
| 58 | + if (node.name.text === "loader") ep.hasLoader = true; |
| 59 | + if (node.name.text === "action") ep.hasAction = true; |
| 60 | + if (node.body) ep.statementCount += node.body.statements.length; |
| 61 | + } |
| 62 | + |
| 63 | + if (ts.isTryStatement(node)) ep.hasTryCatch = true; |
| 64 | + |
| 65 | + if (ts.isCallExpression(node)) { |
| 66 | + const cn = calleeName(node.expression); |
| 67 | + if (cn) ep.calleeNames.push(cn); |
| 68 | + } |
| 69 | + |
| 70 | + ts.forEachChild(node, visit); |
| 71 | + }; |
| 72 | + |
| 73 | + visit(sf); |
| 74 | + |
| 75 | + if (!ep.hasLoader && !ep.hasAction) return null; |
| 76 | + if (ep.statementCount === 0) { |
| 77 | + ep.statementCount = countArrowBodyStatements(sf); |
| 78 | + } |
| 79 | + return ep; |
| 80 | +} |
| 81 | + |
| 82 | +function countArrowBodyStatements(sf: ts.SourceFile): number { |
| 83 | + let count = 0; |
| 84 | + const visit = (n: ts.Node) => { |
| 85 | + if ((ts.isArrowFunction(n) || ts.isFunctionExpression(n)) && n.body && ts.isBlock(n.body)) { |
| 86 | + count += n.body.statements.length; |
| 87 | + } |
| 88 | + ts.forEachChild(n, visit); |
| 89 | + }; |
| 90 | + visit(sf); |
| 91 | + return count; |
| 92 | +} |
| 93 | + |
| 94 | +export function scanDirectory(dir: string): { |
| 95 | + entryPoints: EntryPoint[]; |
| 96 | + parseFailures: string[]; |
| 97 | +} { |
| 98 | + const entryPoints: EntryPoint[] = []; |
| 99 | + const parseFailures: string[] = []; |
| 100 | + const files = readdirSync(dir).filter((f) => /\.(ts|tsx)$/.test(f) && !f.endsWith(".test.ts")); |
| 101 | + |
| 102 | + for (const fileName of files) { |
| 103 | + try { |
| 104 | + const ep = scanFile(fileName, readFileSync(join(dir, fileName), "utf8")); |
| 105 | + if (ep) entryPoints.push(ep); |
| 106 | + } catch { |
| 107 | + parseFailures.push(fileName); |
| 108 | + } |
| 109 | + } |
| 110 | + return { entryPoints, parseFailures }; |
| 111 | +} |
0 commit comments