Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 17, 2024
1 parent ea4cba6 commit daa9797
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,27 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
}

// 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
const paramTypes = params.match(/:\s*([^,)=]+)/g) || []
paramTypes.forEach(type => addUsedType(type.slice(1).trim()))

// Check for return type
addUsedType(returnType.trim())
addUsedType(cleanReturnType)
}

// Handle other exports (interface, type, const)
Expand Down

0 comments on commit daa9797

Please sign in to comment.