Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(40817): Dts compiled from anonymous arrow functions cannot be automatically imported #40845

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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)) {
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind, checker);
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
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`]);