export async function extractArgs(code: string) {
const { default: ts } = await import("npm:typescript");
const sourceFile = ts.createSourceFile(
"dummy.ts",
code,
ts.ScriptTarget.Latest,
);
function findFunctionDeclaration(
node: ts.Node,
): ts.FunctionDeclaration | undefined {
if (ts.isFunctionDeclaration(node)) {
return node;
}
return node.forEachChild(findFunctionDeclaration);
}
const functionDeclaration = findFunctionDeclaration(sourceFile);
if (!functionDeclaration) {
return null;
}
return functionDeclaration.parameters.map((param) => ({
name: code.slice(param.name.pos, param.name.end).trim(),
type: param.type
? code.slice(param.type.pos, param.type.end).trim()
: "any",
}));
}