Fix auto imports for export default edge cases (#41068)

This commit is contained in:
Andrew Branch 2020-10-12 15:42:58 -07:00 committed by GitHub
parent 4bf8ac25be
commit 83d02a5f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 1 deletions

View File

@ -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 &&

View File

@ -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
}
});

View File

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