@@ -255,7 +255,9 @@ export function extractDtsTypes(sourceCode: string): string {
255
255
* // }
256
256
*/
257
257
function extractFunctionSignature ( declaration : string ) : FunctionSignature {
258
- // Get full declaration without early trimming
258
+ debugLog ( undefined , 'signature-start' , `Processing declaration: ${ declaration } ` )
259
+
260
+ // Get full declaration without early trimming to preserve structure
259
261
const cleanDeclaration = getCleanDeclaration ( declaration )
260
262
debugLog ( undefined , 'signature-clean' , `Clean declaration: ${ cleanDeclaration } ` )
261
263
@@ -267,12 +269,12 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
267
269
268
270
const name = functionMatch [ 1 ]
269
271
const contentAfterName = cleanDeclaration . slice ( cleanDeclaration . indexOf ( name ) + name . length )
272
+ debugLog ( undefined , 'signature-content' , `Content after name: ${ contentAfterName } ` )
270
273
271
274
// Handle generics
272
275
let generics = ''
273
276
let rest = contentAfterName . trim ( )
274
277
275
- // Extract complete generic block if it exists
276
278
if ( rest . startsWith ( '<' ) ) {
277
279
let depth = 0
278
280
let pos = 0
@@ -295,7 +297,9 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
295
297
}
296
298
}
297
299
298
- // Extract parameters
300
+ debugLog ( undefined , 'signature-generics' , `Extracted generics: ${ generics } ` )
301
+
302
+ // Extract parameters with improved destructuring support
299
303
let params = ''
300
304
if ( rest . startsWith ( '(' ) ) {
301
305
let depth = 0
@@ -319,6 +323,7 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
319
323
}
320
324
}
321
325
326
+ // Track depth for all bracket types
322
327
if ( ! insideString ) {
323
328
if ( char === '(' || char === '{' || char === '<' )
324
329
depth ++
@@ -329,14 +334,17 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
329
334
content . push ( char )
330
335
pos = i
331
336
332
- if ( depth === 0 && content [ 0 ] === '(' ) {
333
- params = content . join ( '' ) . slice ( 1 , - 1 ) . trim ( ) // Remove outer parentheses
337
+ // Break when we find closing parenthesis at root level
338
+ if ( depth === 0 && content [ 0 ] === '(' && char === ')' ) {
339
+ params = content . join ( '' ) . slice ( 1 , - 1 ) . trim ( )
334
340
rest = rest . slice ( pos + 1 ) . trim ( )
335
341
break
336
342
}
337
343
}
338
344
}
339
345
346
+ debugLog ( undefined , 'signature-params' , `Extracted params: ${ params } ` )
347
+
340
348
// Extract return type
341
349
let returnType = 'void'
342
350
if ( rest . startsWith ( ':' ) ) {
@@ -345,7 +353,7 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
345
353
let insideString = false
346
354
let stringChar = ''
347
355
348
- // Skip the colon
356
+ // Skip the colon and any whitespace
349
357
for ( let i = 1 ; i < rest . length ; i ++ ) {
350
358
const char = rest [ i ]
351
359
const prevChar = rest [ i - 1 ]
@@ -378,13 +386,9 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
378
386
returnType = content . join ( '' ) . trim ( )
379
387
}
380
388
381
- debugLog ( undefined , 'signature-parts' , `
382
- Name: ${ name }
383
- Generics: ${ generics }
384
- Params: ${ params }
385
- Return Type: ${ returnType }
386
- ` )
389
+ debugLog ( undefined , 'signature-return' , `Extracted return type: ${ returnType } ` )
387
390
391
+ // Preserve parameter structure exactly as written
388
392
return {
389
393
name,
390
394
generics,
@@ -1672,46 +1676,48 @@ function processVariable(declaration: string, isExported: boolean, state: Proces
1672
1676
/**
1673
1677
* Process function declarations with overloads
1674
1678
*/
1675
- function processFunction ( declaration : string , usedTypes ?: Set < string > , isExported = true ) : string {
1676
- debugLog ( undefined , 'process-function-start' , `Starting to process: ${ declaration } ` )
1677
-
1678
- const signature = extractFunctionSignature ( declaration )
1679
- debugLog ( undefined , 'process-function-signature' , JSON . stringify ( signature , null , 2 ) )
1680
-
1681
- // Build the declaration
1682
- const parts = [
1683
- isExported ? 'export ' : '' ,
1684
- 'declare function ' ,
1685
- signature . name ,
1686
- signature . generics ,
1687
- `(${ signature . params } )` ,
1688
- `: ${ signature . returnType } ` ,
1689
- ';' ,
1690
- ]
1691
-
1692
- const result = parts . filter ( Boolean ) . join ( '' )
1693
- debugLog ( undefined , 'process-function-final' , `Final declaration: ${ result } ` )
1694
- return result
1695
- }
1679
+ function processFunction ( declaration : string , usedTypes ?: Set < string > , isExported = true ) : string {
1680
+ debugLog ( undefined , 'process-function-start' , `Starting to process: ${ declaration } ` )
1681
+
1682
+ const signature = extractFunctionSignature ( declaration )
1683
+ debugLog ( undefined , 'process-function-signature' , JSON . stringify ( signature , null , 2 ) )
1684
+
1685
+ // Check if the function is async
1686
+ const isAsync = declaration . includes ( 'async function' )
1687
+ if ( isAsync && ! signature . returnType . includes ( 'Promise' ) ) {
1688
+ signature . returnType = `Promise<${ signature . returnType } >`
1689
+ }
1690
+
1691
+ // Build the declaration preserving all parts exactly
1692
+ const parts = [
1693
+ isExported ? 'export ' : '' ,
1694
+ 'declare function ' ,
1695
+ signature . name ,
1696
+ signature . generics ,
1697
+ `(${ signature . params } )` ,
1698
+ `: ${ signature . returnType } ` ,
1699
+ ';' ,
1700
+ ]
1701
+
1702
+ const result = parts . filter ( Boolean ) . join ( '' )
1703
+ debugLog ( undefined , 'process-function-final' , `Final declaration: ${ result } ` )
1704
+ return result
1705
+ }
1696
1706
1697
1707
function getCleanDeclaration ( declaration : string ) : string {
1698
- // Split on the first { that isn't inside a type definition
1699
- let depth = 0
1700
- let pos = 0
1708
+ // Remove leading comments while preserving the structure
1709
+ const lines = declaration . split ( '\n' )
1710
+ let startIndex = 0
1701
1711
1702
- for ( ; pos < declaration . length ; pos ++ ) {
1703
- const char = declaration [ pos ]
1704
- if ( char === '{' ) {
1705
- if ( depth === 0 && declaration [ pos - 1 ] !== ':' ) {
1706
- break
1707
- }
1708
- depth ++
1712
+ while ( startIndex < lines . length ) {
1713
+ const line = lines [ startIndex ] . trim ( )
1714
+ if ( ! line . startsWith ( '//' ) && ! line . startsWith ( '/*' ) && ! line . startsWith ( '*' ) && line !== '' ) {
1715
+ break
1709
1716
}
1710
- if ( char === '}' )
1711
- depth --
1717
+ startIndex ++
1712
1718
}
1713
1719
1714
- return declaration . slice ( 0 , pos ) . trim ( )
1720
+ return lines . slice ( startIndex ) . join ( '\n' ) . trim ( )
1715
1721
}
1716
1722
1717
1723
function processGeneratorFunction ( declaration : string , state ?: ProcessingState ) : string {
0 commit comments