Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ts-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abcoder-ts-parser",
"version": "0.0.21",
"version": "0.0.24",
"description": "TypeScript AST parser for UNIAST specification",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion ts-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const program = new Command();
program
.name('abcoder-ts-parser')
.description('TypeScript AST parser for UNIAST specification')
.version('0.0.21');
.version('0.0.24');

program
.command('parse')
Expand Down
98 changes: 98 additions & 0 deletions ts-parser/src/parser/FunctionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MethodSignature,
GetAccessorDeclaration,
SetAccessorDeclaration,
PropertyDeclaration,
Node,
SyntaxKind,
ParameterDeclaration,
Expand Down Expand Up @@ -122,8 +123,23 @@ export class FunctionParser {
console.error('Error processing setter:', setter, error);
}
}

// Parse properties with function initializers (arrow functions or function expressions)
const properties = cls.getProperties();
for (const prop of properties) {
const initializer = prop.getInitializer();
if (initializer && (Node.isArrowFunction(initializer) || Node.isFunctionExpression(initializer))) {
try {
const propObj = this.parsePropertyFunction(prop, initializer, moduleName, packagePath, sourceFile, className);
functions[propObj.Name] = propObj;
} catch (error) {
console.error('Error processing property function:', prop, error);
}
}
}
}


// Parse arrow functions assigned to variables
const variableDeclarations = sourceFile.getVariableDeclarations();
for (const varDecl of variableDeclarations) {
Expand Down Expand Up @@ -650,6 +666,88 @@ export class FunctionParser {
};
}

private parsePropertyFunction(
prop: PropertyDeclaration,
funcExpr: ArrowFunction | FunctionExpression,
moduleName: string,
packagePath: string,
sourceFile: SourceFile,
className: string
): UniFunction {
const symbol = prop.getSymbol();
let propName = "";
if (symbol) {
propName = assignSymbolName(symbol);
} else {
propName = "anonymous_" + prop.getStart();
}
const startLine = prop.getStartLineNumber();
const startOffset = prop.getStart();
const endOffset = prop.getEnd();
const content = prop.getFullText();
const signature = this.extractSignature(funcExpr);

const parent = prop.getParent();
const parentSym = parent.getSymbol();
let isExported = false;
if (Node.isClassDeclaration(parent)) {
isExported = parent.isExported() || parent.isDefaultExport() || (this.defaultExportSymbol === parentSym && parentSym !== undefined);
} else if (Node.isClassExpression(parent)) {
const grandParent = parent.getParent();
if (Node.isVariableDeclaration(grandParent)) {
const varStatement = grandParent.getVariableStatement();
const varSymbol = grandParent.getSymbol();
isExported = varStatement ? (varStatement.isExported() || varStatement.isDefaultExport() || (this.defaultExportSymbol === varSymbol && varSymbol !== undefined)) : false;
}
}

// Parse receiver
const receiver: Receiver = {
IsPointer: false,
Type: {
ModPath: moduleName,
PkgPath: this.getPkgPath(packagePath),
Name: className
}
};

// Parse parameters
const params = this.parseParameters(funcExpr.getParameters(), moduleName, packagePath, sourceFile);

// Parse return types
const results = this.parseReturnTypes(funcExpr, moduleName, packagePath, sourceFile);

// Parse function calls
const functionCalls = this.extractFunctionCalls(funcExpr, moduleName, packagePath, sourceFile);
const methodCalls = this.extractMethodCalls(funcExpr, moduleName, packagePath, sourceFile);

// Extract type references and global variables
const types = this.extractTypeReferences(funcExpr, moduleName, packagePath, sourceFile);
const globalVars = this.extractGlobalVarReferences(funcExpr, moduleName, packagePath, sourceFile);

return {
ModPath: moduleName,
PkgPath: this.getPkgPath(packagePath),
Name: propName,
File: this.getRelativePath(sourceFile.getFilePath()),
Line: startLine,
StartOffset: startOffset,
EndOffset: endOffset,
Exported: isExported,
IsMethod: true,
IsInterfaceMethod: false,
Content: content,
Signature: signature,
Receiver: receiver,
Params: params,
Results: results,
FunctionCalls: functionCalls,
MethodCalls: methodCalls,
Types: types,
GlobalVars: globalVars
};
}

// Parse parameters and extract type dependencies
private parseParameters(parameters: ParameterDeclaration[], moduleName: string, packagePath: string, _sourceFile: SourceFile): Dependency[] {
const dependencies: Dependency[] = [];
Expand Down
Loading
Loading