Skip to content

Commit aff343c

Browse files
committed
chore: wip
1 parent e234c8a commit aff343c

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

fixtures/output/function.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export declare function fetchUsers(): Promise<ResponseData>;
44
export declare function getProduct(id: number): Promise<ApiResponse<Product>>;
55
export declare function authenticate(user: string, password: string): Promise<AuthResponse>;
66
export declare function dts(options?: DtsGenerationOption): BunPlugin;
7-
export declare function loadConfig<T extends Record<string, unknown>>(): void;
7+
export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: Options<T>): Promise<T>;
88
export declare function processData(data: string): string;
99
export declare function processData(data: number): number;
1010
export declare function processData(data: boolean): boolean;

src/extract.ts

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ export function extractDtsTypes(sourceCode: string): string {
255255
* // }
256256
*/
257257
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
259261
const cleanDeclaration = getCleanDeclaration(declaration)
260262
debugLog(undefined, 'signature-clean', `Clean declaration: ${cleanDeclaration}`)
261263

@@ -267,12 +269,12 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
267269

268270
const name = functionMatch[1]
269271
const contentAfterName = cleanDeclaration.slice(cleanDeclaration.indexOf(name) + name.length)
272+
debugLog(undefined, 'signature-content', `Content after name: ${contentAfterName}`)
270273

271274
// Handle generics
272275
let generics = ''
273276
let rest = contentAfterName.trim()
274277

275-
// Extract complete generic block if it exists
276278
if (rest.startsWith('<')) {
277279
let depth = 0
278280
let pos = 0
@@ -295,7 +297,9 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
295297
}
296298
}
297299

298-
// Extract parameters
300+
debugLog(undefined, 'signature-generics', `Extracted generics: ${generics}`)
301+
302+
// Extract parameters with improved destructuring support
299303
let params = ''
300304
if (rest.startsWith('(')) {
301305
let depth = 0
@@ -319,6 +323,7 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
319323
}
320324
}
321325

326+
// Track depth for all bracket types
322327
if (!insideString) {
323328
if (char === '(' || char === '{' || char === '<')
324329
depth++
@@ -329,14 +334,17 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
329334
content.push(char)
330335
pos = i
331336

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()
334340
rest = rest.slice(pos + 1).trim()
335341
break
336342
}
337343
}
338344
}
339345

346+
debugLog(undefined, 'signature-params', `Extracted params: ${params}`)
347+
340348
// Extract return type
341349
let returnType = 'void'
342350
if (rest.startsWith(':')) {
@@ -345,7 +353,7 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
345353
let insideString = false
346354
let stringChar = ''
347355

348-
// Skip the colon
356+
// Skip the colon and any whitespace
349357
for (let i = 1; i < rest.length; i++) {
350358
const char = rest[i]
351359
const prevChar = rest[i - 1]
@@ -378,13 +386,9 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
378386
returnType = content.join('').trim()
379387
}
380388

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}`)
387390

391+
// Preserve parameter structure exactly as written
388392
return {
389393
name,
390394
generics,
@@ -1672,46 +1676,48 @@ function processVariable(declaration: string, isExported: boolean, state: Proces
16721676
/**
16731677
* Process function declarations with overloads
16741678
*/
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+
}
16961706

16971707
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
17011711

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
17091716
}
1710-
if (char === '}')
1711-
depth--
1717+
startIndex++
17121718
}
17131719

1714-
return declaration.slice(0, pos).trim()
1720+
return lines.slice(startIndex).join('\n').trim()
17151721
}
17161722

17171723
function processGeneratorFunction(declaration: string, state?: ProcessingState): string {

0 commit comments

Comments
 (0)