mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-13 06:20:23 -06:00
Organize imports within ambient module declarations
This commit is contained in:
parent
189eb505b9
commit
8ead7ab29c
@ -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);
|
||||
|
||||
@ -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();
|
||||
|
||||
17
tests/baselines/reference/organizeImports/AmbientModule.ts
Normal file
17
tests/baselines/reference/organizeImports/AmbientModule.ts
Normal 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) {}
|
||||
}
|
||||
@ -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();
|
||||
Loading…
x
Reference in New Issue
Block a user