@@ -17,27 +17,32 @@ namespace ts.OrganizeImports {
1717
1818 const changeTracker = textChanges . ChangeTracker . fromContext ( { host, formatContext } ) ;
1919
20+ const coalesceAndOrganizeImports = ( importGroup : ReadonlyArray < ImportDeclaration > ) => coalesceImports ( removeUnusedImports ( importGroup , sourceFile , program ) ) ;
21+
2022 // All of the old ImportDeclarations in the file, in syntactic order.
2123 const topLevelImportDecls = sourceFile . statements . filter ( isImportDeclaration ) ;
22- organizeImportsWorker ( topLevelImportDecls ) ;
24+ organizeImportsWorker ( topLevelImportDecls , coalesceAndOrganizeImports ) ;
2325
2426 // All of the old ExportDeclarations in the file, in syntactic order.
2527 const topLevelExportDecls = sourceFile . statements . filter ( isExportDeclaration ) ;
26- organizeImportsWorker ( topLevelExportDecls ) ;
28+ organizeImportsWorker ( topLevelExportDecls , coalesceExports ) ;
2729
2830 for ( const ambientModule of sourceFile . statements . filter ( isAmbientModule ) ) {
2931 const ambientModuleBody = getModuleBlock ( ambientModule as ModuleDeclaration ) ;
3032
3133 const ambientModuleImportDecls = ambientModuleBody . statements . filter ( isImportDeclaration ) ;
32- organizeImportsWorker ( ambientModuleImportDecls ) ;
34+ organizeImportsWorker ( ambientModuleImportDecls , coalesceAndOrganizeImports ) ;
3335
3436 const ambientModuleExportDecls = ambientModuleBody . statements . filter ( isExportDeclaration ) ;
35- organizeImportsWorker ( ambientModuleExportDecls ) ;
37+ organizeImportsWorker ( ambientModuleExportDecls , coalesceExports ) ;
3638 }
3739
3840 return changeTracker . getChanges ( ) ;
3941
40- function organizeImportsWorker ( oldImportDecls : ReadonlyArray < ImportDeclaration | ExportDeclaration > ) {
42+ function organizeImportsWorker < T extends ImportDeclaration | ExportDeclaration > (
43+ oldImportDecls : ReadonlyArray < T > ,
44+ coalesce : ( group : ReadonlyArray < T > ) => ReadonlyArray < T > ) {
45+
4146 if ( length ( oldImportDecls ) === 0 ) {
4247 return ;
4348 }
@@ -49,15 +54,11 @@ namespace ts.OrganizeImports {
4954 // but the consequences of being wrong are very minor.
5055 suppressLeadingTrivia ( oldImportDecls [ 0 ] ) ;
5156
52- const areImports = isImportDeclaration ( oldImportDecls [ 0 ] ) ;
53-
5457 const oldImportGroups = group ( oldImportDecls , importDecl => getExternalModuleName ( importDecl . moduleSpecifier ) ) ;
5558 const sortedImportGroups = stableSort ( oldImportGroups , ( group1 , group2 ) => compareModuleSpecifiers ( group1 [ 0 ] . moduleSpecifier , group2 [ 0 ] . moduleSpecifier ) ) ;
5659 const newImportDecls = flatMap ( sortedImportGroups , importGroup =>
5760 getExternalModuleName ( importGroup [ 0 ] . moduleSpecifier )
58- ? areImports
59- ? coalesceImports ( removeUnusedImports ( importGroup as ReadonlyArray < ImportDeclaration > , sourceFile , program ) )
60- : coalesceExports ( importGroup as ReadonlyArray < ExportDeclaration > )
61+ ? coalesce ( importGroup )
6162 : importGroup ) ;
6263
6364 // Delete or replace the first import.
0 commit comments