@@ -17,27 +17,32 @@ namespace ts.OrganizeImports {
17
17
18
18
const changeTracker = textChanges . ChangeTracker . fromContext ( { host, formatContext } ) ;
19
19
20
+ const coalesceAndOrganizeImports = ( importGroup : ReadonlyArray < ImportDeclaration > ) => coalesceImports ( removeUnusedImports ( importGroup , sourceFile , program ) ) ;
21
+
20
22
// All of the old ImportDeclarations in the file, in syntactic order.
21
23
const topLevelImportDecls = sourceFile . statements . filter ( isImportDeclaration ) ;
22
- organizeImportsWorker ( topLevelImportDecls ) ;
24
+ organizeImportsWorker ( topLevelImportDecls , coalesceAndOrganizeImports ) ;
23
25
24
26
// All of the old ExportDeclarations in the file, in syntactic order.
25
27
const topLevelExportDecls = sourceFile . statements . filter ( isExportDeclaration ) ;
26
- organizeImportsWorker ( topLevelExportDecls ) ;
28
+ organizeImportsWorker ( topLevelExportDecls , coalesceExports ) ;
27
29
28
30
for ( const ambientModule of sourceFile . statements . filter ( isAmbientModule ) ) {
29
31
const ambientModuleBody = getModuleBlock ( ambientModule as ModuleDeclaration ) ;
30
32
31
33
const ambientModuleImportDecls = ambientModuleBody . statements . filter ( isImportDeclaration ) ;
32
- organizeImportsWorker ( ambientModuleImportDecls ) ;
34
+ organizeImportsWorker ( ambientModuleImportDecls , coalesceAndOrganizeImports ) ;
33
35
34
36
const ambientModuleExportDecls = ambientModuleBody . statements . filter ( isExportDeclaration ) ;
35
- organizeImportsWorker ( ambientModuleExportDecls ) ;
37
+ organizeImportsWorker ( ambientModuleExportDecls , coalesceExports ) ;
36
38
}
37
39
38
40
return changeTracker . getChanges ( ) ;
39
41
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
+
41
46
if ( length ( oldImportDecls ) === 0 ) {
42
47
return ;
43
48
}
@@ -49,15 +54,11 @@ namespace ts.OrganizeImports {
49
54
// but the consequences of being wrong are very minor.
50
55
suppressLeadingTrivia ( oldImportDecls [ 0 ] ) ;
51
56
52
- const areImports = isImportDeclaration ( oldImportDecls [ 0 ] ) ;
53
-
54
57
const oldImportGroups = group ( oldImportDecls , importDecl => getExternalModuleName ( importDecl . moduleSpecifier ) ) ;
55
58
const sortedImportGroups = stableSort ( oldImportGroups , ( group1 , group2 ) => compareModuleSpecifiers ( group1 [ 0 ] . moduleSpecifier , group2 [ 0 ] . moduleSpecifier ) ) ;
56
59
const newImportDecls = flatMap ( sortedImportGroups , importGroup =>
57
60
getExternalModuleName ( importGroup [ 0 ] . moduleSpecifier )
58
- ? areImports
59
- ? coalesceImports ( removeUnusedImports ( importGroup as ReadonlyArray < ImportDeclaration > , sourceFile , program ) )
60
- : coalesceExports ( importGroup as ReadonlyArray < ExportDeclaration > )
61
+ ? coalesce ( importGroup )
61
62
: importGroup ) ;
62
63
63
64
// Delete or replace the first import.
0 commit comments