25 lines
1.0 KiB
TypeScript
25 lines
1.0 KiB
TypeScript
import ts from 'typescript';
|
|
|
|
export function parseFunctionInfo(sourceCode: string, functionName: string): { arguments: { name: string, type: string }[], returnType: string } | null {
|
|
const sourceFile = ts.createSourceFile('temp.ts', sourceCode, ts.ScriptTarget.ES2015, true);
|
|
|
|
let functionInfo: { arguments: { name: string, type: string }[], returnType: string } | null = null;
|
|
|
|
for (const node of sourceFile.statements) {
|
|
if (ts.isFunctionDeclaration(node) && node.name && node.name.text === functionName) {
|
|
const argumentTypes: { name: string, type: string }[] = node.parameters.map(parameter => {
|
|
const name = parameter.name.getText();
|
|
const type = parameter.type ? sourceCode.substring(parameter.type.pos, parameter.type.end) : 'any';
|
|
return { name, type };
|
|
});
|
|
|
|
const returnType = node.type ? sourceCode.substring(node.type.pos, node.type.end) : 'any';
|
|
|
|
functionInfo = { arguments: argumentTypes, returnType };
|
|
break;
|
|
}
|
|
}
|
|
|
|
return functionInfo;
|
|
}
|