Skip to content

Commit 16e40e2

Browse files
committed
Never show pnpm paths in auto imports, even if there’s no other path
1 parent 7c72d49 commit 16e40e2

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,16 @@ namespace ts.moduleSpecifiers {
163163

164164
return pathsSpecifiers?.length ? pathsSpecifiers :
165165
nodeModulesSpecifiers?.length ? nodeModulesSpecifiers :
166-
Debug.checkDefined(relativeSpecifiers);
166+
fallback();
167+
168+
// Unless 'preferFailureToResultsWithIgnoredPaths' is set, we *must* generate a relative path fallback.
169+
// (The declaration emitter must write something, but auto-imports may exclude bad suggestions.)
170+
function fallback() {
171+
if (host.preferFailureToResultsWithIgnoredPaths?.()) {
172+
return relativeSpecifiers || emptyArray;
173+
}
174+
return Debug.checkDefined(relativeSpecifiers);
175+
}
167176
}
168177

169178
interface Info {
@@ -286,7 +295,7 @@ namespace ts.moduleSpecifiers {
286295
const redirects = host.redirectTargetsMap.get(importedPath) || emptyArray;
287296
const importedFileNames = [...(referenceRedirect ? [referenceRedirect] : emptyArray), importedFileName, ...redirects];
288297
const targets = importedFileNames.map(f => getNormalizedAbsolutePath(f, cwd));
289-
let shouldFilterIgnoredPaths = !every(targets, containsIgnoredPath);
298+
let shouldFilterIgnoredPaths = host.preferFailureToResultsWithIgnoredPaths?.() || !every(targets, containsIgnoredPath);
290299

291300
if (!preferSymlinks) {
292301
// Symlinks inside ignored paths are already filtered out of the symlink cache,

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8137,6 +8137,7 @@ namespace ts {
81378137
getModuleSpecifierCache?(): ModuleSpecifierCache;
81388138
getGlobalTypingsCacheLocation?(): string | undefined;
81398139
getNearestAncestorDirectoryWithPackageJson?(fileName: string, rootDir?: string): string | undefined;
8140+
preferFailureToResultsWithIgnoredPaths?(): boolean;
81408141

81418142
getSourceFiles(): readonly SourceFile[];
81428143
readonly redirectTargetsMap: RedirectTargetsMap;

src/services/codefixes/importFixes.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ namespace ts.codefix {
6666
const preferTypeOnlyImport = !!usageIsTypeOnly && compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error;
6767
const useRequire = shouldUseRequire(sourceFile, program);
6868
const fix = getImportFixForSymbol(sourceFile, exportInfos, moduleSymbol, symbolName, program, /*position*/ undefined, preferTypeOnlyImport, useRequire, host, preferences);
69-
addImport({ fixes: [fix], symbolName });
69+
if (fix) {
70+
addImport({ fixes: [fix], symbolName });
71+
}
7072
}
7173

7274
function addImport(info: FixesInfo) {
@@ -203,7 +205,7 @@ namespace ts.codefix {
203205
: getAllReExportingModules(sourceFile, exportedSymbol, moduleSymbol, symbolName, host, program, /*useAutoImportProvider*/ true);
204206
const useRequire = shouldUseRequire(sourceFile, program);
205207
const preferTypeOnlyImport = compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error && !isSourceFileJS(sourceFile) && isValidTypeOnlyAliasUseSite(getTokenAtPosition(sourceFile, position));
206-
const fix = getImportFixForSymbol(sourceFile, exportInfos, moduleSymbol, symbolName, program, position, preferTypeOnlyImport, useRequire, host, preferences);
208+
const fix = Debug.checkDefined(getImportFixForSymbol(sourceFile, exportInfos, moduleSymbol, symbolName, program, position, preferTypeOnlyImport, useRequire, host, preferences));
207209
return { moduleSpecifier: fix.moduleSpecifier, codeAction: codeFixActionToCodeAction(codeActionForFix({ host, formatContext, preferences }, sourceFile, symbolName, fix, getQuotePreference(sourceFile, preferences))) };
208210
}
209211

@@ -274,7 +276,7 @@ namespace ts.codefix {
274276
program: Program,
275277
host: LanguageServiceHost,
276278
preferences: UserPreferences
277-
): { exportInfo?: SymbolExportInfo, moduleSpecifier: string } {
279+
): { exportInfo?: SymbolExportInfo, moduleSpecifier: string } | undefined {
278280
return getBestFix(getNewImportFixes(program, importingFile, /*position*/ undefined, /*preferTypeOnlyImport*/ false, /*useRequire*/ false, exportInfo, host, preferences), importingFile, host);
279281
}
280282

@@ -529,7 +531,8 @@ namespace ts.codefix {
529531
return sort(fixes, (a, b) => compareValues(a.kind, b.kind) || compareModuleSpecifiers(a, b, allowsImportingSpecifier));
530532
}
531533

532-
function getBestFix<T extends ImportFix>(fixes: readonly T[], sourceFile: SourceFile, host: LanguageServiceHost): T {
534+
function getBestFix<T extends ImportFix>(fixes: readonly T[], sourceFile: SourceFile, host: LanguageServiceHost): T | undefined {
535+
if (!some(fixes)) return;
533536
// These will always be placed first if available, and are better than other kinds
534537
if (fixes[0].kind === ImportFixKind.UseNamespace || fixes[0].kind === ImportFixKind.AddToExisting) {
535538
return fixes[0];

src/services/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ namespace ts.Completions {
17621762
// If we don't need to resolve module specifiers, we can use any re-export that is importable at all
17631763
// (We need to ensure that at least one is importable to show a completion.)
17641764
const { moduleSpecifier, exportInfo } = resolveModuleSpecifiers
1765-
? codefix.getModuleSpecifierForBestExportInfo(info, sourceFile, program, host, preferences)
1765+
? codefix.getModuleSpecifierForBestExportInfo(info, sourceFile, program, host, preferences) || {}
17661766
: { moduleSpecifier: undefined, exportInfo: find(info, isImportableExportInfo) };
17671767
if (!exportInfo) return;
17681768
const moduleFile = tryCast(exportInfo.moduleSymbol.valueDeclaration, isSourceFile);

src/services/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,7 @@ namespace ts {
18521852
isSourceOfProjectReferenceRedirect: fileName => program.isSourceOfProjectReferenceRedirect(fileName),
18531853
getNearestAncestorDirectoryWithPackageJson: maybeBind(host, host.getNearestAncestorDirectoryWithPackageJson),
18541854
getFileIncludeReasons: () => program.getFileIncludeReasons(),
1855+
preferFailureToResultsWithIgnoredPaths: returnTrue,
18551856
};
18561857
}
18571858

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /project/tsconfig.json
4+
//// { "compilerOptions": { "module": "commonjs" } }
5+
6+
// @Filename: /project/node_modules/.pnpm/@types+react@17.0.7/node_modules/@types/react/index.d.ts
7+
//// import "csstype";
8+
//// export declare function Component(): void;
9+
10+
// @Filename: /project/node_modules/.pnpm/csstype@3.0.8/node_modules/csstype/index.d.ts
11+
//// export interface SvgProperties {}
12+
13+
// @Filename: /project/index.ts
14+
//// [|import SvgProp/**/|]
15+
16+
// @link: /project/node_modules/.pnpm/@types+react@17.0.7/node_modules/@types/react -> /project/node_modules/@types/react
17+
// @link: /project/node_modules/.pnpm/csstype@3.0.8/node_modules/csstype -> /project/node_modules/.pnpm/@types+react@17.0.7/node_modules/csstype
18+
19+
goTo.marker("");
20+
verify.completions({
21+
marker: "",
22+
exact: [],
23+
preferences: {
24+
includeCompletionsForImportStatements: true,
25+
includeCompletionsWithInsertText: true,
26+
includeCompletionsWithSnippetText: true,
27+
}
28+
});

0 commit comments

Comments
 (0)