Skip to content

Commit f51f28f

Browse files
committed
chore: wip
1 parent 1533ddf commit f51f28f

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/extract.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,34 @@ export function processImport(importLine: string, typeSources: Map<string, strin
157157
/**
158158
* Filter out unused imports and only keep type imports
159159
*/
160-
export function processImports(imports: string[]): string[] {
161-
return imports.filter(line => line.trim().startsWith('import type'))
160+
function processImports(imports: string[]): string[] {
161+
const importMap = new Map<string, Set<string>>()
162+
163+
// Process each import line to extract module and types
164+
for (const line of imports) {
165+
const typeImportMatch = line.match(REGEX.typeImport)
166+
const regularImportMatch = line.match(REGEX.regularImport)
167+
const match = typeImportMatch || regularImportMatch
168+
169+
if (match) {
170+
const types = match[1].split(',').map(t => t.trim())
171+
const module = match[2]
172+
173+
// Add types to the set for this module
174+
if (!importMap.has(module)) {
175+
importMap.set(module, new Set())
176+
}
177+
types.forEach(type => importMap.get(module)!.add(type))
178+
}
179+
}
180+
181+
// Format imports
182+
return Array.from(importMap.entries())
183+
.map(([module, types]) => {
184+
const sortedTypes = Array.from(types).sort()
185+
return `import type { ${sortedTypes.join(', ')} } from '${module}';`
186+
})
187+
.sort() // Sort imports alphabetically
162188
}
163189

164190
/**
@@ -368,7 +394,7 @@ export function isFunction(value: string): boolean {
368394
/**
369395
* Infer array type from array literal
370396
*/
371-
export function inferArrayType(value: string): string {
397+
function inferArrayType(value: string): string {
372398
const content = extractNestedContent(value, '[', ']')
373399
if (!content)
374400
return 'never[]'
@@ -422,12 +448,12 @@ export function inferElementType(element: string): string {
422448
return formatObjectType(parseObjectLiteral(trimmed))
423449
}
424450

425-
// Handle function references and calls
451+
// Handle function references and calls - now parenthesized
426452
if (trimmed === 'console.log' || trimmed.endsWith('.log')) {
427453
return '((...args: any[]) => void)'
428454
}
429455

430-
// Handle arrow functions
456+
// Handle arrow functions - now parenthesized
431457
if (trimmed.includes('=>')) {
432458
return '((...args: any[]) => void)'
433459
}
@@ -513,7 +539,7 @@ export function inferReturnType(returnStatement: string): string {
513539
/**
514540
* Split array elements while respecting nested structures
515541
*/
516-
export function splitArrayElements(content: string): string[] {
542+
function splitArrayElements(content: string): string[] {
517543
const elements: string[] = []
518544
let current = ''
519545
let depth = 0
@@ -554,7 +580,6 @@ export function splitArrayElements(content: string): string[] {
554580
}
555581
}
556582

557-
// Add the last element
558583
if (current.trim()) {
559584
elements.push(current.trim())
560585
}
@@ -760,35 +785,30 @@ export function processDeclarationLine(line: string, state: ProcessingState): vo
760785
}
761786
}
762787

763-
function formatOutput(state: ProcessingState): string {
764-
const imports = processImports(state.imports)
788+
export function formatOutput(state: ProcessingState): string {
789+
const uniqueImports = processImports(state.imports)
765790
const dynamicImports = Array.from(state.usedTypes)
766791
.map((type) => {
767792
const source = state.typeSources.get(type)
768793
return source ? `import type { ${type} } from '${source}';` : ''
769794
})
770795
.filter(Boolean)
796+
.sort()
797+
798+
// Combine and deduplicate all imports
799+
const allImports = [...new Set([...uniqueImports, ...dynamicImports])]
771800

772801
const declarations = state.dtsLines.map(line =>
773802
line.startsWith('*') ? ` ${line}` : line,
774803
)
775804

776805
const result = [
777-
...imports,
778-
...dynamicImports,
806+
...allImports,
779807
'',
780808
...declarations,
781809
].filter(Boolean).join('\n')
782810

783-
// Handle default export
784-
if (state.defaultExport) {
785-
const cleanDefaultExport = state.defaultExport
786-
.replace(/^export\s+default\s+/, '')
787-
.replace(/;+$/, '')
788-
return `${result}\n\nexport default ${cleanDefaultExport};`
789-
}
790-
791-
return result
811+
return state.defaultExport ? `${result}\n\nexport default ${state.defaultExport.trim()};` : result
792812
}
793813

794814
/**

0 commit comments

Comments
 (0)