Skip to content

Commit 9497d6f

Browse files
committed
chore: wip
chore: wip chore: wip
1 parent d74fcee commit 9497d6f

File tree

3 files changed

+216
-127
lines changed

3 files changed

+216
-127
lines changed

fixtures/input/example-0001.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,4 +476,71 @@ interface RegexPatterns {
476476
readonly moduleAugmentation: RegExp
477477
}
478478

479+
/**
480+
* Extract complete function signature using regex
481+
*/
482+
export function extractFunctionSignature(declaration: string): FunctionSignature {
483+
// Remove comments and clean up the declaration
484+
const cleanDeclaration = removeLeadingComments(declaration).trim()
485+
486+
const functionPattern = /^\s*(export\s+)?(async\s+)?function\s*(\*)?\s*([^(<\s]+)/
487+
const functionMatch = cleanDeclaration.match(functionPattern)
488+
489+
if (!functionMatch) {
490+
console.error('Function name could not be extracted from declaration:', declaration)
491+
return {
492+
name: '',
493+
params: '',
494+
returnType: 'void',
495+
generics: '',
496+
}
497+
}
498+
499+
const name = functionMatch[4]
500+
let rest = cleanDeclaration.slice(cleanDeclaration.indexOf(name) + name.length).trim()
501+
502+
// Extract generics
503+
let generics = ''
504+
if (rest.startsWith('<')) {
505+
const genericsResult = extractBalancedSymbols(rest, '<', '>')
506+
if (genericsResult) {
507+
generics = genericsResult.content
508+
rest = genericsResult.rest.trim()
509+
}
510+
}
511+
512+
// Extract parameters
513+
let params = ''
514+
if (rest.startsWith('(')) {
515+
const paramsResult = extractBalancedSymbols(rest, '(', ')')
516+
if (paramsResult) {
517+
params = paramsResult.content.slice(1, -1).trim()
518+
rest = paramsResult.rest.trim()
519+
}
520+
}
521+
522+
// Extract return type - keep it exactly as specified
523+
let returnType = 'void'
524+
if (rest.startsWith(':')) {
525+
const match = rest.match(/^:\s*([^{]+)/)
526+
if (match) {
527+
returnType = match[1].trim()
528+
}
529+
}
530+
531+
return {
532+
name,
533+
params,
534+
returnType: normalizeType(returnType),
535+
generics,
536+
}
537+
}
538+
539+
// export interface ImportTrackingState {
540+
// typeImports: Map<string, Set<string>>
541+
// valueImports: Map<string, Set<string>>
542+
// usedTypes: Set<string>
543+
// usedValues: Set<string>
544+
// }
545+
479546
export default dts

fixtures/output/example-0001.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ declare interface RegexPatterns {
185185
readonly moduleDeclaration: RegExp
186186
readonly moduleAugmentation: RegExp
187187
}
188+
/**
189+
* Extract complete function signature using regex
190+
*/
191+
export declare function extractFunctionSignature(declaration: string): FunctionSignature;
188192

189193
export { generate, dtsConfig }
190194
export type { DtsGenerationOption }

0 commit comments

Comments
 (0)