Skip to content

Commit ddb23d1

Browse files
committed
chore: wip
1 parent bc11fd4 commit ddb23d1

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

fixtures/output/example-0002.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/**
22
* Example of another const declaration
33
*/
4-
export declare const settings: {
5-
theme: 'dark',
6-
language: 'en',
7-
}
4+
export declare const settings: { [key: string]: any }
85

96
export interface Product {
107
id: number

src/extract.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,60 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
6565
}
6666

6767
// Handle all declarations
68-
const declarationRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(const|interface|type|function|async 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]*?(?=export\s|$))/g
6969
const declarationMatches = Array.from(fileContent.matchAll(declarationRegex))
7070
for (const [, comment, declaration, declType, name] of declarationMatches) {
7171
if (!processedDeclarations.has(name)) {
7272
if (comment)
7373
declarations += `${comment.trim()}\n`
7474

7575
if (declType === 'const') {
76-
const constMatch = declaration.match(/export\s+const\s+(\w+)\s*(?::\s*([^=]+)\s*)?=\s*(\{[^}]+\})/)
76+
const constMatch = declaration.match(/export\s+const\s+(\w+)(\s*:\s*([^=]+))?\s*=\s*(\{[^}]+\})/)
7777
if (constMatch) {
78-
const [, constName, constType, constValue] = constMatch
78+
const [, constName, , , constValue] = constMatch
7979
// Parse the object literal
8080
const parsedValue = parseObjectLiteral(constValue.slice(1, -1))
8181
const formattedValue = Object.entries(parsedValue)
8282
.map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? value : `'${value}'`}`)
8383
.join('\n')
84+
8485
declarations += `export declare const ${constName}: {\n${formattedValue}\n}\n\n`
8586
}
8687
else {
8788
// Fallback to the original declaration if parsing fails
8889
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
8990
}
9091
}
92+
else if (declType === 'interface' || declType === 'type') {
93+
declarations += `${declaration.trim()}\n\n`
94+
}
9195
else if (declType === 'function' || declType === 'async function') {
92-
const funcMatch = declaration.match(/export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+)/)
93-
if (funcMatch) {
94-
const [, isAsync, funcName, params, returnType] = funcMatch
95-
declarations += `export declare ${isAsync || ''}function ${funcName}(${params}): ${returnType.trim()}\n\n`
96+
const funcSignatureRegex = /export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+)/
97+
const funcSignatureMatch = declaration.match(funcSignatureRegex)
98+
99+
if (funcSignatureMatch) {
100+
const [, isAsync, funcName, params, returnType] = funcSignatureMatch
101+
declarations += `export declare ${isAsync || ''}function ${funcName}(${params.trim()}): ${returnType.trim()}\n\n`
96102
}
97103
else {
98-
declarations += `export declare ${declaration.replace(/export\s+/, '').trim()}\n\n`
104+
// If we can't match the full signature, let's try to extract what we can
105+
const funcNameParamsRegex = /export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)/
106+
const funcNameParamsMatch = declaration.match(funcNameParamsRegex)
107+
108+
if (funcNameParamsMatch) {
109+
const [, isAsync, funcName, params] = funcNameParamsMatch
110+
// Try to find the return type
111+
const returnTypeRegex = /\)\s*:\s*([^{]+)/
112+
const returnTypeMatch = declaration.match(returnTypeRegex)
113+
const returnType = returnTypeMatch ? returnTypeMatch[1].trim() : 'any'
114+
115+
declarations += `export declare ${isAsync || ''}function ${funcName}(${params.trim()}): ${returnType}\n\n`
116+
}
117+
else {
118+
// If all else fails, just add 'declare' to the original export
119+
const simplifiedDeclaration = declaration.replace(/export\s+/, '').split('{')[0].trim()
120+
declarations += `export declare ${simplifiedDeclaration}\n\n`
121+
}
99122
}
100123
}
101124
else {

0 commit comments

Comments
 (0)