Skip to content

Use correct program when checking for reexports from AutoImportProviderProject #40843

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
Sep 30, 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
12 changes: 6 additions & 6 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ namespace ts.codefix {
const compilerOptions = program.getCompilerOptions();
const preferTypeOnlyImport = compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error && isValidTypeOnlyAliasUseSite(symbolToken);
const useRequire = shouldUseRequire(sourceFile, compilerOptions);
const exportInfos = getExportInfos(symbolName, getMeaningFromLocation(symbolToken), cancellationToken, sourceFile, checker, program, useAutoImportProvider, host);
const exportInfos = getExportInfos(symbolName, getMeaningFromLocation(symbolToken), cancellationToken, sourceFile, program, useAutoImportProvider, host);
const fixes = arrayFrom(flatMapIterator(exportInfos.entries(), ([_, exportInfos]) =>
getFixForImport(exportInfos, symbolName, symbolToken.getStart(sourceFile), preferTypeOnlyImport, useRequire, program, sourceFile, host, preferences)));
return { fixes, symbolName };
Expand All @@ -521,29 +521,29 @@ namespace ts.codefix {
currentTokenMeaning: SemanticMeaning,
cancellationToken: CancellationToken,
sourceFile: SourceFile,
checker: TypeChecker,
program: Program,
useAutoImportProvider: boolean,
host: LanguageServiceHost
): ReadonlyESMap<string, readonly SymbolExportInfo[]> {
// For each original symbol, keep all re-exports of that symbol together so we can call `getCodeActionsForImport` on the whole group at once.
// Maps symbol id to info for modules providing that symbol (original export + re-exports).
const originalSymbolToExportInfos = createMultiMap<SymbolExportInfo>();
function addSymbol(moduleSymbol: Symbol, exportedSymbol: Symbol, importKind: ImportKind): void {
function addSymbol(moduleSymbol: Symbol, exportedSymbol: Symbol, importKind: ImportKind, checker: TypeChecker): void {
originalSymbolToExportInfos.add(getUniqueSymbolId(exportedSymbol, checker).toString(), { moduleSymbol, importKind, exportedSymbolIsTypeOnly: isTypeOnlySymbol(exportedSymbol, checker) });
}
forEachExternalModuleToImportFrom(program, host, sourceFile, /*filterByPackageJson*/ true, useAutoImportProvider, moduleSymbol => {
forEachExternalModuleToImportFrom(program, host, sourceFile, /*filterByPackageJson*/ true, useAutoImportProvider, (moduleSymbol, _, program) => {
const checker = program.getTypeChecker();
Comment on lines +534 to +535
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there’s an AutoImportProviderProject, these modules can come from different programs, but this was always asking for exports of module symbols with the same checker instance, which would be missing resolved exports cached on symbol links if the module was checked by a different checker. That’s why a program is passed to this callback, but I missed this usage of it in the original refactor.

cancellationToken.throwIfCancellationRequested();

const defaultInfo = getDefaultLikeExportInfo(sourceFile, moduleSymbol, checker, program.getCompilerOptions());
if (defaultInfo && defaultInfo.name === symbolName && symbolHasMeaning(defaultInfo.symbolForMeaning, currentTokenMeaning)) {
addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind);
addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind, checker);
}

// check exports with the same name
const exportSymbolWithIdenticalName = checker.tryGetMemberInModuleExportsAndProperties(symbolName, moduleSymbol);
if (exportSymbolWithIdenticalName && symbolHasMeaning(exportSymbolWithIdenticalName, currentTokenMeaning)) {
addSymbol(moduleSymbol, exportSymbolWithIdenticalName, ImportKind.Named);
addSymbol(moduleSymbol, exportSymbolWithIdenticalName, ImportKind.Named, checker);
}
});
return originalSymbolToExportInfos;
Expand Down
22 changes: 22 additions & 0 deletions tests/cases/fourslash/server/autoImportProvider5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path="../fourslash.ts" />

// @Filename: /package.json
//// { "dependencies": { "react-hook-form": "*" } }

// @Filename: /node_modules/react-hook-form/package.json
//// { "types": "dist/index.d.ts" }

// @Filename: /node_modules/react-hook-form/dist/index.d.ts
//// export * from "./useForm";

// @Filename: /node_modules/react-hook-form/dist/useForm.d.ts
//// export declare function useForm(): void;

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

goTo.marker("");
verify.importFixAtPosition([
`import { useForm } from "react-hook-form";\r\n\r\nuseForm`,
`import { useForm } from "react-hook-form/dist/useForm";\r\n\r\nuseForm`
]);