Skip to content

Commit 60db86b

Browse files
Andaristsnovader
authored andcommitted
Fixed an issue with type-only import promoting (microsoft#55365)
1 parent a4c53f6 commit 60db86b

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/services/codefixes/importFixes.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1363,15 +1363,16 @@ function promoteFromTypeOnly(changes: textChanges.ChangeTracker, aliasDeclaratio
13631363
if (aliasDeclaration.isTypeOnly) {
13641364
const sortKind = OrganizeImports.detectImportSpecifierSorting(aliasDeclaration.parent.elements, preferences);
13651365
if (aliasDeclaration.parent.elements.length > 1 && sortKind) {
1366-
changes.delete(sourceFile, aliasDeclaration);
13671366
const newSpecifier = factory.updateImportSpecifier(aliasDeclaration, /*isTypeOnly*/ false, aliasDeclaration.propertyName, aliasDeclaration.name);
13681367
const comparer = OrganizeImports.getOrganizeImportsComparer(preferences, sortKind === SortKind.CaseInsensitive);
13691368
const insertionIndex = OrganizeImports.getImportSpecifierInsertionIndex(aliasDeclaration.parent.elements, newSpecifier, comparer);
1370-
changes.insertImportSpecifierAtIndex(sourceFile, newSpecifier, aliasDeclaration.parent, insertionIndex);
1371-
}
1372-
else {
1373-
changes.deleteRange(sourceFile, aliasDeclaration.getFirstToken()!);
1369+
if (aliasDeclaration.parent.elements.indexOf(aliasDeclaration) !== insertionIndex) {
1370+
changes.delete(sourceFile, aliasDeclaration);
1371+
changes.insertImportSpecifierAtIndex(sourceFile, newSpecifier, aliasDeclaration.parent, insertionIndex);
1372+
return aliasDeclaration;
1373+
}
13741374
}
1375+
changes.deleteRange(sourceFile, aliasDeclaration.getFirstToken()!);
13751376
return aliasDeclaration;
13761377
}
13771378
else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
// @module: es2015
3+
4+
// https://github.com/microsoft/TypeScript/issues/55363
5+
6+
// @Filename: index.ts
7+
//// import { TwistyAlgEditor, type TwistyPlayer } from "./other-file";
8+
//// new TwistyPlayer();
9+
10+
// @Filename: other-file.ts
11+
//// export class TwistyAlgEditor {}
12+
//// export class TwistyPlayer {}
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: [ts.Diagnostics.Remove_type_from_import_of_0_from_1.message, "TwistyPlayer", "./other-file"],
17+
applyChanges: true,
18+
newFileContent:
19+
`import { TwistyAlgEditor, TwistyPlayer } from "./other-file";
20+
new TwistyPlayer();`
21+
})

0 commit comments

Comments
 (0)