diff --git a/src/services/organizeImports.ts b/src/services/organizeImports.ts index 5bfa0c74ab1..b6be92fbf9c 100644 --- a/src/services/organizeImports.ts +++ b/src/services/organizeImports.ts @@ -89,7 +89,7 @@ namespace ts.OrganizeImports { const usedImports: ImportDeclaration[] = []; for (const importDecl of oldImports) { - const {importClause} = importDecl; + const { importClause, moduleSpecifier } = importDecl; if (!importClause) { // Imports without import clauses are assumed to be included for their side effects and are not removed. @@ -125,6 +125,14 @@ namespace ts.OrganizeImports { if (name || namedBindings) { usedImports.push(updateImportDeclarationAndClause(importDecl, name, namedBindings)); } + // If a module is imported to be augmented, keep the import declaration, but without an import clause + else if (hasModuleDeclarationMatchingSpecifier(sourceFile, moduleSpecifier)) { + usedImports.push(createImportDeclaration( + importDecl.decorators, + importDecl.modifiers, + /*importClause*/ undefined, + moduleSpecifier)); + } } return usedImports; @@ -135,6 +143,14 @@ namespace ts.OrganizeImports { } } + function hasModuleDeclarationMatchingSpecifier(sourceFile: SourceFile, moduleSpecifier: Expression) { + const moduleSpecifierText = isStringLiteral(moduleSpecifier) && moduleSpecifier.text; + return isString(moduleSpecifierText) && some(sourceFile.statements, statement => + isModuleDeclaration(statement) + && isStringLiteral(statement.name) + && statement.name.text === moduleSpecifierText); + } + function getExternalModuleName(specifier: Expression) { return specifier !== undefined && isStringLiteralLike(specifier) ? specifier.text diff --git a/src/testRunner/unittests/services/organizeImports.ts b/src/testRunner/unittests/services/organizeImports.ts index 9216a7adf88..8003e1867cd 100644 --- a/src/testRunner/unittests/services/organizeImports.ts +++ b/src/testRunner/unittests/services/organizeImports.ts @@ -1,5 +1,5 @@ namespace ts { - describe("unittests:: services:: Organize imports", () => { + describe("unittests:: services:: organizeImports", () => { describe("Sort imports", () => { it("Sort - non-relative vs non-relative", () => { assertSortsBefore( @@ -347,8 +347,10 @@ import { } from "lib"; { path: "/test.d.ts", content: ` +import foo from 'foo'; import { Caseless } from 'caseless'; +declare module 'foo' {} declare module 'caseless' { interface Caseless { test(name: KeyType): boolean; diff --git a/tests/baselines/reference/organizeImports/Unused_false_positive_module_augmentation.ts b/tests/baselines/reference/organizeImports/Unused_false_positive_module_augmentation.ts new file mode 100644 index 00000000000..4eaf184d493 --- /dev/null +++ b/tests/baselines/reference/organizeImports/Unused_false_positive_module_augmentation.ts @@ -0,0 +1,22 @@ +// ==ORIGINAL== + +import foo from 'foo'; +import { Caseless } from 'caseless'; + +declare module 'foo' {} +declare module 'caseless' { + interface Caseless { + test(name: KeyType): boolean; + } +} +// ==ORGANIZED== + +import 'caseless'; +import 'foo'; + +declare module 'foo' {} +declare module 'caseless' { + interface Caseless { + test(name: KeyType): boolean; + } +} \ No newline at end of file