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 7245f8b commit bc11fd4
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,55 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
}
}

// Function to parse object literal
const parseObjectLiteral = (str: string) => {
const obj: Record<string, string> = {}
str.split(',').forEach((pair) => {
const trimmedPair = pair.trim()
if (trimmedPair) {
const colonIndex = trimmedPair.indexOf(':')
if (colonIndex !== -1) {
const key = trimmedPair.slice(0, colonIndex).trim().replace(/['"]/g, '')
const value = trimmedPair.slice(colonIndex + 1).trim()
obj[key] = value
}
}
})
return obj
}

// Handle all declarations
const declarationRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(const|interface|type|function)\s+(\w+)[\s\S]*?(?:;|\})\s*)/g
const declarationRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(const|interface|type|function|async function)\s+(\w+)[\s\S]*?(?:;|\})\s*)/g
const declarationMatches = Array.from(fileContent.matchAll(declarationRegex))
for (const [, comment, declaration, declType, name] of declarationMatches) {
if (!processedDeclarations.has(name)) {
if (comment)
declarations += `${comment.trim()}\n`

if (declType === 'const') {
const constMatch = declaration.match(/export\s+const\s+(\w+)\s*:\s*([^=]+)=/)
const constMatch = declaration.match(/export\s+const\s+(\w+)\s*(?::\s*([^=]+)\s*)?=\s*(\{[^}]+\})/)
if (constMatch) {
declarations += `export declare const ${constMatch[1]}: ${constMatch[2].trim()}\n\n`
const [, constName, constType, constValue] = constMatch
// Parse the object literal
const parsedValue = parseObjectLiteral(constValue.slice(1, -1))
const formattedValue = Object.entries(parsedValue)
.map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? value : `'${value}'`}`)
.join('\n')
declarations += `export declare const ${constName}: {\n${formattedValue}\n}\n\n`
}
else {
declarations += `${declaration.trim()}\n\n`
// Fallback to the original declaration if parsing fails
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
}
}
else if (declType === 'function') {
const funcMatch = declaration.match(/export\s+function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+)/)
else if (declType === 'function' || declType === 'async function') {
const funcMatch = declaration.match(/export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+)/)
if (funcMatch) {
declarations += `export declare function ${funcMatch[1]}(${funcMatch[2]}): ${funcMatch[3].trim()}\n\n`
const [, isAsync, funcName, params, returnType] = funcMatch
declarations += `export declare ${isAsync || ''}function ${funcName}(${params}): ${returnType.trim()}\n\n`
}
else {
declarations += `${declaration.trim()}\n\n`
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
}
}
else {
Expand Down

0 comments on commit bc11fd4

Please # to comment.