Fixed an issue with type-only import promoting (#55365)

This commit is contained in:
Mateusz Burzyński
2023-09-18 21:38:00 +02:00
committed by GitHub
parent 70b7de11fb
commit 0464e91c8b
2 changed files with 27 additions and 5 deletions

View File

@@ -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 {

View File

@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />
// @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();`
})