From 0464e91c8b67579a4ed840e5783575a493c958e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 18 Sep 2023 21:38:00 +0200 Subject: [PATCH] Fixed an issue with type-only import promoting (#55365) --- src/services/codefixes/importFixes.ts | 11 +++++----- .../fourslash/codeFixPromoteTypeOnly1.ts | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 tests/cases/fourslash/codeFixPromoteTypeOnly1.ts diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 60d8f5fe361..e1c97053de5 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -1363,15 +1363,16 @@ function promoteFromTypeOnly(changes: textChanges.ChangeTracker, aliasDeclaratio if (aliasDeclaration.isTypeOnly) { const sortKind = OrganizeImports.detectImportSpecifierSorting(aliasDeclaration.parent.elements, preferences); if (aliasDeclaration.parent.elements.length > 1 && sortKind) { - changes.delete(sourceFile, aliasDeclaration); const newSpecifier = factory.updateImportSpecifier(aliasDeclaration, /*isTypeOnly*/ false, aliasDeclaration.propertyName, aliasDeclaration.name); const comparer = OrganizeImports.getOrganizeImportsComparer(preferences, sortKind === SortKind.CaseInsensitive); const insertionIndex = OrganizeImports.getImportSpecifierInsertionIndex(aliasDeclaration.parent.elements, newSpecifier, comparer); - changes.insertImportSpecifierAtIndex(sourceFile, newSpecifier, aliasDeclaration.parent, insertionIndex); - } - else { - changes.deleteRange(sourceFile, aliasDeclaration.getFirstToken()!); + if (aliasDeclaration.parent.elements.indexOf(aliasDeclaration) !== insertionIndex) { + changes.delete(sourceFile, aliasDeclaration); + changes.insertImportSpecifierAtIndex(sourceFile, newSpecifier, aliasDeclaration.parent, insertionIndex); + return aliasDeclaration; + } } + changes.deleteRange(sourceFile, aliasDeclaration.getFirstToken()!); return aliasDeclaration; } else { diff --git a/tests/cases/fourslash/codeFixPromoteTypeOnly1.ts b/tests/cases/fourslash/codeFixPromoteTypeOnly1.ts new file mode 100644 index 00000000000..c18c7b2d0fd --- /dev/null +++ b/tests/cases/fourslash/codeFixPromoteTypeOnly1.ts @@ -0,0 +1,21 @@ +/// +// @module: es2015 + +// https://github.com/microsoft/TypeScript/issues/55363 + +// @Filename: index.ts +//// import { TwistyAlgEditor, type TwistyPlayer } from "./other-file"; +//// new TwistyPlayer(); + +// @Filename: other-file.ts +//// export class TwistyAlgEditor {} +//// export class TwistyPlayer {} + +verify.codeFix({ + index: 0, + description: [ts.Diagnostics.Remove_type_from_import_of_0_from_1.message, "TwistyPlayer", "./other-file"], + applyChanges: true, + newFileContent: +`import { TwistyAlgEditor, TwistyPlayer } from "./other-file"; +new TwistyPlayer();` +}) \ No newline at end of file