fix(48544): allow to convert default exports to names for import type nodes (#48550)

This commit is contained in:
Oleksandr T 2022-04-06 02:53:01 +03:00 committed by GitHub
parent 50a5bc839a
commit 9881c993fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -1193,10 +1193,15 @@ namespace ts.FindAllReferences {
cb: (ref: Identifier) => void,
): void {
const importTracker = createImportTracker(sourceFiles, new Set(sourceFiles.map(f => f.fileName)), checker, cancellationToken);
const { importSearches, indirectUsers } = importTracker(exportSymbol, { exportKind: isDefaultExport ? ExportKind.Default : ExportKind.Named, exportingModuleSymbol }, /*isForRename*/ false);
const { importSearches, indirectUsers, singleReferences } = importTracker(exportSymbol, { exportKind: isDefaultExport ? ExportKind.Default : ExportKind.Named, exportingModuleSymbol }, /*isForRename*/ false);
for (const [importLocation] of importSearches) {
cb(importLocation);
}
for (const singleReference of singleReferences) {
if (isIdentifier(singleReference) && isImportTypeNode(singleReference.parent)) {
cb(singleReference);
}
}
for (const indirectUser of indirectUsers) {
for (const node of getPossibleSymbolReferenceNodes(indirectUser, isDefaultExport ? "default" : exportName)) {
// Import specifiers should be handled by importSearches

View File

@ -212,6 +212,10 @@ namespace ts.refactor {
}
break;
}
case SyntaxKind.ImportType:
const importTypeNode = parent as ImportTypeNode;
changes.replaceNode(importingSourceFile, parent, factory.createImportTypeNode(importTypeNode.argument, factory.createIdentifier(exportName), importTypeNode.typeArguments, importTypeNode.isTypeOf));
break;
default:
Debug.failBadSyntaxKind(parent);
}

View File

@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />
// @Filename: /a.ts
/////*a*/export default class A {}/*b*/
// @Filename: /b.ts
////export type A = typeof import("./a").default;
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Convert export",
actionName: "Convert default export to named export",
actionDescription: "Convert default export to named export",
newContent: {
"/a.ts": "export class A {}",
"/b.ts": "export type A = typeof import(\"./a\").A;"
},
});