Skip to content

Commit 5c55fc0

Browse files
authored
fix(40817): suggest import for default exported alias (#40845)
1 parent a21003d commit 5c55fc0

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ namespace ts.codefix {
239239
}
240240

241241
const defaultInfo = getDefaultLikeExportInfo(importingFile, moduleSymbol, checker, compilerOptions);
242-
if (defaultInfo && defaultInfo.name === symbolName && skipAlias(defaultInfo.symbol, checker) === exportedSymbol) {
242+
if (defaultInfo && (defaultInfo.name === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target) === symbolName) && skipAlias(defaultInfo.symbol, checker) === exportedSymbol) {
243243
result.push({ moduleSymbol, importKind: defaultInfo.kind, exportedSymbolIsTypeOnly: isTypeOnlySymbol(defaultInfo.symbol, checker) });
244244
}
245245

@@ -556,8 +556,9 @@ namespace ts.codefix {
556556
const checker = program.getTypeChecker();
557557
cancellationToken.throwIfCancellationRequested();
558558

559-
const defaultInfo = getDefaultLikeExportInfo(sourceFile, moduleSymbol, checker, program.getCompilerOptions());
560-
if (defaultInfo && defaultInfo.name === symbolName && symbolHasMeaning(defaultInfo.symbolForMeaning, currentTokenMeaning)) {
559+
const compilerOptions = program.getCompilerOptions();
560+
const defaultInfo = getDefaultLikeExportInfo(sourceFile, moduleSymbol, checker, compilerOptions);
561+
if (defaultInfo && (defaultInfo.name === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target) === symbolName) && symbolHasMeaning(defaultInfo.symbolForMeaning, currentTokenMeaning)) {
561562
addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind, checker);
562563
}
563564

@@ -628,7 +629,7 @@ namespace ts.codefix {
628629
defaultExport.escapedName !== InternalSymbolName.ExportEquals) {
629630
return { symbolForMeaning: defaultExport, name: defaultExport.getName() };
630631
}
631-
return { symbolForMeaning: defaultExport, name: moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target!) };
632+
return { symbolForMeaning: defaultExport, name: moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target) };
632633
}
633634

634635
function getNameForExportDefault(symbol: Symbol): string | undefined {
@@ -937,11 +938,11 @@ namespace ts.codefix {
937938
|| (!!globalCachePath && startsWith(getCanonicalFileName(globalCachePath), toNodeModulesParent));
938939
}
939940

940-
export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget): string {
941+
export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget | undefined): string {
941942
return moduleSpecifierToValidIdentifier(removeFileExtension(stripQuotes(moduleSymbol.name)), target);
942943
}
943944

944-
export function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target: ScriptTarget): string {
945+
export function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target: ScriptTarget | undefined): string {
945946
const baseName = getBaseFileName(removeSuffix(moduleSpecifier, "/index"));
946947
let res = "";
947948
let lastCharWasValid = true;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /foo.ts
4+
////const a = () => {};
5+
////export default a;
6+
7+
// @Filename: /test.ts
8+
////[|foo|];
9+
10+
goTo.file("/test.ts");
11+
verify.importFixAtPosition([`import foo from "./foo";
12+
13+
foo`]);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
5+
// @Filename: /node_modules/hooks/useFoo.ts
6+
////declare const _default: () => void;
7+
////export default _default;
8+
9+
// @Filename: /test.ts
10+
////[|useFoo|];
11+
12+
goTo.file("/test.ts");
13+
verify.importFixAtPosition([`import useFoo from "hooks/useFoo";
14+
15+
useFoo`]);

0 commit comments

Comments
 (0)