@@ -157,8 +157,34 @@ export function processImport(importLine: string, typeSources: Map<string, strin
157
157
/**
158
158
* Filter out unused imports and only keep type imports
159
159
*/
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
162
188
}
163
189
164
190
/**
@@ -368,7 +394,7 @@ export function isFunction(value: string): boolean {
368
394
/**
369
395
* Infer array type from array literal
370
396
*/
371
- export function inferArrayType ( value : string ) : string {
397
+ function inferArrayType ( value : string ) : string {
372
398
const content = extractNestedContent ( value , '[' , ']' )
373
399
if ( ! content )
374
400
return 'never[]'
@@ -422,12 +448,12 @@ export function inferElementType(element: string): string {
422
448
return formatObjectType ( parseObjectLiteral ( trimmed ) )
423
449
}
424
450
425
- // Handle function references and calls
451
+ // Handle function references and calls - now parenthesized
426
452
if ( trimmed === 'console.log' || trimmed . endsWith ( '.log' ) ) {
427
453
return '((...args: any[]) => void)'
428
454
}
429
455
430
- // Handle arrow functions
456
+ // Handle arrow functions - now parenthesized
431
457
if ( trimmed . includes ( '=>' ) ) {
432
458
return '((...args: any[]) => void)'
433
459
}
@@ -513,7 +539,7 @@ export function inferReturnType(returnStatement: string): string {
513
539
/**
514
540
* Split array elements while respecting nested structures
515
541
*/
516
- export function splitArrayElements ( content : string ) : string [ ] {
542
+ function splitArrayElements ( content : string ) : string [ ] {
517
543
const elements : string [ ] = [ ]
518
544
let current = ''
519
545
let depth = 0
@@ -554,7 +580,6 @@ export function splitArrayElements(content: string): string[] {
554
580
}
555
581
}
556
582
557
- // Add the last element
558
583
if ( current . trim ( ) ) {
559
584
elements . push ( current . trim ( ) )
560
585
}
@@ -760,35 +785,30 @@ export function processDeclarationLine(line: string, state: ProcessingState): vo
760
785
}
761
786
}
762
787
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 )
765
790
const dynamicImports = Array . from ( state . usedTypes )
766
791
. map ( ( type ) => {
767
792
const source = state . typeSources . get ( type )
768
793
return source ? `import type { ${ type } } from '${ source } ';` : ''
769
794
} )
770
795
. filter ( Boolean )
796
+ . sort ( )
797
+
798
+ // Combine and deduplicate all imports
799
+ const allImports = [ ...new Set ( [ ...uniqueImports , ...dynamicImports ] ) ]
771
800
772
801
const declarations = state . dtsLines . map ( line =>
773
802
line . startsWith ( '*' ) ? ` ${ line } ` : line ,
774
803
)
775
804
776
805
const result = [
777
- ...imports ,
778
- ...dynamicImports ,
806
+ ...allImports ,
779
807
'' ,
780
808
...declarations ,
781
809
] . filter ( Boolean ) . join ( '\n' )
782
810
783
- // Handle default export
784
- if ( state . defaultExport ) {
785
- const cleanDefaultExport = state . defaultExport
786
- . replace ( / ^ e x p o r t \s + d e f a u l t \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
792
812
}
793
813
794
814
/**
0 commit comments