namespace ts . server { export interface ModuleSpecifierResolutionCacheHost { watchNodeModulesForPackageJsonChanges ( directoryPath ) : FileWatcher ; } export function createModuleSpecifierCache ( host ) { let containedNodeModulesWatchers ; let cache ; let currentKey ; const result = { get ( fromFileName , toFileName , preferences ) { if ( ! cache || currentKey !== key ( fromFileName , preferences ) ) return undefined ; return cache . get ( toFileName ) ; } , set ( fromFileName , toFileName , preferences , modulePaths , moduleSpecifiers ) { ensureCache ( fromFileName , preferences ) . set ( toFileName , createInfo ( modulePaths , moduleSpecifiers , true ) ) ; if ( moduleSpecifiers ) { for ( const p of modulePaths ) { if ( p . isInNodeModules ) { const nodeModulesPath = p . path . substring ( 0 , p . path . indexOf ( nodeModulesPathPart ) + nodeModulesPathPart . length - 1 ) ; if ( ! containedNodeModulesWatchers ?. has ( nodeModulesPath ) ) { ( containedNodeModulesWatchers ||= new Map ( ) ) . set ( nodeModulesPath , host . watchNodeModulesForPackageJsonChanges ( nodeModulesPath ) ) ; } } } } } , setModulePaths ( fromFileName , toFileName , preferences , modulePaths ) { const cache = ensureCache ( fromFileName , preferences ) ; const info = cache . get ( toFileName ) ; if ( info ) { info . modulePaths = modulePaths ; } else { cache . set ( toFileName , createInfo ( modulePaths , undefined , undefined ) ) ; } } , setIsAutoImportable ( fromFileName , toFileName , preferences , isAutoImportable ) { const cache = ensureCache ( fromFileName , preferences ) ; const info = cache . get ( toFileName ) ; if ( info ) { info . isAutoImportable = isAutoImportable ; } else { cache . set ( toFileName , createInfo ( undefined , undefined , isAutoImportable ) ) ; } } , clear ( ) { containedNodeModulesWatchers ?. forEach ( watcher => watcher . close ( ) ) ; cache ?. clear ( ) ; containedNodeModulesWatchers ?. clear ( ) ; currentKey = undefined ; } , count ( ) { return cache ? cache . size : 0 ; } } ; if ( Debug . isDebugging ) { Object . defineProperty ( result , "__cache" , { get : ( ) => cache } ) ; } return result ; function ensureCache ( fromFileName , preferences ) { const newKey = key ( fromFileName , preferences ) ; if ( cache && ( currentKey !== newKey ) ) { result . clear ( ) ; } currentKey = newKey ; return cache ||= new Map ( ) ; } function key ( fromFileName , preferences ) { return `${ fromFileName },${ preferences . importModuleSpecifierEnding },${ preferences . importModuleSpecifierPreference }` ; } function createInfo ( modulePaths , moduleSpecifiers , isAutoImportable ) { return { modulePaths , moduleSpecifiers , isAutoImportable } ; } } }