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
This commit is contained in:
Oleksandr T 2022-12-09 03:05:12 +02:00 committed by GitHub
parent 6327931d4f
commit 8f2ee38b52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 0 deletions

View File

@ -145,6 +145,7 @@ export function organizeImports(
// Consider the first node to have trailingTrivia as we want to exclude the
// "header" comment.
changeTracker.deleteNodes(sourceFile, oldImportDecls, {
leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude,
trailingTriviaOption: textChanges.TrailingTriviaOption.Include,
}, /*hasTrailingComment*/ true);
}

View File

@ -0,0 +1,33 @@
/// <reference path="fourslash.ts" />
// @filename: /a.ts
////export const foo = 1;
// @filename: /b.ts
/////**
//// * Module doc comment
//// *
//// * @module
//// */
////
////// comment 1
////
////// comment 2
////
////import { foo } from "./a";
////import { foo } from "./a";
////import { foo } from "./a";
goTo.file("/b.ts");
verify.organizeImports(
`/**
* Module doc comment
*
* @module
*/
// comment 1
// comment 2
`);

View File

@ -0,0 +1,31 @@
/// <reference path="fourslash.ts" />
// @filename: /a.ts
////export const foo = 1;
// @filename: /b.ts
/////**
//// * Module doc comment
//// *
//// * @module
//// */
////
////// comment 1
////
////// comment 2
////
////import { foo } from "./a";
goTo.file("/b.ts");
verify.organizeImports(
`/**
* Module doc comment
*
* @module
*/
// comment 1
// comment 2
`);