From 8f2ee38b52e67bb1e08c9e091413541c16208058 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Fri, 9 Dec 2022 03:05:12 +0200 Subject: [PATCH] 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 --- src/services/organizeImports.ts | 1 + tests/cases/fourslash/organizeImports14.ts | 33 ++++++++++++++++++++++ tests/cases/fourslash/organizeImports15.ts | 31 ++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 tests/cases/fourslash/organizeImports14.ts create mode 100644 tests/cases/fourslash/organizeImports15.ts diff --git a/src/services/organizeImports.ts b/src/services/organizeImports.ts index 9a85c7acd54..103b156c9c4 100644 --- a/src/services/organizeImports.ts +++ b/src/services/organizeImports.ts @@ -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); } diff --git a/tests/cases/fourslash/organizeImports14.ts b/tests/cases/fourslash/organizeImports14.ts new file mode 100644 index 00000000000..cd19c162100 --- /dev/null +++ b/tests/cases/fourslash/organizeImports14.ts @@ -0,0 +1,33 @@ +/// + +// @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 + +`); diff --git a/tests/cases/fourslash/organizeImports15.ts b/tests/cases/fourslash/organizeImports15.ts new file mode 100644 index 00000000000..afdb1bbcba1 --- /dev/null +++ b/tests/cases/fourslash/organizeImports15.ts @@ -0,0 +1,31 @@ +/// + +// @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 + +`);