Fix regression organize imports duplicates comments (#38599)

* delete import comments on organize imports

* add unit test

* accept new baseline

* respond to review comment
This commit is contained in:
Jesse Trinity 2020-05-15 14:25:11 -07:00 committed by GitHub
parent 1cbe7ef000
commit 1a15717bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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";