@@ -16,43 +16,42 @@ namespace ts.OrganizeImports {
16
16
// TODO (https://github.com/Microsoft/TypeScript/issues/10020): sort *within* ambient modules (find using isAmbientModule)
17
17
18
18
// All of the old ImportDeclarations in the file, in syntactic order.
19
- const oldImportDecls = sourceFile . statements . filter ( isImportDeclaration ) ;
19
+ const topLevelImportDecls = sourceFile . statements . filter ( isImportDeclaration ) ;
20
20
21
- if ( oldImportDecls . length === 0 ) {
22
- return [ ] ;
23
- }
24
-
25
- const oldImportGroups = group ( oldImportDecls , importDecl => getExternalModuleName ( importDecl . moduleSpecifier ) ) ;
26
-
27
- const sortedImportGroups = stableSort ( oldImportGroups , ( group1 , group2 ) =>
28
- compareModuleSpecifiers ( group1 [ 0 ] . moduleSpecifier , group2 [ 0 ] . moduleSpecifier ) ) ;
21
+ const changeTracker = textChanges . ChangeTracker . fromContext ( { host, formatContext } ) ;
22
+ organizeImportsWorker ( topLevelImportDecls ) ;
23
+ return changeTracker . getChanges ( ) ;
29
24
30
- const newImportDecls = flatMap ( sortedImportGroups , importGroup =>
31
- getExternalModuleName ( importGroup [ 0 ] . moduleSpecifier )
32
- ? coalesceImports ( removeUnusedImports ( importGroup , sourceFile , program ) )
33
- : importGroup ) ;
25
+ function organizeImportsWorker ( oldImportDecls : ReadonlyArray < ImportDeclaration > ) {
26
+ if ( length ( oldImportDecls ) === 0 ) {
27
+ return ;
28
+ }
34
29
35
- const changeTracker = textChanges . ChangeTracker . fromContext ( { host, formatContext } ) ;
30
+ const oldImportGroups = group ( oldImportDecls , importDecl => getExternalModuleName ( importDecl . moduleSpecifier ) ) ;
31
+ const sortedImportGroups = stableSort ( oldImportGroups , ( group1 , group2 ) => compareModuleSpecifiers ( group1 [ 0 ] . moduleSpecifier , group2 [ 0 ] . moduleSpecifier ) ) ;
32
+ const newImportDecls = flatMap ( sortedImportGroups , importGroup =>
33
+ getExternalModuleName ( importGroup [ 0 ] . moduleSpecifier )
34
+ ? coalesceImports ( removeUnusedImports ( importGroup , sourceFile , program ) )
35
+ : importGroup ) ;
36
36
37
- // Delete or replace the first import.
38
- if ( newImportDecls . length === 0 ) {
39
- changeTracker . deleteNode ( sourceFile , oldImportDecls [ 0 ] ) ;
40
- }
41
- else {
42
- // Note: Delete the surrounding trivia because it will have been retained in newImportDecls.
43
- changeTracker . replaceNodeWithNodes ( sourceFile , oldImportDecls [ 0 ] , newImportDecls , {
44
- useNonAdjustedStartPosition : false ,
45
- useNonAdjustedEndPosition : false ,
46
- suffix : getNewLineOrDefaultFromHost ( host , formatContext . options ) ,
47
- } ) ;
48
- }
37
+ // Delete or replace the first import.
38
+ if ( newImportDecls . length === 0 ) {
39
+ changeTracker . deleteNode ( sourceFile , oldImportDecls [ 0 ] ) ;
40
+ }
41
+ else {
42
+ // Note: Delete the surrounding trivia because it will have been retained in newImportDecls.
43
+ changeTracker . replaceNodeWithNodes ( sourceFile , oldImportDecls [ 0 ] , newImportDecls , {
44
+ useNonAdjustedStartPosition : false ,
45
+ useNonAdjustedEndPosition : false ,
46
+ suffix : getNewLineOrDefaultFromHost ( host , formatContext . options ) ,
47
+ } ) ;
48
+ }
49
49
50
- // Delete any subsequent imports.
51
- for ( let i = 1 ; i < oldImportDecls . length ; i ++ ) {
52
- changeTracker . deleteNode ( sourceFile , oldImportDecls [ i ] ) ;
50
+ // Delete any subsequent imports.
51
+ for ( let i = 1 ; i < oldImportDecls . length ; i ++ ) {
52
+ changeTracker . deleteNode ( sourceFile , oldImportDecls [ i ] ) ;
53
+ }
53
54
}
54
-
55
- return changeTracker . getChanges ( ) ;
56
55
}
57
56
58
57
function removeUnusedImports ( oldImports : ReadonlyArray < ImportDeclaration > , sourceFile : SourceFile , program : Program ) {
0 commit comments