Skip to content

Fix auto imports for export default edge cases #41068

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

Merged
merged 1 commit into from
Oct 12, 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
8 changes: 7 additions & 1 deletion src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,13 @@ namespace ts.codefix {

if (defaultExport.flags & SymbolFlags.Alias) {
const aliased = checker.getImmediateAliasedSymbol(defaultExport);
return aliased && getDefaultExportInfoWorker(aliased, Debug.checkDefined(aliased.parent, "Alias targets of default exports must have a parent"), checker, compilerOptions);
if (aliased && aliased.parent) {
// - `aliased` will be undefined if the module is exporting an unresolvable name,
// but we can still offer completions for it.
// - `aliased.parent` will be undefined if the module is exporting `globalThis.something`,
// or another expression that resolves to a global.
return getDefaultExportInfoWorker(aliased, aliased.parent, checker, compilerOptions);
}
}

if (defaultExport.escapedName !== InternalSymbolName.Default &&
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/importNameCodeFixDefaultExport6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.ts
//// export default Math.foo;

// @Filename: /index.ts
//// a/**/

verify.applyCodeActionFromCompletion("", {
name: "a",
source: "/a",
description: `Import default 'a' from module "./a"`,
newFileContent: `import a from "./a";\n\na`,
preferences: {
includeCompletionsForModuleExports: true
}
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/importNameCodeFixDefaultExport7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

// @lib: dom

// @Filename: foo.ts
//// export default globalThis.localStorage;

// @Filename: index.ts
//// foo/**/

goTo.marker("");
verify.importFixAtPosition([`import foo from "./foo";

foo`]);