Organize imports within ambient module declarations

This commit is contained in:
Andrew Casey 2018-02-20 17:41:44 -08:00
parent 189eb505b9
commit 8ead7ab29c
4 changed files with 97 additions and 3 deletions

View File

@ -325,6 +325,43 @@ F2();
/*A*/import /*B*/ { /*C*/ F1 /*D*/, /*E*/ F2 /*F*/ } /*G*/ from /*H*/ "lib" /*I*/;/*J*/ //K
F1();
`,
},
libFile);
testOrganizeImports("AmbientModule",
{
path: "/test.ts",
content: `
declare module "mod" {
import { F1 } from "lib";
import * as NS from "lib";
import { F2 } from "lib";
function F(f1: {} = F1, f2: {} = F2) {}
}
`,
},
libFile);
testOrganizeImports("TopLevelAndAmbientModule",
{
path: "/test.ts",
content: `
import D from "lib";
declare module "mod" {
import { F1 } from "lib";
import * as NS from "lib";
import { F2 } from "lib";
function F(f1: {} = F1, f2: {} = F2) {}
}
import E from "lib";
import "lib";
D();
`,
},
libFile);

View File

@ -13,13 +13,18 @@ namespace ts.OrganizeImports {
host: LanguageServiceHost,
program: Program) {
// TODO (https://github.com/Microsoft/TypeScript/issues/10020): sort *within* ambient modules (find using isAmbientModule)
const changeTracker = textChanges.ChangeTracker.fromContext({ host, formatContext });
// All of the old ImportDeclarations in the file, in syntactic order.
const topLevelImportDecls = sourceFile.statements.filter(isImportDeclaration);
const changeTracker = textChanges.ChangeTracker.fromContext({ host, formatContext });
organizeImportsWorker(topLevelImportDecls);
for (const ambientModule of sourceFile.statements.filter(isAmbientModule)) {
const ambientModuleBody = getModuleBlock(ambientModule as ModuleDeclaration);
const ambientModuleImportDecls = ambientModuleBody.statements.filter(isImportDeclaration);
organizeImportsWorker(ambientModuleImportDecls);
}
return changeTracker.getChanges();
function organizeImportsWorker(oldImportDecls: ReadonlyArray<ImportDeclaration>) {
@ -54,6 +59,11 @@ namespace ts.OrganizeImports {
}
}
function getModuleBlock(moduleDecl: ModuleDeclaration): ModuleBlock | undefined {
const body = moduleDecl.body;
return body && !isIdentifier(body) && (isModuleBlock(body) ? body : getModuleBlock(body));
}
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {
const typeChecker = program.getTypeChecker();
const jsxNamespace = typeChecker.getJsxNamespace();

View File

@ -0,0 +1,17 @@
// ==ORIGINAL==
declare module "mod" {
import { F1 } from "lib";
import * as NS from "lib";
import { F2 } from "lib";
function F(f1: {} = F1, f2: {} = F2) {}
}
// ==ORGANIZED==
declare module "mod" {
import { F1, F2 } from "lib";
function F(f1: {} = F1, f2: {} = F2) {}
}

View File

@ -0,0 +1,30 @@
// ==ORIGINAL==
import D from "lib";
declare module "mod" {
import { F1 } from "lib";
import * as NS from "lib";
import { F2 } from "lib";
function F(f1: {} = F1, f2: {} = F2) {}
}
import E from "lib";
import "lib";
D();
// ==ORGANIZED==
import "lib";
import D from "lib";
declare module "mod" {
import { F1, F2 } from "lib";
function F(f1: {} = F1, f2: {} = F2) {}
}
D();