Skip to content

Commit 6bd1502

Browse files
committed
chore: wip
chore: wip chore: wip
1 parent ea4cba6 commit 6bd1502

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/extract.ts

+34-29
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,58 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
66
let usedTypes = new Set<string>()
77
let importMap = new Map<string, Set<string>>()
88

9-
// Capture all imported types
10-
const importRegex = /import\s+type\s*\{([^}]+)\}\s*from\s*['"]([^'"]+)['"]/g
9+
// Capture all imports
10+
const importRegex = /import\s+(?:(type)\s+)?(?:(\{[^}]+\})|(\w+))(?:\s*,\s*(?:(\{[^}]+\})|(\w+)))?\s+from\s+['"]([^'"]+)['"]/g
1111
let importMatch
1212
while ((importMatch = importRegex.exec(fileContent)) !== null) {
13-
const types = importMatch[1].split(',').map(t => t.trim())
14-
const from = importMatch[2]
15-
if (!importMap.has(from)) importMap.set(from, new Set())
16-
types.forEach(type => importMap.get(from)!.add(type))
17-
}
18-
19-
// Function to add used types
20-
const addUsedType = (type: string) => {
21-
const cleanType = type.replace(/[\[\]?]/g, '').trim() // Remove brackets and question marks
22-
if (/^[A-Z]/.test(cleanType)) { // Only add if it starts with a capital letter (likely a type)
23-
usedTypes.add(cleanType)
13+
const [, isTypeImport, namedImports1, defaultImport1, namedImports2, defaultImport2, from] = importMatch
14+
const processImports = (imports: string | undefined, isType: boolean) => {
15+
if (imports) {
16+
const types = imports.replace(/[{}]/g, '').split(',').map(t => {
17+
const [name, alias] = t.split(' as ').map(s => s.trim())
18+
return { name: name.replace(/^type\s+/, ''), alias: alias || name.replace(/^type\s+/, '') }
19+
})
20+
if (!importMap.has(from)) importMap.set(from, new Set())
21+
types.forEach(({ name, alias }) => {
22+
importMap.get(from)!.add(name)
23+
})
24+
}
2425
}
26+
27+
processImports(namedImports1, !!isTypeImport)
28+
processImports(namedImports2, !!isTypeImport)
29+
if (defaultImport1) importMap.get(from)!.add(defaultImport1)
30+
if (defaultImport2) importMap.get(from)!.add(defaultImport2)
2531
}
2632

2733
// Handle exported functions with comments
28-
const exportedFunctionRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+(async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+))/g
34+
const exportedFunctionRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+)(async\s+)?(function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([^{]+))/g
2935
let match
3036
while ((match = exportedFunctionRegex.exec(fileContent)) !== null) {
31-
const [, comment, , isAsync, name, params, returnType] = match
37+
const [, comment, exportKeyword, isAsync, , name, params, returnType] = match
3238
const cleanParams = params.replace(/\s*=\s*[^,)]+/g, '')
33-
const declaration = `${comment || ''}export declare ${isAsync || ''}function ${name}(${cleanParams}): ${returnType.trim()}`
34-
declarations += `${declaration}\n\n`
39+
let cleanReturnType = returnType.trim()
3540

36-
// Check for types used in parameters
37-
const paramTypes = params.match(/:\s*([^,)=]+)/g) || []
38-
paramTypes.forEach(type => addUsedType(type.slice(1).trim()))
41+
if (isAsync && !cleanReturnType.startsWith('Promise')) {
42+
cleanReturnType = `Promise<${cleanReturnType}>`
43+
}
3944

40-
// Check for return type
41-
addUsedType(returnType.trim())
45+
const declaration = `${comment || ''}${exportKeyword}declare function ${name}(${cleanParams}): ${cleanReturnType}`
46+
declarations += `${declaration}\n\n`
47+
48+
// Add parameter types and return type to usedTypes
49+
params.match(/:\s*(\w+)/g)?.forEach(type => usedTypes.add(type.slice(1).trim()))
50+
cleanReturnType.match(/\b([A-Z]\w+)\b/g)?.forEach(type => usedTypes.add(type))
4251
}
4352

4453
// Handle other exports (interface, type, const)
4554
const otherExportRegex = /(\/\*\*[\s\S]*?\*\/\s*)?(export\s+((?:interface|type|const)\s+\w+(?:\s*=\s*[^;]+|\s*\{[^}]*\})));?/gs
4655
while ((match = otherExportRegex.exec(fileContent)) !== null) {
47-
const [, comment, exportStatement, declaration] = match
56+
const [, comment, exportStatement] = match
4857
declarations += `${comment || ''}${exportStatement}\n\n`
4958

50-
// Check for types used in the declaration
51-
const typeRegex = /\b([A-Z]\w+)\b/g
52-
let typeMatch
53-
while ((typeMatch = typeRegex.exec(declaration)) !== null) {
54-
addUsedType(typeMatch[1])
55-
}
59+
// Add types used in the export to usedTypes
60+
exportStatement.match(/\b([A-Z]\w+)\b/g)?.forEach(type => usedTypes.add(type))
5661
}
5762

5863
// Generate import statements for used types

0 commit comments

Comments
 (0)