Skip to content

Commit

Permalink
Merge pull request #38668 from typescript-bot/pick/38599/release-3.9
Browse files Browse the repository at this point in the history
🤖 Pick PR #38599 (Fix regression organize imports dup...) into release-3.9
  • Loading branch information
DanielRosenwasser authored May 19, 2020
2 parents 5b29b14 + fefdd74 commit f713571
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace ts.OrganizeImports {

// Delete any subsequent imports.
for (let i = 1; i < oldImportDecls.length; i++) {
changeTracker.delete(sourceFile, oldImportDecls[i]);
changeTracker.deleteNode(sourceFile, oldImportDecls[i]);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ namespace ts.textChanges {
this.deletedNodes.push({ sourceFile, node });
}

public deleteNode(sourceFile: SourceFile, node: Node, options: ConfigurableStartEnd = { leadingTriviaOption: LeadingTriviaOption.IncludeAll }): void {
this.deleteRange(sourceFile, getAdjustedRange(sourceFile, node, node, options));
}

public deleteModifier(sourceFile: SourceFile, modifier: Modifier): void {
this.deleteRange(sourceFile, { pos: modifier.getStart(sourceFile), end: skipTrivia(sourceFile.text, modifier.end, /*stopAfterLineBreak*/ true) });
}
Expand Down
16 changes: 16 additions & 0 deletions src/testRunner/unittests/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,22 @@ import "lib1";
{ path: "/lib1.ts", content: "" },
{ path: "/lib2.ts", content: "" });

testOrganizeImports("SortComments",
{
path: "/test.ts",
content: `
// Header
import "lib3";
// Comment2
import "lib2";
// Comment1
import "lib1";
`,
},
{ path: "/lib1.ts", content: "" },
{ path: "/lib2.ts", content: "" },
{ path: "/lib3.ts", content: "" });

testOrganizeImports("AmbientModule",
{
path: "/test.ts",
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/organizeImports/SortComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ==ORIGINAL==

// Header
import "lib3";
// Comment2
import "lib2";
// Comment1
import "lib1";

// ==ORGANIZED==

// Header
// Comment1
import "lib1";
// Comment2
import "lib2";
import "lib3";

0 comments on commit f713571

Please sign in to comment.