Skip to content

Commit a301001

Browse files
authored
Use correct program when checking for reexports from AutoImportProviderProject (microsoft#40843)
1 parent f615e22 commit a301001

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ namespace ts.codefix {
498498
const compilerOptions = program.getCompilerOptions();
499499
const preferTypeOnlyImport = compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error && isValidTypeOnlyAliasUseSite(symbolToken);
500500
const useRequire = shouldUseRequire(sourceFile, compilerOptions);
501-
const exportInfos = getExportInfos(symbolName, getMeaningFromLocation(symbolToken), cancellationToken, sourceFile, checker, program, useAutoImportProvider, host);
501+
const exportInfos = getExportInfos(symbolName, getMeaningFromLocation(symbolToken), cancellationToken, sourceFile, program, useAutoImportProvider, host);
502502
const fixes = arrayFrom(flatMapIterator(exportInfos.entries(), ([_, exportInfos]) =>
503503
getFixForImport(exportInfos, symbolName, symbolToken.getStart(sourceFile), preferTypeOnlyImport, useRequire, program, sourceFile, host, preferences)));
504504
return { fixes, symbolName };
@@ -521,29 +521,29 @@ namespace ts.codefix {
521521
currentTokenMeaning: SemanticMeaning,
522522
cancellationToken: CancellationToken,
523523
sourceFile: SourceFile,
524-
checker: TypeChecker,
525524
program: Program,
526525
useAutoImportProvider: boolean,
527526
host: LanguageServiceHost
528527
): ReadonlyESMap<string, readonly SymbolExportInfo[]> {
529528
// For each original symbol, keep all re-exports of that symbol together so we can call `getCodeActionsForImport` on the whole group at once.
530529
// Maps symbol id to info for modules providing that symbol (original export + re-exports).
531530
const originalSymbolToExportInfos = createMultiMap<SymbolExportInfo>();
532-
function addSymbol(moduleSymbol: Symbol, exportedSymbol: Symbol, importKind: ImportKind): void {
531+
function addSymbol(moduleSymbol: Symbol, exportedSymbol: Symbol, importKind: ImportKind, checker: TypeChecker): void {
533532
originalSymbolToExportInfos.add(getUniqueSymbolId(exportedSymbol, checker).toString(), { moduleSymbol, importKind, exportedSymbolIsTypeOnly: isTypeOnlySymbol(exportedSymbol, checker) });
534533
}
535-
forEachExternalModuleToImportFrom(program, host, sourceFile, /*filterByPackageJson*/ true, useAutoImportProvider, moduleSymbol => {
534+
forEachExternalModuleToImportFrom(program, host, sourceFile, /*filterByPackageJson*/ true, useAutoImportProvider, (moduleSymbol, _, program) => {
535+
const checker = program.getTypeChecker();
536536
cancellationToken.throwIfCancellationRequested();
537537

538538
const defaultInfo = getDefaultLikeExportInfo(sourceFile, moduleSymbol, checker, program.getCompilerOptions());
539539
if (defaultInfo && defaultInfo.name === symbolName && symbolHasMeaning(defaultInfo.symbolForMeaning, currentTokenMeaning)) {
540-
addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind);
540+
addSymbol(moduleSymbol, defaultInfo.symbol, defaultInfo.kind, checker);
541541
}
542542

543543
// check exports with the same name
544544
const exportSymbolWithIdenticalName = checker.tryGetMemberInModuleExportsAndProperties(symbolName, moduleSymbol);
545545
if (exportSymbolWithIdenticalName && symbolHasMeaning(exportSymbolWithIdenticalName, currentTokenMeaning)) {
546-
addSymbol(moduleSymbol, exportSymbolWithIdenticalName, ImportKind.Named);
546+
addSymbol(moduleSymbol, exportSymbolWithIdenticalName, ImportKind.Named, checker);
547547
}
548548
});
549549
return originalSymbolToExportInfos;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /package.json
4+
//// { "dependencies": { "react-hook-form": "*" } }
5+
6+
// @Filename: /node_modules/react-hook-form/package.json
7+
//// { "types": "dist/index.d.ts" }
8+
9+
// @Filename: /node_modules/react-hook-form/dist/index.d.ts
10+
//// export * from "./useForm";
11+
12+
// @Filename: /node_modules/react-hook-form/dist/useForm.d.ts
13+
//// export declare function useForm(): void;
14+
15+
// @Filename: /index.ts
16+
//// useForm/**/
17+
18+
goTo.marker("");
19+
verify.importFixAtPosition([
20+
`import { useForm } from "react-hook-form";\r\n\r\nuseForm`,
21+
`import { useForm } from "react-hook-form/dist/useForm";\r\n\r\nuseForm`
22+
]);

0 commit comments

Comments
 (0)