Skip to content

Commit 83d02a5

Browse files
authored
Fix auto imports for export default edge cases (microsoft#41068)
1 parent 4bf8ac2 commit 83d02a5

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,13 @@ namespace ts.codefix {
622622

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

628634
if (defaultExport.escapedName !== InternalSymbolName.Default &&
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
//// export default Math.foo;
5+
6+
// @Filename: /index.ts
7+
//// a/**/
8+
9+
verify.applyCodeActionFromCompletion("", {
10+
name: "a",
11+
source: "/a",
12+
description: `Import default 'a' from module "./a"`,
13+
newFileContent: `import a from "./a";\n\na`,
14+
preferences: {
15+
includeCompletionsForModuleExports: true
16+
}
17+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @lib: dom
4+
5+
// @Filename: foo.ts
6+
//// export default globalThis.localStorage;
7+
8+
// @Filename: index.ts
9+
//// foo/**/
10+
11+
goTo.marker("");
12+
verify.importFixAtPosition([`import foo from "./foo";
13+
14+
foo`]);

0 commit comments

Comments
 (0)