Cherry-pick PR #38599 into release-3.9

Component commits:
428f5a19d6 delete import comments on organize imports

8003791d9f add unit test

26eaf706ab accept new baseline

81d1732fd8 respond to review comment
This commit is contained in:
Jesse Trinity
2020-05-19 20:48:08 +00:00
committed by typescript-bot
parent 8037e26dd4
commit fefdd74c2c
4 changed files with 38 additions and 1 deletions

View File

@@ -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]);
}
}
}

View File

@@ -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) });
}

View File

@@ -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",

View File

@@ -0,0 +1,17 @@
// ==ORIGINAL==
// Header
import "lib3";
// Comment2
import "lib2";
// Comment1
import "lib1";
// ==ORGANIZED==
// Header
// Comment1
import "lib1";
// Comment2
import "lib2";
import "lib3";