forked from cloudwego/abcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryParser.ts
More file actions
432 lines (385 loc) · 15.2 KB
/
RepositoryParser.ts
File metadata and controls
432 lines (385 loc) · 15.2 KB
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
import { Project, ts } from 'ts-morph';
import * as path from 'path';
import * as fs from 'fs';
import { Repository, Node, Relation, Identity, Function } from '../types/uniast';
import { ModuleParser } from './ModuleParser';
import { TsConfigCache } from '../utils/tsconfig-cache';
import { MonorepoUtils } from '../utils/monorepo';
export class RepositoryParser {
private project?: Project;
private moduleParser?: ModuleParser;
private tsConfigCache: TsConfigCache;
private projectRoot: string;
private tsConfigPath?: string;
constructor(projectRoot: string, tsConfigPath?: string) {
this.tsConfigCache = TsConfigCache.getInstance();
this.projectRoot = projectRoot;
this.tsConfigPath = tsConfigPath;
}
async parseRepository(repoPath: string, options: { loadExternalSymbols?: boolean, noDist?: boolean, srcPatterns?: string[], monorepoMode?: 'combined' | 'separate' } = {}): Promise<Repository> {
const absolutePath = path.resolve(repoPath);
const repository: Repository = {
ASTVersion: "v0.1.3",
id: path.basename(absolutePath),
Modules: {},
Graph: {}
};
const isMonorepo = MonorepoUtils.isMonorepo(absolutePath);
if (isMonorepo) {
const packages = MonorepoUtils.getMonorepoPackages(absolutePath);
const monorepoMode = options.monorepoMode || 'combined';
// Using separate output mode - each package will be written to individual JSON files
if (monorepoMode === 'separate') {
for (const pkg of packages) {
const packageTsConfigPath = path.join(pkg.absolutePath, 'tsconfig.json');
if (fs.existsSync(packageTsConfigPath)) {
console.log(`Parsing package ${pkg.name || pkg.path} with tsconfig ${packageTsConfigPath}`);
try {
const project = new Project({
tsConfigFilePath: packageTsConfigPath,
compilerOptions: {
allowJs: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true
}
});
const moduleParser = new ModuleParser(project, this.projectRoot);
const module = await moduleParser.parseModule(pkg.absolutePath, pkg.path, options);
// Add module to main repository for combined output
repository.Modules[module.Name] = module;
// Create a separate repository for each package
const packageRepository: Repository = {
ASTVersion: "v0.1.3",
id: pkg.name || path.basename(pkg.absolutePath),
Modules: { [module.Name]: module },
Graph: {}
};
this.buildGlobalGraph(packageRepository);
// Write JSON file for this package
const sanitizedPackageName = (pkg.name || path.basename(pkg.absolutePath)).replace(/[\/\\:*?"<>|@]/g, '_');
const outputPath = path.join(process.cwd(), `${sanitizedPackageName}.json`);
const jsonOutput = JSON.stringify(packageRepository, null, 2);
fs.writeFileSync(outputPath, jsonOutput);
console.log(`Package ${pkg.name || pkg.path} written to: ${outputPath}`);
} catch (error) {
console.warn(`Failed to parse package ${pkg.name || pkg.path}:`, error);
}
} else {
console.log(`No tsconfig.json found for package ${pkg.name || pkg.path}, skipping.`);
}
}
console.log(`All packages have been written to separate files`);
console.log(`Total packages processed: ${packages.length}`);
} else {
// Using combined output mode - all packages will be merged into one JSON file
for (const pkg of packages) {
const packageTsConfigPath = path.join(pkg.absolutePath, 'tsconfig.json');
if (fs.existsSync(packageTsConfigPath)) {
try {
const project = new Project({
tsConfigFilePath: packageTsConfigPath,
compilerOptions: {
allowJs: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true
}
});
const moduleParser = new ModuleParser(project, this.projectRoot);
const module = await moduleParser.parseModule(pkg.absolutePath, pkg.path, options);
repository.Modules[module.Name] = module;
} catch (error) {
console.warn(`Failed to parse package ${pkg.name || pkg.path}:`, error);
}
} else {
console.log(`No tsconfig.json found for package ${pkg.name || pkg.path}, skipping.`);
}
}
}
} else {
console.log('Single project detected.');
this.project = this.createProjectForSingleRepo(this.projectRoot, this.tsConfigPath);
this.moduleParser = new ModuleParser(this.project, this.projectRoot);
const module = await this.moduleParser.parseModule(absolutePath, '.', options);
repository.Modules[module.Name] = module;
}
this.buildGlobalGraph(repository);
return repository;
}
private createProjectForSingleRepo(projectRoot: string, tsConfigPath?: string): Project {
let configPath = path.join(projectRoot, 'tsconfig.json');
if (tsConfigPath) {
let absoluteTsConfigPath = tsConfigPath;
if (!path.isAbsolute(absoluteTsConfigPath)) {
absoluteTsConfigPath = path.join(projectRoot, absoluteTsConfigPath);
}
configPath = absoluteTsConfigPath;
this.tsConfigCache.setGlobalConfigPath(absoluteTsConfigPath);
}
if (fs.existsSync(configPath)) {
const project = new Project({
tsConfigFilePath: configPath,
compilerOptions: {
allowJs: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true
}
});
const tsConfigQueue: string[] = [configPath];
const processedTsConfigs = new Set<string>();
while (tsConfigQueue.length > 0) {
const currentTsConfig = path.resolve(tsConfigQueue.shift()!);
if (processedTsConfigs.has(currentTsConfig)) {
continue;
}
processedTsConfigs.add(currentTsConfig);
const tsConfig_ = ts.readConfigFile(
currentTsConfig, ts.sys.readFile
);
if(tsConfig_.error) {
console.warn("parse tsconfig error", tsConfig_.error)
continue;
}
const parsedConfig = ts.parseJsonConfigFileContent(
tsConfig_.config,
ts.sys,
path.dirname(currentTsConfig)
);
if(parsedConfig.errors.length > 0) {
parsedConfig.errors.forEach(err => {
console.warn("parse tsconfig warning:", err.messageText)
});
}
project.addSourceFilesAtPaths(parsedConfig.fileNames);
const references = parsedConfig.projectReferences;
if (!references) {
continue;
}
for (const ref of references) {
const resolvedRef = ts.resolveProjectReferencePath(ref);
if (resolvedRef.length > 0) {
const refPath = path.resolve(path.dirname(currentTsConfig), resolvedRef);
if(fs.existsSync(refPath)) {
tsConfigQueue.push(refPath);
}
}
}
}
return project;
} else {
return new Project({
compilerOptions: {
target: 99,
module: 1,
allowJs: true,
checkJs: false,
skipLibCheck: true,
skipDefaultLibCheck: true,
strict: false,
noImplicitAny: false,
strictNullChecks: false,
strictFunctionTypes: false,
strictBindCallApply: false,
strictPropertyInitialization: false,
noImplicitReturns: false,
noFallthroughCasesInSwitch: false,
noUncheckedIndexedAccess: false,
noImplicitOverride: false,
noPropertyAccessFromIndexSignature: false,
allowUnusedLabels: false,
allowUnreachableCode: false,
exactOptionalPropertyTypes: false,
noImplicitThis: false,
alwaysStrict: false,
noImplicitUseStrict: false,
forceConsistentCasingInFileNames: true
}
});
}
}
private buildGlobalGraph(repository: Repository): void {
// First pass: Create all nodes from functions, types, and variables
for (const [, module] of Object.entries(repository.Modules)) {
for (const [, pkg] of Object.entries(module.Packages)) {
// Add functions to graph
for (const [, func] of Object.entries(pkg.Functions)) {
const nodeKey = this.createNodeKey(func.ModPath, func.PkgPath, func.Name);
const node: Node = {
ModPath: func.ModPath,
PkgPath: func.PkgPath,
Name: func.Name,
Type: 'FUNC'
};
// Add dependencies from function
node.Dependencies = this.extractDependenciesFromFunction(func, repository);
node.References = this.extractReferencesFromFunction(func, repository);
repository.Graph[nodeKey] = node;
}
// Add types to graph
for (const [, type] of Object.entries(pkg.Types)) {
const nodeKey = this.createNodeKey(type.ModPath, type.PkgPath, type.Name);
const node: Node = {
ModPath: type.ModPath,
PkgPath: type.PkgPath,
Name: type.Name,
Type: 'TYPE'
};
// Add implements relationships
if (type.Implements && type.Implements.length > 0) {
node.Implements = type.Implements.map(impl => this.createRelation(impl, 'Implement'));
}
repository.Graph[nodeKey] = node;
}
// Add variables to graph
for (const [, variable] of Object.entries(pkg.Vars)) {
const nodeKey = this.createNodeKey(variable.ModPath, variable.PkgPath, variable.Name);
const node: Node = {
ModPath: variable.ModPath,
PkgPath: variable.PkgPath,
Name: variable.Name,
Type: 'VAR'
};
// Add dependencies from variable
if (variable.Dependencies && variable.Dependencies.length > 0) {
node.Dependencies = variable.Dependencies.map(dep => this.createRelation(dep, 'Dependency'));
}
// Add groups from variable
if (variable.Groups && variable.Groups.length > 0) {
node.Groups = variable.Groups.map(group => this.createRelation(group, 'Group'));
}
repository.Graph[nodeKey] = node;
}
}
}
// Second pass: Add reverse relationships (References)
this.buildReverseRelationships(repository);
}
private createNodeKey(modPath: string, pkgPath: string, name: string): string {
return `${modPath}?${pkgPath}#${name}`;
}
private createRelation(identity: Identity, kind: Relation['Kind']): Relation {
return {
ModPath: identity.ModPath,
PkgPath: identity.PkgPath,
Name: identity.Name,
Kind: kind
};
}
private extractDependenciesFromFunction(func: Function, _repository: Repository): Relation[] {
const dependencies: Relation[] = [];
// Extract from function calls
if (func.FunctionCalls) {
for (const call of func.FunctionCalls) {
dependencies.push(this.createRelation(call, 'Dependency'));
}
}
// Extract from method calls
if (func.MethodCalls) {
for (const call of func.MethodCalls) {
dependencies.push(this.createRelation(call, 'Dependency'));
}
}
// Extract from types
if (func.Types) {
for (const type of func.Types) {
dependencies.push(this.createRelation(type, 'Dependency'));
}
}
// Extract from global variables
if (func.GlobalVars) {
for (const globalVar of func.GlobalVars) {
dependencies.push(this.createRelation(globalVar, 'Dependency'));
}
}
return dependencies;
}
private extractReferencesFromFunction(func: Function, _repository: Repository): Relation[] {
const references: Relation[] = [];
// Extract from parameters
if (func.Params) {
for (const param of func.Params) {
references.push(this.createRelation(param, 'Dependency'));
}
}
// Extract from results
if (func.Results) {
for (const result of func.Results) {
references.push(this.createRelation(result, 'Dependency'));
}
}
return references;
}
private buildReverseRelationships(repository: Repository): void {
// Build a map of all relations to create reverse references
const relationMap = new Map<string, Map<string, Relation[]>>();
// Collect all relations
for (const [nodeKey, node] of Object.entries(repository.Graph)) {
if (node.Dependencies) {
for (const dep of node.Dependencies) {
const targetKey = this.createNodeKey(dep.ModPath, dep.PkgPath, dep.Name);
if (!relationMap.has(targetKey)) {
relationMap.set(targetKey, new Map());
}
if (!relationMap.get(targetKey)!.has(nodeKey)) {
relationMap.get(targetKey)!.set(nodeKey, []);
}
relationMap.get(targetKey)!.get(nodeKey)!.push(dep);
}
}
}
// Add reverse references
for (const [targetKey, referringNodes] of relationMap) {
if (repository.Graph[targetKey]) {
const references: Relation[] = [];
for (const [sourceKey, relations] of referringNodes) {
for (const relation of relations) {
const sourceNode = repository.Graph[sourceKey];
if (sourceNode) {
references.push({
ModPath: sourceNode.ModPath,
PkgPath: sourceNode.PkgPath,
Name: sourceNode.Name,
Kind: 'Dependency'
});
} else {
// Handle missing nodes with UNKNOWN type
references.push({
ModPath: relation.ModPath,
PkgPath: relation.PkgPath,
Name: relation.Name,
Kind: 'Dependency'
});
}
}
}
repository.Graph[targetKey].References = references;
} else {
// Create missing node with UNKNOWN type
const parts = targetKey.split(/[?#]/);
const modPath = parts[0];
const pkgPath = parts[1];
const name = parts[2];
const missingNode: Node = {
ModPath: modPath,
PkgPath: pkgPath,
Name: name,
Type: 'UNKNOWN'
};
// Add references to the missing node
const references: Relation[] = [];
for (const [sourceKey, ] of referringNodes) {
const sourceNode = repository.Graph[sourceKey];
if (sourceNode) {
references.push({
ModPath: sourceNode.ModPath,
PkgPath: sourceNode.PkgPath,
Name: sourceNode.Name,
Kind: 'Dependency'
});
}
}
missingNode.References = references;
repository.Graph[targetKey] = missingNode;
}
}
}
}