mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Make AutoImportProvider look in ATA cache (#48329)
* Make AutoImportProvider consider ATA cache * Delete unnecessary test
This commit is contained in:
@@ -994,7 +994,7 @@ namespace Harness.LanguageService {
|
||||
cancellationToken: ts.server.nullCancellationToken,
|
||||
useSingleInferredProject: false,
|
||||
useInferredProjectPerProjectRoot: false,
|
||||
typingsInstaller: undefined!, // TODO: GH#18217
|
||||
typingsInstaller: { ...ts.server.nullTypingsInstaller, globalTypingsCacheLocation: "/Library/Caches/typescript" },
|
||||
byteLength: Utils.byteLength,
|
||||
hrtime: process.hrtime,
|
||||
logger: serverHost,
|
||||
|
||||
+20
-13
@@ -1974,19 +1974,26 @@ namespace ts.server {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to load from the @types package.
|
||||
const typesPackageJson = resolvePackageNameToPackageJson(
|
||||
`@types/${name}`,
|
||||
hostProject.currentDirectory,
|
||||
compilerOptions,
|
||||
moduleResolutionHost,
|
||||
program.getModuleResolutionCache());
|
||||
if (typesPackageJson) {
|
||||
const entrypoints = getRootNamesFromPackageJson(typesPackageJson, program, symlinkCache);
|
||||
rootNames = concatenate(rootNames, entrypoints);
|
||||
dependenciesAdded += entrypoints?.length ? 1 : 0;
|
||||
continue;
|
||||
}
|
||||
// 2. Try to load from the @types package in the tree and in the global
|
||||
// typings cache location, if enabled.
|
||||
const done = forEach([hostProject.currentDirectory, hostProject.getGlobalTypingsCacheLocation()], directory => {
|
||||
if (directory) {
|
||||
const typesPackageJson = resolvePackageNameToPackageJson(
|
||||
`@types/${name}`,
|
||||
directory,
|
||||
compilerOptions,
|
||||
moduleResolutionHost,
|
||||
program.getModuleResolutionCache());
|
||||
if (typesPackageJson) {
|
||||
const entrypoints = getRootNamesFromPackageJson(typesPackageJson, program, symlinkCache);
|
||||
rootNames = concatenate(rootNames, entrypoints);
|
||||
dependenciesAdded += entrypoints?.length ? 1 : 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (done) continue;
|
||||
|
||||
// 3. If the @types package did not exist and the user has settings that
|
||||
// allow processing JS from node_modules, go back to the implementation
|
||||
|
||||
@@ -59,6 +59,7 @@ namespace ts {
|
||||
export interface CacheableExportInfoMapHost {
|
||||
getCurrentProgram(): Program | undefined;
|
||||
getPackageJsonAutoImportProvider(): Program | undefined;
|
||||
getGlobalTypingsCacheLocation(): string | undefined;
|
||||
}
|
||||
|
||||
export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost): ExportInfoMap {
|
||||
@@ -99,7 +100,7 @@ namespace ts {
|
||||
packageName = unmangleScopedPackageName(getPackageNameFromTypesPackageName(moduleFile.fileName.substring(topLevelPackageNameIndex + 1, packageRootIndex)));
|
||||
if (startsWith(importingFile, moduleFile.path.substring(0, topLevelNodeModulesIndex))) {
|
||||
const prevDeepestNodeModulesPath = packages.get(packageName);
|
||||
const nodeModulesPath = moduleFile.fileName.substring(0, topLevelPackageNameIndex);
|
||||
const nodeModulesPath = moduleFile.fileName.substring(0, topLevelPackageNameIndex + 1);
|
||||
if (prevDeepestNodeModulesPath) {
|
||||
const prevDeepestNodeModulesIndex = prevDeepestNodeModulesPath.indexOf(nodeModulesPathPart);
|
||||
if (topLevelNodeModulesIndex > prevDeepestNodeModulesIndex) {
|
||||
@@ -272,6 +273,8 @@ namespace ts {
|
||||
|
||||
function isNotShadowedByDeeperNodeModulesPackage(info: SymbolExportInfo, packageName: string | undefined) {
|
||||
if (!packageName || !info.moduleFileName) return true;
|
||||
const typingsCacheLocation = host.getGlobalTypingsCacheLocation();
|
||||
if (typingsCacheLocation && startsWith(info.moduleFileName, typingsCacheLocation)) return true;
|
||||
const packageDeepestNodeModulesPath = packages.get(packageName);
|
||||
return !packageDeepestNodeModulesPath || startsWith(info.moduleFileName, packageDeepestNodeModulesPath);
|
||||
}
|
||||
@@ -367,6 +370,7 @@ namespace ts {
|
||||
const cache = host.getCachedExportInfoMap?.() || createCacheableExportInfoMap({
|
||||
getCurrentProgram: () => program,
|
||||
getPackageJsonAutoImportProvider: () => host.getPackageJsonAutoImportProvider?.(),
|
||||
getGlobalTypingsCacheLocation: () => host.getGlobalTypingsCacheLocation?.(),
|
||||
});
|
||||
|
||||
if (cache.isUsableByFile(importingFile.path)) {
|
||||
|
||||
@@ -4,11 +4,17 @@
|
||||
//// { "compilerOptions": { "allowJs": true, "checkJs": true } }
|
||||
|
||||
// @Filename: /Library/Caches/typescript/node_modules/@types/react-router-dom/package.json
|
||||
//// { "name": "react-router-dom" }
|
||||
//// { "name": "@types/react-router-dom", "version": "16.8.4", "types": "index.d.ts" }
|
||||
|
||||
// @Filename: /Library/Caches/typescript/node_modules/@types/react-router-dom/index.d.ts
|
||||
////export class BrowserRouter {}
|
||||
|
||||
// @Filename: /project/node_modules/react-router-dom/package.json
|
||||
//// { "name": "react-router-dom", "version": "16.8.4", "main": "index.js" }
|
||||
|
||||
// @Filename: /project/node_modules/react-router-dom/index.js
|
||||
//// export const BrowserRouter = () => null;
|
||||
|
||||
// @Filename: /project/index.js
|
||||
////BrowserRouter/**/
|
||||
|
||||
@@ -16,3 +22,4 @@ goTo.file("/project/index.js");
|
||||
verify.importFixAtPosition([`const { BrowserRouter } = require("react-router-dom");
|
||||
|
||||
BrowserRouter`]);
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/// <reference path="../fourslash.ts" />
|
||||
|
||||
// @Filename: /Library/Caches/typescript/node_modules/@types/react-router-dom/package.json
|
||||
//// { "name": "@types/react-router-dom", "version": "16.8.4", "types": "index.d.ts" }
|
||||
|
||||
// @Filename: /Library/Caches/typescript/node_modules/@types/react-router-dom/index.d.ts
|
||||
//// export class BrowserRouterFromDts {}
|
||||
|
||||
// @Filename: /project/package.json
|
||||
//// { "dependencies": { "react-router-dom": "*" } }
|
||||
|
||||
// @Filename: /project/tsconfig.json
|
||||
//// { "compilerOptions": { "module": "commonjs", "allowJs": true, "checkJs": true, "maxNodeModuleJsDepth": 2 }, "typeAcquisition": { "enable": true } }
|
||||
|
||||
// @Filename: /project/node_modules/react-router-dom/package.json
|
||||
//// { "name": "react-router-dom", "version": "16.8.4", "main": "index.js" }
|
||||
|
||||
// @Filename: /project/node_modules/react-router-dom/index.js
|
||||
//// import "./BrowserRouter";
|
||||
//// export {};
|
||||
|
||||
// @Filename: /project/node_modules/react-router-dom/BrowserRouter.js
|
||||
//// export const BrowserRouterFromJs = () => null;
|
||||
|
||||
// @Filename: /project/index.js
|
||||
////BrowserRouter/**/
|
||||
|
||||
verify.completions({
|
||||
marker: "",
|
||||
exact: completion.globalsInJsPlus([{
|
||||
name: "BrowserRouterFromDts",
|
||||
source: "react-router-dom",
|
||||
sourceDisplay: "react-router-dom",
|
||||
hasAction: true,
|
||||
sortText: completion.SortText.AutoImportSuggestions,
|
||||
}]),
|
||||
preferences: {
|
||||
allowIncompleteCompletions: true,
|
||||
includeCompletionsForModuleExports: true,
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user