@@ -13,13 +13,18 @@ namespace ts.OrganizeImports {
1313 host : LanguageServiceHost ,
1414 program : Program ) {
1515
16- // TODO (https://github.com/Microsoft/TypeScript/issues/10020): sort *within* ambient modules (find using isAmbientModule)
16+ const changeTracker = textChanges . ChangeTracker . fromContext ( { host , formatContext } ) ;
1717
1818 // All of the old ImportDeclarations in the file, in syntactic order.
1919 const topLevelImportDecls = sourceFile . statements . filter ( isImportDeclaration ) ;
20-
21- const changeTracker = textChanges . ChangeTracker . fromContext ( { host, formatContext } ) ;
2220 organizeImportsWorker ( topLevelImportDecls ) ;
21+
22+ for ( const ambientModule of sourceFile . statements . filter ( isAmbientModule ) ) {
23+ const ambientModuleBody = getModuleBlock ( ambientModule as ModuleDeclaration ) ;
24+ const ambientModuleImportDecls = ambientModuleBody . statements . filter ( isImportDeclaration ) ;
25+ organizeImportsWorker ( ambientModuleImportDecls ) ;
26+ }
27+
2328 return changeTracker . getChanges ( ) ;
2429
2530 function organizeImportsWorker ( oldImportDecls : ReadonlyArray < ImportDeclaration > ) {
@@ -54,6 +59,11 @@ namespace ts.OrganizeImports {
5459 }
5560 }
5661
62+ function getModuleBlock ( moduleDecl : ModuleDeclaration ) : ModuleBlock | undefined {
63+ const body = moduleDecl . body ;
64+ return body && ! isIdentifier ( body ) && ( isModuleBlock ( body ) ? body : getModuleBlock ( body ) ) ;
65+ }
66+
5767 function removeUnusedImports ( oldImports : ReadonlyArray < ImportDeclaration > , sourceFile : SourceFile , program : Program ) {
5868 const typeChecker = program . getTypeChecker ( ) ;
5969 const jsxNamespace = typeChecker . getJsxNamespace ( ) ;
0 commit comments