Skip to content

Commit bc11fd4

Browse files
committed
chore: wip
1 parent 7245f8b commit bc11fd4

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

src/extract.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,55 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
4747
}
4848
}
4949

50+
// Function to parse object literal
51+
const parseObjectLiteral = (str: string) => {
52+
const obj: Record<string, string> = {}
53+
str.split(',').forEach((pair) => {
54+
const trimmedPair = pair.trim()
55+
if (trimmedPair) {
56+
const colonIndex = trimmedPair.indexOf(':')
57+
if (colonIndex !== -1) {
58+
const key = trimmedPair.slice(0, colonIndex).trim().replace(/['"]/g, '')
59+
const value = trimmedPair.slice(colonIndex + 1).trim()
60+
obj[key] = value
61+
}
62+
}
63+
})
64+
return obj
65+
}
66+
5067
// Handle all declarations
51-
const declarationRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(const|interface|type|function)\s+(\w+)[\s\S]*?(?:;|\})\s*)/g
68+
const declarationRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(const|interface|type|function|async function)\s+(\w+)[\s\S]*?(?:;|\})\s*)/g
5269
const declarationMatches = Array.from(fileContent.matchAll(declarationRegex))
5370
for (const [, comment, declaration, declType, name] of declarationMatches) {
5471
if (!processedDeclarations.has(name)) {
5572
if (comment)
5673
declarations += `${comment.trim()}\n`
5774

5875
if (declType === 'const') {
59-
const constMatch = declaration.match(/export\s+const\s+(\w+)\s*:\s*([^=]+)=/)
76+
const constMatch = declaration.match(/export\s+const\s+(\w+)\s*(?::\s*([^=]+)\s*)?=\s*(\{[^}]+\})/)
6077
if (constMatch) {
61-
declarations += `export declare const ${constMatch[1]}: ${constMatch[2].trim()}\n\n`
78+
const [, constName, constType, constValue] = constMatch
79+
// Parse the object literal
80+
const parsedValue = parseObjectLiteral(constValue.slice(1, -1))
81+
const formattedValue = Object.entries(parsedValue)
82+
.map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? value : `'${value}'`}`)
83+
.join('\n')
84+
declarations += `export declare const ${constName}: {\n${formattedValue}\n}\n\n`
6285
}
6386
else {
64-
declarations += `${declaration.trim()}\n\n`
87+
// Fallback to the original declaration if parsing fails
88+
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
6589
}
6690
}
67-
else if (declType === 'function') {
68-
const funcMatch = declaration.match(/export\s+function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+)/)
91+
else if (declType === 'function' || declType === 'async function') {
92+
const funcMatch = declaration.match(/export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+)/)
6993
if (funcMatch) {
70-
declarations += `export declare function ${funcMatch[1]}(${funcMatch[2]}): ${funcMatch[3].trim()}\n\n`
94+
const [, isAsync, funcName, params, returnType] = funcMatch
95+
declarations += `export declare ${isAsync || ''}function ${funcName}(${params}): ${returnType.trim()}\n\n`
7196
}
7297
else {
73-
declarations += `${declaration.trim()}\n\n`
98+
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
7499
}
75100
}
76101
else {

0 commit comments

Comments
 (0)