Skip to content

Commit 8ead7ab

Browse files
committed
Organize imports within ambient module declarations
1 parent 189eb50 commit 8ead7ab

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

src/harness/unittests/organizeImports.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,43 @@ F2();
325325
/*A*/import /*B*/ { /*C*/ F1 /*D*/, /*E*/ F2 /*F*/ } /*G*/ from /*H*/ "lib" /*I*/;/*J*/ //K
326326
327327
F1();
328+
`,
329+
},
330+
libFile);
331+
332+
testOrganizeImports("AmbientModule",
333+
{
334+
path: "/test.ts",
335+
content: `
336+
declare module "mod" {
337+
import { F1 } from "lib";
338+
import * as NS from "lib";
339+
import { F2 } from "lib";
340+
341+
function F(f1: {} = F1, f2: {} = F2) {}
342+
}
343+
`,
344+
},
345+
libFile);
346+
347+
testOrganizeImports("TopLevelAndAmbientModule",
348+
{
349+
path: "/test.ts",
350+
content: `
351+
import D from "lib";
352+
353+
declare module "mod" {
354+
import { F1 } from "lib";
355+
import * as NS from "lib";
356+
import { F2 } from "lib";
357+
358+
function F(f1: {} = F1, f2: {} = F2) {}
359+
}
360+
361+
import E from "lib";
362+
import "lib";
363+
364+
D();
328365
`,
329366
},
330367
libFile);

src/services/organizeImports.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ==ORIGINAL==
2+
3+
declare module "mod" {
4+
import { F1 } from "lib";
5+
import * as NS from "lib";
6+
import { F2 } from "lib";
7+
8+
function F(f1: {} = F1, f2: {} = F2) {}
9+
}
10+
11+
// ==ORGANIZED==
12+
13+
declare module "mod" {
14+
import { F1, F2 } from "lib";
15+
16+
function F(f1: {} = F1, f2: {} = F2) {}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ==ORIGINAL==
2+
3+
import D from "lib";
4+
5+
declare module "mod" {
6+
import { F1 } from "lib";
7+
import * as NS from "lib";
8+
import { F2 } from "lib";
9+
10+
function F(f1: {} = F1, f2: {} = F2) {}
11+
}
12+
13+
import E from "lib";
14+
import "lib";
15+
16+
D();
17+
18+
// ==ORGANIZED==
19+
20+
import "lib";
21+
import D from "lib";
22+
23+
declare module "mod" {
24+
import { F1, F2 } from "lib";
25+
26+
function F(f1: {} = F1, f2: {} = F2) {}
27+
}
28+
29+
30+
D();

0 commit comments

Comments
 (0)