Skip to content

Commit 189eb50

Browse files
committed
Factor worker method out of ts.OrganizeImports.organizeImports
1 parent 427e6ed commit 189eb50

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/services/organizeImports.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,42 @@ namespace ts.OrganizeImports {
1616
// TODO (https://github.com/Microsoft/TypeScript/issues/10020): sort *within* ambient modules (find using isAmbientModule)
1717

1818
// All of the old ImportDeclarations in the file, in syntactic order.
19-
const oldImportDecls = sourceFile.statements.filter(isImportDeclaration);
19+
const topLevelImportDecls = sourceFile.statements.filter(isImportDeclaration);
2020

21-
if (oldImportDecls.length === 0) {
22-
return [];
23-
}
24-
25-
const oldImportGroups = group(oldImportDecls, importDecl => getExternalModuleName(importDecl.moduleSpecifier));
26-
27-
const sortedImportGroups = stableSort(oldImportGroups, (group1, group2) =>
28-
compareModuleSpecifiers(group1[0].moduleSpecifier, group2[0].moduleSpecifier));
21+
const changeTracker = textChanges.ChangeTracker.fromContext({ host, formatContext });
22+
organizeImportsWorker(topLevelImportDecls);
23+
return changeTracker.getChanges();
2924

30-
const newImportDecls = flatMap(sortedImportGroups, importGroup =>
31-
getExternalModuleName(importGroup[0].moduleSpecifier)
32-
? coalesceImports(removeUnusedImports(importGroup, sourceFile, program))
33-
: importGroup);
25+
function organizeImportsWorker(oldImportDecls: ReadonlyArray<ImportDeclaration>) {
26+
if (length(oldImportDecls) === 0) {
27+
return;
28+
}
3429

35-
const changeTracker = textChanges.ChangeTracker.fromContext({ host, formatContext });
30+
const oldImportGroups = group(oldImportDecls, importDecl => getExternalModuleName(importDecl.moduleSpecifier));
31+
const sortedImportGroups = stableSort(oldImportGroups, (group1, group2) => compareModuleSpecifiers(group1[0].moduleSpecifier, group2[0].moduleSpecifier));
32+
const newImportDecls = flatMap(sortedImportGroups, importGroup =>
33+
getExternalModuleName(importGroup[0].moduleSpecifier)
34+
? coalesceImports(removeUnusedImports(importGroup, sourceFile, program))
35+
: importGroup);
3636

37-
// Delete or replace the first import.
38-
if (newImportDecls.length === 0) {
39-
changeTracker.deleteNode(sourceFile, oldImportDecls[0]);
40-
}
41-
else {
42-
// Note: Delete the surrounding trivia because it will have been retained in newImportDecls.
43-
changeTracker.replaceNodeWithNodes(sourceFile, oldImportDecls[0], newImportDecls, {
44-
useNonAdjustedStartPosition: false,
45-
useNonAdjustedEndPosition: false,
46-
suffix: getNewLineOrDefaultFromHost(host, formatContext.options),
47-
});
48-
}
37+
// Delete or replace the first import.
38+
if (newImportDecls.length === 0) {
39+
changeTracker.deleteNode(sourceFile, oldImportDecls[0]);
40+
}
41+
else {
42+
// Note: Delete the surrounding trivia because it will have been retained in newImportDecls.
43+
changeTracker.replaceNodeWithNodes(sourceFile, oldImportDecls[0], newImportDecls, {
44+
useNonAdjustedStartPosition: false,
45+
useNonAdjustedEndPosition: false,
46+
suffix: getNewLineOrDefaultFromHost(host, formatContext.options),
47+
});
48+
}
4949

50-
// Delete any subsequent imports.
51-
for (let i = 1; i < oldImportDecls.length; i++) {
52-
changeTracker.deleteNode(sourceFile, oldImportDecls[i]);
50+
// Delete any subsequent imports.
51+
for (let i = 1; i < oldImportDecls.length; i++) {
52+
changeTracker.deleteNode(sourceFile, oldImportDecls[i]);
53+
}
5354
}
54-
55-
return changeTracker.getChanges();
5655
}
5756

5857
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {

0 commit comments

Comments
 (0)