-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.ts
366 lines (314 loc) · 14.6 KB
/
parse.ts
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
#!/usr/bin/env node
import ts from "typescript";
import fs from "node:fs";
import path from "node:path";
import { stripComments } from "jsonc-parser";
const tsconfig = JSON.parse(stripComments(fs.readFileSync("./tsconfig.json", "utf8")));
let rootDir = tsconfig.compilerOptions.rootDir;
const packageJson = JSON.parse(stripComments(fs.readFileSync("./package.json", "utf8")));
let entryPoint = path.resolve(`./${packageJson.main}`);
/**
* This is a helper function for logging.
* It uses the rest parameter syntax to accept an arbitrary number of arguments and
* could be used for debugging purposes by uncommenting the console.log(...args); line.
* @param args
*/
function log(...args: any[]) {
// console.log(...args);
}
/**
* Changes the current working directory of the Node.js process to the "src" directory.
* This affects the base directory from which the script operates, especially when reading files.
*/
process.chdir(rootDir);
/**
* Recursively scans a directory for TypeScript files.
*/
function scan(directory: string): string[] {
let files = fs.readdirSync(directory).map(file => path.resolve(directory + path.sep + file));
return files.map((file) => {
return fs.statSync(file).isDirectory() ? scan(file) : file;
}).flat().filter((fileName) => fileName.endsWith(".ts"));
}
/**
* Creates an array of file paths to be analyzed, starting with the main script and
* including all TypeScript files found by the scan function.
*/
let files = [
entryPoint,
...scan(".")
];
/**
* Create a TypeScript program object using the file paths.
* This program object is used to analyze the files.
*/
let program = ts.createProgram(files, {});
/**
* Retrieve a TypeScript type checker using the program object.
* This type checker object is used to analyze the type system.
*/
let typeChecker = program.getTypeChecker();
/**
* Filters the source files to include only those that are within the current working directory,
* ignoring external library definitions and other files outside the project's scope.
*/
let basePath = process.cwd() + path.sep;
let sourceFiles = program.getSourceFiles().filter((sourceFile: ts.SourceFile) => {
return sourceFile.fileName.startsWith(basePath);
});
let registry: any = {};
/**
* Iterate over each source file, extracting module names,
* analyzing import declarations to resolve import paths,
* and inspecting each node (e.g., class, interface)
* to gather information such as inheritance (extends),
* members (methods, properties), and parameters, including their types.
*/
sourceFiles.forEach((sourceFile: ts.SourceFile) => {
/**
* Compute relative module name based on the file path, adjusting it to remove the .ts extension.
* This name serves as an identifier for the module within the registry.
*/
let relativePath = "./" + path.relative(basePath, sourceFile.fileName);
if (relativePath.endsWith(".d.ts")) return;
let moduleName = path.dirname(relativePath) + "/" + path.basename(relativePath, ".ts");
log(moduleName);
let imports: any = {};
/**
* Iterate over all nodes (syntactic constructs) in the file.
*/
sourceFile.forEachChild((node: ts.Node) => {
/**
* For nodes that are import declarations, extract the path of the import and any imported identifiers.
* This is crucial for resolving type references and inheritance later on.
*/
if (ts.isImportDeclaration(node)) {
let importPath: string = "";
node.forEachChild((node: ts.Node) => {
if (ts.isStringLiteral(node)) {
importPath = node.text;
/**
* For relative imports (those starting with .),
* resolve the absolute path to ensure accurate linkage.
*/
if (importPath.charAt(0) == ".") {
importPath = path.resolve(path.dirname(sourceFile.fileName), importPath);
}
}
});
node.forEachChild((node: ts.Node) => {
/**
* Capture both default imports and named imports,
* and store this information in the imports object
* with the structure indicating the source of each import.
*/
if (ts.isImportClause(node)) {
node.forEachChild((node: ts.Node) => {
/**
* Handle default imports.
*/
if (ts.isIdentifier(node)) {
imports[node.escapedText as string] = {
from: importPath
}
}
/**
* Handle named imports.
*/
if (ts.isNamedImports(node)) {
node.elements.forEach((importSpecifier: ts.ImportSpecifier) => {
if (importSpecifier.propertyName) {
imports[importSpecifier.name.escapedText as string] = {
// @ts-ignore
name: importSpecifier.propertyName.escapedText,
from: importPath
}
}
else {
imports[importSpecifier.name.escapedText as string] = {
name: importSpecifier.name.escapedText,
from: importPath
}
}
});
}
});
}
});
}
});
/**
* Analyze exported symbols and extract class and interface definitions.
*/
let moduleSymbol = typeChecker.getSymbolAtLocation(sourceFile);
moduleSymbol?.exports?.forEach((exportedSymbol, name) => {
let fullyQualifiedExportName: string;
let reflectionData: any;
let exportedDeclaration = exportedSymbol.declarations?.pop();
if (exportedDeclaration?.kind == ts.SyntaxKind.ClassDeclaration) {
log(" ", exportedSymbol.escapedName, "(class)");
reflectionData = {
type: "class",
name: exportedSymbol.escapedName,
members: []
}
/**
* Check if the class extends another class.
*/
// @ts-ignore
if (exportedDeclaration?.heritageClauses) {
// @ts-ignore
log(" ", "extends", exportedDeclaration?.heritageClauses[0].types[0].expression.escapedText);
// @ts-ignore
let parentName = exportedDeclaration?.heritageClauses[0].types[0].expression.escapedText;
let typeReferenceModulePath = imports[parentName.split(".")[0]]?.from;
let typeReferenceModule;
if (typeReferenceModulePath) {
if (typeReferenceModulePath.charAt(0) == "/") {
let relativePath = "./" + path.relative(basePath, typeReferenceModulePath);
typeReferenceModule = relativePath + ":" + imports[parentName.split(".")[0]]?.name;
} else {
typeReferenceModule = typeReferenceModulePath + ":" + imports[parentName.split(".")[0]]?.name;
}
} else {
// console.log(parentName);
}
reflectionData.extends = {
type: parentName.split(".").pop(),
reference: typeReferenceModule
};
}
/**
* Analyze constructor and methods and extract parameter types, return types and modifiers.
*/
exportedSymbol.members?.forEach((memberSymbol) => {
let memberDeclaration = memberSymbol.declarations?.pop();
if (memberDeclaration?.kind == ts.SyntaxKind.Constructor ||
memberDeclaration?.kind == ts.SyntaxKind.MethodDeclaration) {
log(" ", memberSymbol.escapedName);
let member: any = {
name: memberSymbol.escapedName,
// @ts-ignore
abstract: memberDeclaration.modifiers?.some((keyword) => { return keyword.kind == ts.SyntaxKind.AbstractKeyword }),
parameters: []
}
// @ts-ignore
memberDeclaration?.parameters.forEach((param) => {
if (param.type) {
let parameter;
switch (param.type.kind) {
/**
* Parameter is a type reference. This can be a class, interface or type alias.
*/
case ts.SyntaxKind.TypeReference:
let typeName = param.type.typeName?.escapedText ? param.type.typeName.escapedText : `${param.type.typeName.left.escapedText}.${param.type.typeName.right.escapedText}`;
log(" ", typeName, imports[typeName.split(".")[0]]?.from);
let typeReferenceModulePath = imports[typeName.split(".")[0]]?.from;
let typeReferenceModule;
if (typeReferenceModulePath) {
if (typeReferenceModulePath.charAt(0) == "/") {
let relativePath = "./" + path.relative(basePath, typeReferenceModulePath);
typeReferenceModule = relativePath + ":" + typeName.split(".").pop();
} else {
typeReferenceModule = typeReferenceModulePath + ":" + typeName.split(".").pop();
}
} else {
// console.log(typeName);
}
parameter = {
type: typeName,
reference: typeReferenceModule
};
break;
case ts.SyntaxKind.AnyKeyword:
log(" ", "any");
parameter = {
type: "any"
};
break;
case ts.SyntaxKind.NumberKeyword:
log(" ", "number");
parameter = {
type: "number"
};
break;
case ts.SyntaxKind.ObjectKeyword:
log(" ", "object");
parameter = {
type: "object"
};
break;
case ts.SyntaxKind.StringKeyword:
log(" ", "string");
parameter = {
type: "string"
};
break;
case ts.SyntaxKind.ArrayType:
log(" ", "array");
// TODO: Handle array types (e.g. Array<string>).
parameter = {
type: "array"
};
break;
case ts.SyntaxKind.TypeLiteral:
log(" ", "literal");
parameter = {
type: "literal"
};
break;
default:
log(" ", param.type.kind);
parameter = {
type: "unknown"
};
}
member.parameters.push(parameter);
} else {
log(" unknown");
member.parameters.push({});
}
});
reflectionData.members.push(member);
}
});
}
if (exportedDeclaration?.kind == ts.SyntaxKind.InterfaceDeclaration) {
log(" ", exportedSymbol.escapedName, "(interface)");
reflectionData = {
type: "interface",
name: exportedSymbol.escapedName
}
}
if (reflectionData) {
fullyQualifiedExportName = moduleName + ":" + reflectionData.name
registry[fullyQualifiedExportName] = reflectionData;
}
});
});
// console.log(registry);
function getConstructorRecursive(reference: string): any {
if (registry[reference]) {
let constructor = registry[reference].members?.find((member: any) => {
return member.name == "__constructor";
});
if (constructor) {
return constructor;
} else {
if (registry[reference].extends) {
return registry[reference].extends.reference;
}
}
}
}
Object.values(registry).forEach((reflectedClass: any) => {
if (reflectedClass.type == "class" && reflectedClass.extends && !reflectedClass.members.some((member: any) => { return member.name == "__constructor" })) {
// console.log(reflectedClass.name);
let inheritedConstructor = getConstructorRecursive(reflectedClass.extends.reference);
if (inheritedConstructor) {
reflectedClass.members.push(inheritedConstructor);
}
}
})
console.log(JSON.stringify(registry, null, 2));
// fs.writeFileSync("../cache/reflection.json", JSON.stringify(registry, null, 2), { });