Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Nov 5, 2024
1 parent a958eb5 commit b3dcb31
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 4 additions & 2 deletions fixtures/output/function.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export declare function getProduct(id: number): Promise<ApiResponse<Product>>;
export declare function authenticate(user: string, password: string): Promise<AuthResponse>;
export declare function dts(options?: DtsGenerationOption): BunPlugin;
export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: Options<T>): Promise<T>;
export declare function processData(data: string): string export function processData(data: number): number;
export declare function processData(data: boolean): boolean export function processData<T extends object>(data: T): T;
export declare function processData(data: string): string;
export declare function processData(data: number): number;
export declare function processData(data: boolean): boolean;
export declare function processData<T extends object>(data: T): T;
export declare function processData(data: unknown): unknown;
export declare function complexAsyncGenerator(): any;
export declare function isUser(value: unknown): value is User;
Expand Down
26 changes: 22 additions & 4 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,17 +1320,34 @@ function processFunctionBlock(cleanDeclaration: string, state: ProcessingState):
}
}

// Regular function detection
// Check for function declarations
if (!/^(?:export\s+)?(?:async\s+)?function\s+[a-zA-Z_$][\w$]*/.test(cleanDeclaration))
return false

debugLog('block-processing', 'Processing function declaration')

// Extract signature
// Handle potential overloads by splitting on newlines and semicolons
const declarations = cleanDeclaration
.split(/[\n;]/)
.map(d => d.trim())
.filter(d => d.startsWith('export function') || d.startsWith('function'))

if (declarations.length > 1) {
// Process each overload separately
declarations.forEach((declaration) => {
if (!declaration.endsWith('{')) { // Skip implementation
const processed = processFunction(declaration, state.usedTypes, declaration.startsWith('export'))
if (processed)
state.dtsLines.push(processed)
}
})
return true
}

// Extract signature for non-overloaded functions
let signatureEnd = 0
let parenDepth = 0
let angleDepth = 0
// const braceDepth = 0

for (let i = 0; i < cleanDeclaration.length; i++) {
const char = cleanDeclaration[i]
Expand Down Expand Up @@ -2006,12 +2023,13 @@ function processVariable(declaration: string, isExported: boolean, state: Proces
function processFunction(declaration: string, usedTypes?: Set<string>, isExported = true): string {
debugLog('process-function-start', `Starting to process: ${declaration}`)

// Normalize while preserving structure
// Normalize while preserving structure and remove any trailing semicolon
const normalizedDeclaration = declaration
.split('\n')
.map(line => line.trim())
.join(' ')
.replace(/\s+/g, ' ')
.replace(/;$/, '')

debugLog('process-function-normalized', `Normalized declaration: ${normalizedDeclaration}`)

Expand Down

0 comments on commit b3dcb31

Please # to comment.