Skip to content

Commit 8f2ee38

Browse files
authored
fix(50888): Organize Imports may delete preceding comments when all members of all import statements are unused (#50983)
* fix(50888): preserve preceding comments of the deleted import * update tests
1 parent 6327931 commit 8f2ee38

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/services/organizeImports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export function organizeImports(
145145
// Consider the first node to have trailingTrivia as we want to exclude the
146146
// "header" comment.
147147
changeTracker.deleteNodes(sourceFile, oldImportDecls, {
148+
leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude,
148149
trailingTriviaOption: textChanges.TrailingTriviaOption.Include,
149150
}, /*hasTrailingComment*/ true);
150151
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @filename: /a.ts
4+
////export const foo = 1;
5+
6+
// @filename: /b.ts
7+
/////**
8+
//// * Module doc comment
9+
//// *
10+
//// * @module
11+
//// */
12+
////
13+
////// comment 1
14+
////
15+
////// comment 2
16+
////
17+
////import { foo } from "./a";
18+
////import { foo } from "./a";
19+
////import { foo } from "./a";
20+
21+
goTo.file("/b.ts");
22+
verify.organizeImports(
23+
`/**
24+
* Module doc comment
25+
*
26+
* @module
27+
*/
28+
29+
// comment 1
30+
31+
// comment 2
32+
33+
`);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @filename: /a.ts
4+
////export const foo = 1;
5+
6+
// @filename: /b.ts
7+
/////**
8+
//// * Module doc comment
9+
//// *
10+
//// * @module
11+
//// */
12+
////
13+
////// comment 1
14+
////
15+
////// comment 2
16+
////
17+
////import { foo } from "./a";
18+
19+
goTo.file("/b.ts");
20+
verify.organizeImports(
21+
`/**
22+
* Module doc comment
23+
*
24+
* @module
25+
*/
26+
27+
// comment 1
28+
29+
// comment 2
30+
31+
`);

0 commit comments

Comments
 (0)