|
| 1 | +import type { Path, UserPreferences } from 'typescript/lib/tsserverlibrary'; |
| 2 | + |
| 3 | +export interface ModulePath { |
| 4 | + path: string; |
| 5 | + isInNodeModules: boolean; |
| 6 | + isRedirect: boolean; |
| 7 | +} |
| 8 | + |
| 9 | +export interface ResolvedModuleSpecifierInfo { |
| 10 | + modulePaths: readonly ModulePath[] | undefined; |
| 11 | + moduleSpecifiers: readonly string[] | undefined; |
| 12 | + isAutoImportable: boolean | undefined; |
| 13 | +} |
| 14 | + |
| 15 | +export interface ModuleSpecifierCache { |
| 16 | + get(fromFileName: Path, toFileName: Path, preferences: UserPreferences): Readonly<ResolvedModuleSpecifierInfo> | undefined; |
| 17 | + set(fromFileName: Path, toFileName: Path, preferences: UserPreferences, modulePaths: readonly ModulePath[], moduleSpecifiers: readonly string[]): void; |
| 18 | + setIsAutoImportable(fromFileName: Path, toFileName: Path, preferences: UserPreferences, isAutoImportable: boolean): void; |
| 19 | + setModulePaths(fromFileName: Path, toFileName: Path, preferences: UserPreferences, modulePaths: readonly ModulePath[]): void; |
| 20 | + clear(): void; |
| 21 | + count(): number; |
| 22 | +} |
| 23 | + |
| 24 | +// export interface ModuleSpecifierResolutionCacheHost { |
| 25 | +// watchNodeModulesForPackageJsonChanges(directoryPath: string): FileWatcher; |
| 26 | +// } |
| 27 | + |
| 28 | +export function createModuleSpecifierCache( |
| 29 | + // host: ModuleSpecifierResolutionCacheHost |
| 30 | +): ModuleSpecifierCache { |
| 31 | + // let containedNodeModulesWatchers: Map<string, FileWatcher> | undefined; // TODO |
| 32 | + let cache: Map<Path, ResolvedModuleSpecifierInfo> | undefined; |
| 33 | + let currentKey: string | undefined; |
| 34 | + const result: ModuleSpecifierCache = { |
| 35 | + get(fromFileName, toFileName, preferences) { |
| 36 | + if (!cache || currentKey !== key(fromFileName, preferences)) return undefined; |
| 37 | + return cache.get(toFileName); |
| 38 | + }, |
| 39 | + set(fromFileName, toFileName, preferences, modulePaths, moduleSpecifiers) { |
| 40 | + ensureCache(fromFileName, preferences).set(toFileName, createInfo(modulePaths, moduleSpecifiers, /*isAutoImportable*/ true)); |
| 41 | + |
| 42 | + // If any module specifiers were generated based off paths in node_modules, |
| 43 | + // a package.json file in that package was read and is an input to the cached. |
| 44 | + // Instead of watching each individual package.json file, set up a wildcard |
| 45 | + // directory watcher for any node_modules referenced and clear the cache when |
| 46 | + // it sees any changes. |
| 47 | + if (moduleSpecifiers) { |
| 48 | + for (const p of modulePaths) { |
| 49 | + if (p.isInNodeModules) { |
| 50 | + // No trailing slash |
| 51 | + // const nodeModulesPath = p.path.substring(0, p.path.indexOf(nodeModulesPathPart) + nodeModulesPathPart.length - 1); |
| 52 | + // if (!containedNodeModulesWatchers?.has(nodeModulesPath)) { |
| 53 | + // (containedNodeModulesWatchers ||= new Map()).set( |
| 54 | + // nodeModulesPath, |
| 55 | + // host.watchNodeModulesForPackageJsonChanges(nodeModulesPath), |
| 56 | + // ); |
| 57 | + // } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + setModulePaths(fromFileName, toFileName, preferences, modulePaths) { |
| 63 | + const cache = ensureCache(fromFileName, preferences); |
| 64 | + const info = cache.get(toFileName); |
| 65 | + if (info) { |
| 66 | + info.modulePaths = modulePaths; |
| 67 | + } |
| 68 | + else { |
| 69 | + cache.set(toFileName, createInfo(modulePaths, /*moduleSpecifiers*/ undefined, /*isAutoImportable*/ undefined)); |
| 70 | + } |
| 71 | + }, |
| 72 | + setIsAutoImportable(fromFileName, toFileName, preferences, isAutoImportable) { |
| 73 | + const cache = ensureCache(fromFileName, preferences); |
| 74 | + const info = cache.get(toFileName); |
| 75 | + if (info) { |
| 76 | + info.isAutoImportable = isAutoImportable; |
| 77 | + } |
| 78 | + else { |
| 79 | + cache.set(toFileName, createInfo(/*modulePaths*/ undefined, /*moduleSpecifiers*/ undefined, isAutoImportable)); |
| 80 | + } |
| 81 | + }, |
| 82 | + clear() { |
| 83 | + // containedNodeModulesWatchers?.forEach(watcher => watcher.close()); |
| 84 | + cache?.clear(); |
| 85 | + // containedNodeModulesWatchers?.clear(); |
| 86 | + currentKey = undefined; |
| 87 | + }, |
| 88 | + count() { |
| 89 | + return cache ? cache.size : 0; |
| 90 | + } |
| 91 | + }; |
| 92 | + // if (Debug.isDebugging) { |
| 93 | + // Object.defineProperty(result, "__cache", { get: () => cache }); |
| 94 | + // } |
| 95 | + return result; |
| 96 | + |
| 97 | + function ensureCache(fromFileName: Path, preferences: UserPreferences) { |
| 98 | + const newKey = key(fromFileName, preferences); |
| 99 | + if (cache && (currentKey !== newKey)) { |
| 100 | + result.clear(); |
| 101 | + } |
| 102 | + currentKey = newKey; |
| 103 | + return cache ||= new Map(); |
| 104 | + } |
| 105 | + |
| 106 | + function key(fromFileName: Path, preferences: UserPreferences) { |
| 107 | + return `${fromFileName},${preferences.importModuleSpecifierEnding},${preferences.importModuleSpecifierPreference}`; |
| 108 | + } |
| 109 | + |
| 110 | + function createInfo( |
| 111 | + modulePaths: readonly ModulePath[] | undefined, |
| 112 | + moduleSpecifiers: readonly string[] | undefined, |
| 113 | + isAutoImportable: boolean | undefined, |
| 114 | + ): ResolvedModuleSpecifierInfo { |
| 115 | + return { modulePaths, moduleSpecifiers, isAutoImportable }; |
| 116 | + } |
| 117 | +} |
0 commit comments