diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 4600dfcd9b0c6..c2ae4083446d3 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -239,7 +239,7 @@ namespace ts.codefix { } const defaultInfo = getDefaultLikeExportInfo(importingFile, moduleSymbol, checker, compilerOptions); - if (defaultInfo && defaultInfo.name === symbolName && skipAlias(defaultInfo.symbol, checker) === exportedSymbol) { + if (defaultInfo && (defaultInfo.name === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target) === symbolName) && skipAlias(defaultInfo.symbol, checker) === exportedSymbol) { result.push({ moduleSymbol, importKind: defaultInfo.kind, exportedSymbolIsTypeOnly: isTypeOnlySymbol(defaultInfo.symbol, checker) }); } @@ -535,8 +535,9 @@ namespace ts.codefix { const checker = program.getTypeChecker(); cancellationToken.throwIfCancellationRequested(); - const defaultInfo = getDefaultLikeExportInfo(sourceFile, moduleSymbol, checker, program.getCompilerOptions()); - if (defaultInfo && defaultInfo.name === symbolName && symbolHasMeaning(defaultInfo.symbolForMeaning, currentTokenMeaning)) { + const compilerOptions = program.getCompilerOptions(); + const defaultInfo = getDefaultLikeExportInfo(sourceFile, moduleSymbol, checker, compilerOptions); + if (defaultInfo && (defaultInfo.name === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target) === symbolName) && symbolHasMeaning(defaultInfo.symbolForMeaning, currentTokenMeaning)) { addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind, checker); } @@ -607,7 +608,7 @@ namespace ts.codefix { defaultExport.escapedName !== InternalSymbolName.ExportEquals) { return { symbolForMeaning: defaultExport, name: defaultExport.getName() }; } - return { symbolForMeaning: defaultExport, name: moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target!) }; + return { symbolForMeaning: defaultExport, name: moduleSymbolToValidIdentifier(moduleSymbol, compilerOptions.target) }; } function getNameForExportDefault(symbol: Symbol): string | undefined { @@ -916,11 +917,11 @@ namespace ts.codefix { || (!!globalCachePath && startsWith(getCanonicalFileName(globalCachePath), toNodeModulesParent)); } - export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget): string { + export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget | undefined): string { return moduleSpecifierToValidIdentifier(removeFileExtension(stripQuotes(moduleSymbol.name)), target); } - export function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target: ScriptTarget): string { + export function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target: ScriptTarget | undefined): string { const baseName = getBaseFileName(removeSuffix(moduleSpecifier, "/index")); let res = ""; let lastCharWasValid = true; diff --git a/tests/cases/fourslash/importNameCodeFixDefaultExport4.ts b/tests/cases/fourslash/importNameCodeFixDefaultExport4.ts new file mode 100644 index 0000000000000..c2460f802176d --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixDefaultExport4.ts @@ -0,0 +1,13 @@ +/// + +// @Filename: /foo.ts +////const a = () => {}; +////export default a; + +// @Filename: /test.ts +////[|foo|]; + +goTo.file("/test.ts"); +verify.importFixAtPosition([`import foo from "./foo"; + +foo`]); diff --git a/tests/cases/fourslash/importNameCodeFixDefaultExport5.ts b/tests/cases/fourslash/importNameCodeFixDefaultExport5.ts new file mode 100644 index 0000000000000..d1fc610ce91db --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixDefaultExport5.ts @@ -0,0 +1,15 @@ +/// + +// @moduleResolution: node + +// @Filename: /node_modules/hooks/useFoo.ts +////declare const _default: () => void; +////export default _default; + +// @Filename: /test.ts +////[|useFoo|]; + +goTo.file("/test.ts"); +verify.importFixAtPosition([`import useFoo from "hooks/useFoo"; + +useFoo`]);