Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 23, 2024
1 parent 017a250 commit d754d9c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ declare interface Options<T> {
cwd?: string;
defaultConfig: T;
}
export declare async function loadConfig<T extends Record<string, unknown>({ name, cwd, defaultConfig }: Options<T>): Promise<T>;
export declare async function loadConfig<T extends Record<string, unknown>(options: Options<T>): Promise<T>;
declare const dtsConfig: DtsGenerationConfig;
export { generate, dtsConfig }
export declare type { DtsGenerationOption }
Expand Down
28 changes: 11 additions & 17 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,7 @@ export function extractFunctionSignature(declaration: string): {
console.log('\n[Signature Extraction Debug]')
console.log('Input:', declaration)

// Check if the function is actually declared as async in the source
const isAsync = declaration.includes('async function')
console.log('Is Async:', isAsync)

const cleanDeclaration = declaration
.replace('export ', '')
.replace('async function ', 'function ')
Expand All @@ -817,15 +814,17 @@ export function extractFunctionSignature(declaration: string): {

console.log('Name and Generics:', { name, generics })

// Extract parameters with improved pattern matching
// Extract parameters with improved destructuring handling
const paramsMatch = cleanDeclaration.match(/\(([\s\S]*?)\)(?=\s*:)/)
let params = paramsMatch ? paramsMatch[1].trim() : ''

// Clean up destructured parameters to use simple parameter name
// Transform destructured parameters by extracting only the type
if (params.startsWith('{')) {
const paramTypeMatch = params.match(/:\s*([^}]+)\}/)
if (paramTypeMatch) {
params = `options: ${paramTypeMatch[1]}`
// Match the type after the colon, handling possible generic types
const typeMatch = params.match(/\}:\s*([^)]+)$/)
if (typeMatch) {
const paramType = typeMatch[1].trim()
params = `options: ${paramType}`
}
}

Expand Down Expand Up @@ -863,7 +862,6 @@ export function processFunctionDeclaration(
const functionSignature = declaration.split('{')[0].trim()
console.log('Signature:', functionSignature)

// Parse function parts
const {
name,
params,
Expand All @@ -880,7 +878,7 @@ export function processFunctionDeclaration(
generics,
})

// Track used types from generics
// Track used types
if (generics) {
const genericTypes = generics.slice(1, -1).split(',').map(t => t.trim())
genericTypes.forEach((type) => {
Expand All @@ -898,16 +896,15 @@ export function processFunctionDeclaration(
})
}

// Track used types from return type
if (returnType && returnType !== 'void') {
const typeMatches = returnType.match(/([A-Z_]\w*)/gi)
if (typeMatches) {
typeMatches.forEach(t => usedTypes.add(t))
}
}

// Build declaration parts
const parts = [
// Build the declaration
const result = [
isExported ? 'export ' : '',
'declare ',
isAsync ? 'async ' : '',
Expand All @@ -919,10 +916,7 @@ export function processFunctionDeclaration(
'): ',
returnType,
';',
]

const result = parts.join('')
console.log('Generated Declaration:', result)
].join('')

return result
.replace(/\s+/g, ' ')
Expand Down

0 comments on commit d754d9c

Please sign in to comment.