Skip to content

Commit 2b59c9a

Browse files
author
Andy
authored
Don't add import completion for re-export with different name (microsoft#23211)
1 parent 61d9fc6 commit 2b59c9a

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/services/codefixes/importFixes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ namespace ts.codefix {
9898
symbolToken: Node | undefined,
9999
preferences: UserPreferences,
100100
): { readonly moduleSpecifier: string, readonly codeAction: CodeAction } {
101-
const exportInfos = getAllReExportingModules(exportedSymbol, checker, allSourceFiles);
101+
const exportInfos = getAllReExportingModules(exportedSymbol, symbolName, checker, allSourceFiles);
102102
Debug.assert(exportInfos.some(info => info.moduleSymbol === moduleSymbol));
103103
// We sort the best codefixes first, so taking `first` is best for completions.
104104
const moduleSpecifier = first(getNewImportInfos(program, sourceFile, exportInfos, compilerOptions, getCanonicalFileName, host, preferences)).moduleSpecifier;
105105
const ctx: ImportCodeFixContext = { host, program, checker, compilerOptions, sourceFile, formatContext, symbolName, getCanonicalFileName, symbolToken, preferences };
106106
return { moduleSpecifier, codeAction: first(getCodeActionsForImport(exportInfos, ctx)) };
107107
}
108-
function getAllReExportingModules(exportedSymbol: Symbol, checker: TypeChecker, allSourceFiles: ReadonlyArray<SourceFile>): ReadonlyArray<SymbolExportInfo> {
108+
function getAllReExportingModules(exportedSymbol: Symbol, symbolName: string, checker: TypeChecker, allSourceFiles: ReadonlyArray<SourceFile>): ReadonlyArray<SymbolExportInfo> {
109109
const result: SymbolExportInfo[] = [];
110110
forEachExternalModule(checker, allSourceFiles, moduleSymbol => {
111111
for (const exported of checker.getExportsOfModule(moduleSymbol)) {
112-
if (skipAlias(exported, checker) === exportedSymbol) {
112+
if (exported.escapedName === InternalSymbolName.Default || exported.name === symbolName && skipAlias(exported, checker) === exportedSymbol) {
113113
const isDefaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol) === exported;
114114
result.push({ moduleSymbol, importKind: isDefaultExport ? ImportKind.Default : ImportKind.Named });
115115
}

tests/cases/fourslash/completionsImport_default_exportDefaultIdentifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
goTo.marker("");
1515
verify.completionListContains({ name: "foo", source: "/a" }, "(alias) const foo: 0\nexport default foo", "", "alias", /*spanIndex*/ undefined, /*hasAction*/ true, {
16-
includeExternalModuleExports: true,
16+
includeCompletionsForModuleExports: true,
1717
sourceDisplay: "./a",
1818
});
1919

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
5+
// @Filename: /a.ts
6+
////export const x = 0;
7+
8+
// @Filename: /index.ts
9+
////export { x as y } from "./a";
10+
11+
// @Filename: /c.ts
12+
/////**/
13+
14+
goTo.marker("");
15+
verify.completionListContains({ name: "x", source: "/a" }, "const x: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, {
16+
includeCompletionsForModuleExports: true,
17+
sourceDisplay: "./a",
18+
});
19+
20+
verify.applyCodeActionFromCompletion("", {
21+
name: "x",
22+
source: "/a",
23+
description: `Import 'x' from module "./a"`,
24+
newFileContent: `import { x } from "./a";
25+
26+
`,
27+
});

0 commit comments

Comments
 (0)