Skip to content

Commit

Permalink
fix(40817): suggest import for default exported alias (#40845)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored Oct 6, 2020
1 parent a21003d commit 5c55fc0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
}

Expand Down Expand Up @@ -556,8 +556,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);
}

Expand Down Expand Up @@ -628,7 +629,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 {
Expand Down Expand Up @@ -937,11 +938,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;
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/fourslash/importNameCodeFixDefaultExport4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

// @Filename: /foo.ts
////const a = () => {};
////export default a;

// @Filename: /test.ts
////[|foo|];

goTo.file("/test.ts");
verify.importFixAtPosition([`import foo from "./foo";
foo`]);
15 changes: 15 additions & 0 deletions tests/cases/fourslash/importNameCodeFixDefaultExport5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

// @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`]);

0 comments on commit 5c55fc0

Please sign in to comment.