From e17362a660e547e5652eb000073cac8ceaa32720 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 28 Oct 2024 15:10:51 +0100 Subject: [PATCH] chore: wip chore: wip --- fixtures/input/example-0001.ts | 25 +++++++++-------- fixtures/output/example-0001.d.ts | 1 + src/extract.ts | 45 +++++++------------------------ 3 files changed, 22 insertions(+), 49 deletions(-) diff --git a/fixtures/input/example-0001.ts b/fixtures/input/example-0001.ts index 106ff94..9061b51 100644 --- a/fixtures/input/example-0001.ts +++ b/fixtures/input/example-0001.ts @@ -352,19 +352,18 @@ export const complexArrays = { // } // } -// Complex Async Patterns -// due to isolatedDeclarations, we can assume the return type here -// export async function* complexAsyncGenerator(): any { -// const results = await Promise.all([ -// fetchUsers(), -// getProduct(1), -// authenticate('user', 'pass'), -// ]) - -// for (const result of results) { -// yield result -// } -// } +// Complex Async Patterns -> due to isolatedDeclarations, we can assume the return type here +export async function* complexAsyncGenerator(): any { + const results = await Promise.all([ + fetchUsers(), + getProduct(1), + authenticate('user', 'pass'), + ]) + + for (const result of results) { + yield result + } +} // Type Assertions and Guards export function isUser(value: unknown): value is User { diff --git a/fixtures/output/example-0001.d.ts b/fixtures/output/example-0001.d.ts index 71b59d9..cb37c7d 100644 --- a/fixtures/output/example-0001.d.ts +++ b/fixtures/output/example-0001.d.ts @@ -133,6 +133,7 @@ export declare const complexArrays: { matrix: Array>> | Array<'a' | 'b' | Array<'c' | 'd'>> | Array>>>; tuples: Array | Array<'literal' | 42 | false>>; }; +export declare function complexAsyncGenerator(): any; export declare function isUser(value: unknown): value is User; export declare type UserId = string & { readonly __brand: unique symbol }; export declare type ProductId = number & { diff --git a/src/extract.ts b/src/extract.ts index f6a5ae1..424181b 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -345,15 +345,13 @@ function extractFunctionName(declaration: string): { name: string, rest: string * Extract complete function signature using regex */ function extractFunctionSignature(declaration: string): FunctionSignature { - // Remove comments from the declaration + // Remove comments and clean up the declaration const cleanDeclaration = removeLeadingComments(declaration).trim() - // Remove leading 'export', 'async', 'function' keywords - const withoutKeywords = cleanDeclaration.replace(/^\s*(export\s+)?(async\s+)?function\s+/, '').trim() + const functionPattern = /^\s*(export\s+)?(async\s+)?function\s*(\*)?\s*([^(<\s]+)/ + const functionMatch = cleanDeclaration.match(functionPattern) - // Extract function name - const { name, rest: afterName } = extractFunctionName(withoutKeywords) - if (!name) { + if (!functionMatch) { console.error('Function name could not be extracted from declaration:', declaration) return { name: '', @@ -363,7 +361,8 @@ function extractFunctionSignature(declaration: string): FunctionSignature { } } - let rest = afterName + const name = functionMatch[4] + let rest = cleanDeclaration.slice(cleanDeclaration.indexOf(name) + name.length).trim() // Extract generics let generics = '' @@ -373,9 +372,6 @@ function extractFunctionSignature(declaration: string): FunctionSignature { generics = genericsResult.content rest = genericsResult.rest.trim() } - else { - console.error('Generics could not be extracted from declaration:', declaration) - } } // Extract parameters @@ -383,30 +379,12 @@ function extractFunctionSignature(declaration: string): FunctionSignature { if (rest.startsWith('(')) { const paramsResult = extractBalancedSymbols(rest, '(', ')') if (paramsResult) { - params = paramsResult.content.slice(1, -1).trim() // Remove the surrounding parentheses + params = paramsResult.content.slice(1, -1).trim() rest = paramsResult.rest.trim() } - else { - console.error('Parameters could not be extracted from declaration:', declaration) - return { - name, - params: '', - returnType: 'void', - generics, - } - } - } - else { - console.error('Parameters could not be extracted from declaration:', declaration) - return { - name, - params: '', - returnType: 'void', - generics, - } } - // Extract return type + // Extract return type - keep it exactly as specified let returnType = 'void' if (rest.startsWith(':')) { const match = rest.match(/^:\s*([^{]+)/) @@ -895,17 +873,12 @@ function processFunctionDeclaration( generics, } = extractFunctionSignature(cleanDeclaration) - // Add logs to verify extracted components - console.log('Function Name:', name) - console.log('Generics:', generics) - console.log('Parameters:', params) - console.log('Return Type:', returnType) - // Track used types if provided if (usedTypes) { trackUsedTypes(`${generics} ${params} ${returnType}`, usedTypes) } + // Build the declaration string const parts = [ isExported ? 'export' : '', 'declare',