Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 20, 2024
1 parent 9c11ad7 commit 414908d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
declarations += `${pendingComment}\n`
pendingComment = ''
}
declarations += `export declare const ${constName}: {\n${formattedValue}\n}\n\n`
declarations += `export declare const ${constName}: {\n${formattedValue}\n}\n`
}
else {
// Handle constants initialized with function calls
Expand All @@ -97,15 +97,15 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
declarations += `${pendingComment}\n`
pendingComment = ''
}
declarations += `export declare const ${constName}: ${constType.trim()}\n\n`
declarations += `export declare const ${constName}: ${constType.trim()}\n`
}
else {
// Fallback to the original declaration if parsing fails
if (pendingComment) {
declarations += `${pendingComment}\n`
pendingComment = ''
}
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n`
}
}
}
Expand All @@ -114,7 +114,7 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
declarations += `${pendingComment}\n`
pendingComment = ''
}
declarations += `${declaration.trim()}\n\n`
declarations += `${declaration.trim()}\n`
}
else if (declType === 'function' || declType === 'async function') {
if (pendingComment) {
Expand All @@ -126,7 +126,7 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {

if (funcSignatureMatch) {
const [, isAsync, funcName, params, returnType] = funcSignatureMatch
declarations += `export declare ${isAsync || ''}function ${funcName}(${params.trim()}): ${returnType.trim()}\n\n`
declarations += `export declare ${isAsync || ''}function ${funcName}(${params.trim()}): ${returnType.trim()}\n`
}
else {
// If we can't match the full signature, let's try to extract what we can
Expand All @@ -140,12 +140,12 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
const returnTypeMatch = declaration.match(returnTypeRegex)
const returnType = returnTypeMatch ? returnTypeMatch[1].trim() : 'any'

declarations += `export declare ${isAsync || ''}function ${funcName}(${params.trim()}): ${returnType}\n\n`
declarations += `export declare ${isAsync || ''}function ${funcName}(${params.trim()}): ${returnType}\n`
}
else {
// If all else fails, just add 'declare' to the original export
const simplifiedDeclaration = declaration.replace(/export\s+/, '').split('{')[0].trim()
declarations += `export declare ${simplifiedDeclaration}\n\n`
declarations += `export declare ${simplifiedDeclaration}\n`
}
}
}
Expand All @@ -172,14 +172,14 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
const typeExportMatches = Array.from(fileContent.matchAll(typeExportRegex))
for (const [, typeList] of typeExportMatches) {
const types = typeList.split(',').map(t => t.trim())
exports += `\n\nexport type { ${types.join(', ')} }`
exports += `\nexport type { ${types.join(', ')} }`
}

// Handle default export
const defaultExportRegex = /export\s+default\s+(\w+)/
const defaultExportMatch = fileContent.match(defaultExportRegex)
if (defaultExportMatch) {
exports += `\n\nexport default ${defaultExportMatch[1]}`
exports += `\nexport default ${defaultExportMatch[1]}`
}

const output = [imports, declarations.trim(), exports.trim()].filter(Boolean).join('\n').trim()
Expand Down

0 comments on commit 414908d

Please sign in to comment.