Don't add import completion for re-export with different name (#23211)

This commit is contained in:
Andy
2018-04-06 10:05:38 -07:00
committed by GitHub
parent 61d9fc65ed
commit 2b59c9a0d7
3 changed files with 31 additions and 4 deletions
+3 -3
View File
@@ -98,18 +98,18 @@ namespace ts.codefix {
symbolToken: Node | undefined,
preferences: UserPreferences,
): { readonly moduleSpecifier: string, readonly codeAction: CodeAction } {
const exportInfos = getAllReExportingModules(exportedSymbol, checker, allSourceFiles);
const exportInfos = getAllReExportingModules(exportedSymbol, symbolName, checker, allSourceFiles);
Debug.assert(exportInfos.some(info => info.moduleSymbol === moduleSymbol));
// We sort the best codefixes first, so taking `first` is best for completions.
const moduleSpecifier = first(getNewImportInfos(program, sourceFile, exportInfos, compilerOptions, getCanonicalFileName, host, preferences)).moduleSpecifier;
const ctx: ImportCodeFixContext = { host, program, checker, compilerOptions, sourceFile, formatContext, symbolName, getCanonicalFileName, symbolToken, preferences };
return { moduleSpecifier, codeAction: first(getCodeActionsForImport(exportInfos, ctx)) };
}
function getAllReExportingModules(exportedSymbol: Symbol, checker: TypeChecker, allSourceFiles: ReadonlyArray<SourceFile>): ReadonlyArray<SymbolExportInfo> {
function getAllReExportingModules(exportedSymbol: Symbol, symbolName: string, checker: TypeChecker, allSourceFiles: ReadonlyArray<SourceFile>): ReadonlyArray<SymbolExportInfo> {
const result: SymbolExportInfo[] = [];
forEachExternalModule(checker, allSourceFiles, moduleSymbol => {
for (const exported of checker.getExportsOfModule(moduleSymbol)) {
if (skipAlias(exported, checker) === exportedSymbol) {
if (exported.escapedName === InternalSymbolName.Default || exported.name === symbolName && skipAlias(exported, checker) === exportedSymbol) {
const isDefaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol) === exported;
result.push({ moduleSymbol, importKind: isDefaultExport ? ImportKind.Default : ImportKind.Named });
}
@@ -13,7 +13,7 @@
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "(alias) const foo: 0\nexport default foo", "", "alias", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
includeCompletionsForModuleExports: true,
sourceDisplay: "./a",
});
@@ -0,0 +1,27 @@
/// <reference path="fourslash.ts" />
// @moduleResolution: node
// @Filename: /a.ts
////export const x = 0;
// @Filename: /index.ts
////export { x as y } from "./a";
// @Filename: /c.ts
/////**/
goTo.marker("");
verify.completionListContains({ name: "x", source: "/a" }, "const x: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeCompletionsForModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "x",
source: "/a",
description: `Import 'x' from module "./a"`,
newFileContent: `import { x } from "./a";
`,
});