Skip to content

Commit eb812cf

Browse files
angrychowHoblovski
authored andcommitted
change type dependency append rules
1 parent 1ca0601 commit eb812cf

File tree

6 files changed

+230
-155
lines changed

6 files changed

+230
-155
lines changed

ts-parser/src/parser/FunctionParser.ts

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
PropertyAccessExpression,
1414
VariableDeclaration,
1515
Symbol,
16-
NewExpression,
17-
Identifier
16+
Identifier,
17+
TypeNode
1818
} from 'ts-morph';
1919
import { Function as UniFunction, Dependency, Receiver } from '../types/uniast';
2020
import { assignSymbolName, SymbolResolver } from '../utils/symbol-resolver';
@@ -46,8 +46,13 @@ export class FunctionParser {
4646
// Parse function declarations
4747
const functionDeclarations = sourceFile.getFunctions();
4848
for (const func of functionDeclarations) {
49-
const funcObj = this.parseFunction(func, moduleName, packagePath, sourceFile);
50-
functions[funcObj.Name] = funcObj;
49+
try {
50+
const funcObj = this.parseFunction(func, moduleName, packagePath, sourceFile);
51+
functions[funcObj.Name] = funcObj;
52+
} catch (error) {
53+
console.error('Error processing function:', func, error);
54+
}
55+
5156
}
5257

5358
// Parse method declarations in classes
@@ -63,22 +68,34 @@ export class FunctionParser {
6368
const methods = cls.getMethods();
6469

6570
for (const method of methods) {
66-
const methodObj = this.parseMethod(method, moduleName, packagePath, sourceFile, className);
67-
functions[methodObj.Name] = methodObj;
71+
try {
72+
const methodObj = this.parseMethod(method, moduleName, packagePath, sourceFile, className);
73+
functions[methodObj.Name] = methodObj;
74+
} catch (error) {
75+
console.error('Error processing method:', method, error);
76+
}
6877
}
6978

7079
// Parse constructors
7180
const constructors = cls.getConstructors();
7281
for (const ctor of constructors) {
73-
const ctorObj = this.parseConstructor(ctor, moduleName, packagePath, sourceFile, className);
74-
functions[ctorObj.Name] = ctorObj;
82+
try {
83+
const ctorObj = this.parseConstructor(ctor, moduleName, packagePath, sourceFile, className);
84+
functions[ctorObj.Name] = ctorObj;
85+
} catch (error) {
86+
console.error('Error processing constructor:', ctor, error);
87+
}
7588
}
7689

7790
// Parse static methods
7891
const staticMethods = cls.getStaticMethods();
7992
for (const staticMethod of staticMethods) {
80-
const methodObj = this.parseMethod(staticMethod, moduleName, packagePath, sourceFile, className);
81-
functions[methodObj.Name] = methodObj;
93+
try {
94+
const methodObj = this.parseMethod(staticMethod, moduleName, packagePath, sourceFile, className);
95+
functions[methodObj.Name] = methodObj;
96+
} catch (error) {
97+
console.error('Error processing static method:', staticMethod, error);
98+
}
8299
}
83100
}
84101

@@ -94,8 +111,13 @@ export class FunctionParser {
94111
} else {
95112
funcName = "anonymous_" + varDecl.getStart()
96113
}
97-
const funcObj = this.parseArrowFunction(initializer, funcName, moduleName, packagePath, sourceFile, varDecl);
98-
functions[funcObj.Name] = funcObj;
114+
try {
115+
const funcObj = this.parseArrowFunction(initializer, funcName, moduleName, packagePath, sourceFile, varDecl);
116+
functions[funcObj.Name] = funcObj;
117+
} catch (error) {
118+
console.error('Error processing arrow function:', initializer, error);
119+
}
120+
99121
}
100122
}
101123

@@ -105,8 +127,12 @@ export class FunctionParser {
105127
const methods = iface.getMethods();
106128

107129
for (const method of methods) {
108-
const methodObj = this.parseInterfaceMethod(method, moduleName, packagePath, sourceFile);
109-
functions[methodObj.Name] = methodObj;
130+
try {
131+
const methodObj = this.parseInterfaceMethod(method, moduleName, packagePath, sourceFile);
132+
functions[methodObj.Name] = methodObj;
133+
} catch (error) {
134+
console.error('Error processing interface method:', method, error);
135+
}
110136
}
111137
}
112138

@@ -414,7 +440,7 @@ export class FunctionParser {
414440
}
415441

416442
// External function could not find decls.
417-
if(resolvedSymbol.isExternal) {
443+
if (resolvedSymbol.isExternal) {
418444
calls.push(dep);
419445
continue;
420446
}
@@ -467,10 +493,10 @@ export class FunctionParser {
467493
if (Node.isIdentifier(expr)) {
468494
lastIdentifier = expr;
469495
} else if (Node.isPropertyAccessExpression(expr)) {
470-
lastIdentifier = expr.getNameNode();
496+
lastIdentifier = expr.getNameNode();
471497
}
472498

473-
if(lastIdentifier) {
499+
if (lastIdentifier) {
474500
this.processNewCall(node, lastIdentifier, moduleName, packagePath, sourceFile, calls, visited);
475501
}
476502
}
@@ -514,7 +540,7 @@ export class FunctionParser {
514540
EndOffset: resolvedSymbol.endOffset
515541
}
516542

517-
if(resolvedSymbol.isExternal) {
543+
if (resolvedSymbol.isExternal) {
518544
calls.push(dep);
519545
return;
520546
}
@@ -571,7 +597,7 @@ export class FunctionParser {
571597
EndOffset: resolvedSymbol.endOffset
572598
}
573599

574-
if(resolvedSymbol.isExternal) {
600+
if (resolvedSymbol.isExternal) {
575601
calls.push(dep);
576602
return;
577603
}
@@ -603,7 +629,24 @@ export class FunctionParser {
603629
const visited = new Set<string>();
604630

605631
// Extract from type references and find their definitions
606-
const typeNodes = node.getDescendantsOfKind(SyntaxKind.Identifier);
632+
const typeNodes: TypeNode[] = node.getDescendantsOfKind(SyntaxKind.TypeReference)
633+
634+
for (const param of node.getParameters()) {
635+
const t = param.getTypeNode()
636+
if (t) {
637+
typeNodes.push(t)
638+
}
639+
}
640+
641+
const returnTypeNode = node.getReturnTypeNode();
642+
if (returnTypeNode) typeNodes.push(returnTypeNode);
643+
644+
for (const typeParam of node.getTypeParameters()) {
645+
const constraint = typeParam.getConstraint();
646+
if (constraint) typeNodes.push(constraint);
647+
const def = typeParam.getDefault();
648+
if (def) typeNodes.push(def);
649+
}
607650

608651
for (const typeNode of typeNodes) {
609652
// Handle union and intersection types by extracting individual type references
@@ -696,7 +739,8 @@ export class FunctionParser {
696739

697740
// Global variable assignments
698741
(Node.isPropertyAssignment(parent) && parent.getNameNode() === identifier) ||
699-
(Node.isShorthandPropertyAssignment(parent) && parent.getNameNode() === identifier)
742+
(Node.isShorthandPropertyAssignment(parent) && parent.getNameNode() === identifier) ||
743+
Node.isTypeReference(parent)
700744
) {
701745
continue;
702746
}

ts-parser/src/parser/TypeParser.ts

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {
2-
SourceFile,
3-
ClassDeclaration,
4-
InterfaceDeclaration,
5-
TypeAliasDeclaration,
6-
EnumDeclaration,
1+
import {
2+
SourceFile,
3+
ClassDeclaration,
4+
InterfaceDeclaration,
5+
TypeAliasDeclaration,
6+
EnumDeclaration,
77
SyntaxKind,
88
TypeNode,
99
SymbolFlags,
@@ -32,37 +32,57 @@ export class TypeParser {
3232
// Parse class declarations
3333
const classes = sourceFile.getClasses();
3434
for (const cls of classes) {
35-
const typeObj = this.parseClass(cls, moduleName, packagePath, sourceFile);
36-
types[typeObj.Name] = typeObj;
35+
try {
36+
const typeObj = this.parseClass(cls, moduleName, packagePath, sourceFile);
37+
types[typeObj.Name] = typeObj;
38+
} catch (error) {
39+
console.error('Error processing class:', cls, error);
40+
}
3741
}
3842

3943
// Parse class expressions (anonymous classes)
4044
const classExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.ClassExpression);
4145
for (let i = 0; i < classExpressions.length; i++) {
4246
const classExpr = classExpressions[i];
43-
const typeObj = this.parseClassExpression(classExpr, moduleName, packagePath, sourceFile, i);
44-
types[typeObj.Name] = typeObj;
47+
try {
48+
const typeObj = this.parseClassExpression(classExpr, moduleName, packagePath, sourceFile, i);
49+
types[typeObj.Name] = typeObj;
50+
} catch (error) {
51+
console.error('Error processing class expression:', classExpr, error);
52+
}
4553
}
4654

4755
// Parse interface declarations
4856
const interfaces = sourceFile.getInterfaces();
4957
for (const iface of interfaces) {
50-
const typeObj = this.parseInterface(iface, moduleName, packagePath, sourceFile);
51-
types[typeObj.Name] = typeObj;
58+
try {
59+
const typeObj = this.parseInterface(iface, moduleName, packagePath, sourceFile);
60+
types[typeObj.Name] = typeObj;
61+
} catch (error) {
62+
console.error('Error processing interface:', iface, error);
63+
}
5264
}
5365

5466
// Parse type alias declarations
5567
const typeAliases = sourceFile.getTypeAliases();
5668
for (const typeAlias of typeAliases) {
57-
const typeObj = this.parseTypeAlias(typeAlias, moduleName, packagePath, sourceFile);
58-
types[typeObj.Name] = typeObj;
69+
try {
70+
const typeObj = this.parseTypeAlias(typeAlias, moduleName, packagePath, sourceFile);
71+
types[typeObj.Name] = typeObj;
72+
} catch (error) {
73+
console.error('Error processing type alias:', typeAlias, error);
74+
}
5975
}
6076

6177
// Parse enum declarations
6278
const enums = sourceFile.getEnums();
6379
for (const enumDecl of enums) {
64-
const typeObj = this.parseEnum(enumDecl, moduleName, packagePath, sourceFile);
65-
types[typeObj.Name] = typeObj;
80+
try {
81+
const typeObj = this.parseEnum(enumDecl, moduleName, packagePath, sourceFile);
82+
types[typeObj.Name] = typeObj;
83+
} catch (error) {
84+
console.error('Error processing enum:', enumDecl, error);
85+
}
6686
}
6787

6888
return types;
@@ -96,12 +116,12 @@ export class TypeParser {
96116
// Parse implemented interfaces and extended classes
97117
const implementsInterfaces: Dependency[] = [];
98118
const extendsClasses: Dependency[] = [];
99-
119+
100120
const heritageClauses = cls.getHeritageClauses();
101121
for (const clause of heritageClauses) {
102122
const clauseType = clause.getToken();
103123
const typeNodes = clause.getTypeNodes();
104-
124+
105125
for (const typeNode of typeNodes) {
106126
const dependencies = this.extractTypeDependencies(typeNode, moduleName, packagePath);
107127
if (clauseType === SyntaxKind.ImplementsKeyword) {
@@ -125,7 +145,7 @@ export class TypeParser {
125145

126146
// Combine implements and extends into Implements, but filter out external symbols
127147
const allImplements = [...implementsInterfaces, ...extendsClasses, ...propertyTypes];
128-
148+
129149
return {
130150
ModPath: moduleName,
131151
PkgPath: this.getPkgPath(packagePath),
@@ -214,7 +234,7 @@ export class TypeParser {
214234

215235
// Combine extends interfaces and other dependencies into Implements, but filter out external symbols
216236
const allImplements = [...extendsInterfaces, ...propertyTypes, ...methodTypes];
217-
237+
218238
return {
219239
ModPath: moduleName,
220240
PkgPath: this.getPkgPath(packagePath),
@@ -313,7 +333,7 @@ export class TypeParser {
313333

314334
// Extract from identifiers and find their definitions
315335
const identifiers = typeNode.getDescendantsOfKind(SyntaxKind.Identifier);
316-
336+
317337
for (const identifier of identifiers) {
318338
const symbol = identifier.getSymbol();
319339
if (!symbol || (symbol.getFlags() & (SymbolFlags.Type | SymbolFlags.TypeAlias | SymbolFlags.TypeLiteral)) === 0) {
@@ -342,7 +362,7 @@ export class TypeParser {
342362
const defEndOffset = decls[0].getEnd();
343363

344364
visited.add(key);
345-
let dep : Dependency = {
365+
let dep: Dependency = {
346366
ModPath: resolvedSymbol.moduleName || moduleName,
347367
PkgPath: this.getPkgPath(resolvedSymbol.packagePath || packagePath),
348368
Name: resolvedSymbol.name,
@@ -385,12 +405,12 @@ export class TypeParser {
385405
name = symbolName;
386406
}
387407
}
388-
408+
389409
const startLine = classExpr.getStartLineNumber();
390410
const startOffset = classExpr.getStart();
391411
const endOffset = classExpr.getEnd();
392412
const content = classExpr.getFullText();
393-
413+
394414
// Parse methods
395415
const methods: Record<string, any> = {};
396416
const classMethods = classExpr.getMethods();
@@ -406,15 +426,15 @@ export class TypeParser {
406426
// Parse implemented interfaces and extended classes
407427
const implementsInterfaces: Dependency[] = [];
408428
const extendsClasses: Dependency[] = [];
409-
429+
410430
const heritageClauses = classExpr.getHeritageClauses();
411431
for (const clause of heritageClauses) {
412432
const clauseType = clause.getToken();
413433
const typeNodes = clause.getTypeNodes();
414-
434+
415435
for (const typeNode of typeNodes) {
416436
const dependencies = this.extractTypeDependencies(typeNode, moduleName, packagePath);
417-
437+
418438
if (clauseType === SyntaxKind.ImplementsKeyword) {
419439
implementsInterfaces.push(...dependencies);
420440
} else if (clauseType === SyntaxKind.ExtendsKeyword) {

0 commit comments

Comments
 (0)