Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/harness/unittests/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ F2();
{
path: "/test.ts",
content: `
/** Comment */
import { F1, F2 } from "lib";
import * as NS from "lib";
import D from "lib";
Expand Down
19 changes: 14 additions & 5 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,35 @@ namespace ts.OrganizeImports {
return;
}

const deletedImportDeclarations = createMap<true>();

const oldImportGroups = group(oldImportDecls, importDecl => getExternalModuleName(importDecl.moduleSpecifier));
const sortedImportGroups = stableSort(oldImportGroups, (group1, group2) => compareModuleSpecifiers(group1[0].moduleSpecifier, group2[0].moduleSpecifier));
const newImportDecls = flatMap(sortedImportGroups, importGroup =>
getExternalModuleName(importGroup[0].moduleSpecifier)
? coalesceImports(removeUnusedImports(importGroup, sourceFile, program))
? coalesceImports(removeUnusedImports(importGroup, sourceFile, program, deletedImportDeclarations))
: importGroup);

// Delete or replace the first import.
if (newImportDecls.length === 0) {
changeTracker.deleteNode(sourceFile, oldImportDecls[0]);
removeOldImport(newImportDecls[0]);
}
else {
// Note: Delete the surrounding trivia because it will have been retained in newImportDecls.
changeTracker.replaceNodeWithNodes(sourceFile, oldImportDecls[0], newImportDecls, {
useNonAdjustedStartPosition: false,
useNonAdjustedStartPosition: deletedImportDeclarations.has(String(getNodeId(oldImportDecls[0]))),
useNonAdjustedEndPosition: false,
suffix: getNewLineOrDefaultFromHost(host, formatContext.options),
});
}

// Delete any subsequent imports.
for (let i = 1; i < oldImportDecls.length; i++) {
changeTracker.deleteNode(sourceFile, oldImportDecls[i]);
removeOldImport(oldImportDecls[i]);
}

function removeOldImport(oldImport: ImportDeclaration): void {
changeTracker.deleteNode(sourceFile, oldImport, { useNonAdjustedStartPosition: deletedImportDeclarations.has(String(getNodeId(oldImport))) });
}
}
}
Expand All @@ -66,7 +72,7 @@ namespace ts.OrganizeImports {
return body && !isIdentifier(body) && (isModuleBlock(body) ? body : getModuleBlock(body));
}

function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program, deletedImportDeclarations: Map<true>) {
const typeChecker = program.getTypeChecker();
const jsxNamespace = typeChecker.getJsxNamespace();
const jsxContext = sourceFile.languageVariant === LanguageVariant.JSX && program.getCompilerOptions().jsx;
Expand Down Expand Up @@ -110,6 +116,9 @@ namespace ts.OrganizeImports {
if (name || namedBindings) {
usedImports.push(updateImportDeclarationAndClause(importDecl, name, namedBindings));
}
else {
deletedImportDeclarations.set(String(getNodeId(importDecl)), true);
}
}

return usedImports;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/organizeImports/Unused_Some.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ==ORIGINAL==

/** Comment */
import { F1, F2 } from "lib";
import * as NS from "lib";
import D from "lib";
Expand All @@ -8,6 +9,7 @@ D();

// ==ORGANIZED==

/** Comment */
import D from "lib";

D();