From daa979770626b2a6eb5bd3f5eff23b2eda979cb4 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 17 Oct 2024 12:26:39 +0200 Subject: [PATCH] chore: wip --- src/extract.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/extract.ts b/src/extract.ts index 73fd35d..8157283 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -25,12 +25,19 @@ export async function extractTypeFromSource(filePath: string): Promise { } // Handle exported functions with comments - const exportedFunctionRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+))/g + const exportedFunctionRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+)(async\s+)?(function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+))/g let match while ((match = exportedFunctionRegex.exec(fileContent)) !== null) { - const [, comment, , isAsync, name, params, returnType] = match + const [, comment, exportKeyword, isAsync, funcDeclaration, name, params, returnType] = match const cleanParams = params.replace(/\s*=\s*[^,)]+/g, '') - const declaration = `${comment || ''}export declare ${isAsync || ''}function ${name}(${cleanParams}): ${returnType.trim()}` + let cleanReturnType = returnType.trim() + + // If the function is async, wrap the return type in Promise + if (isAsync) { + cleanReturnType = `Promise<${cleanReturnType}>` + } + + const declaration = `${comment || ''}${exportKeyword}declare function ${name}(${cleanParams}): ${cleanReturnType}` declarations += `${declaration}\n\n` // Check for types used in parameters @@ -38,7 +45,7 @@ export async function extractTypeFromSource(filePath: string): Promise { paramTypes.forEach(type => addUsedType(type.slice(1).trim())) // Check for return type - addUsedType(returnType.trim()) + addUsedType(cleanReturnType) } // Handle other exports (interface, type, const)