@@ -194,6 +194,88 @@ namespace ts {
194194 } ) ;
195195
196196 describe ( "Node module resolution - non-relative paths" , ( ) => {
197+ it ( "computes correct commonPrefix for moduleName cache" , ( ) => {
198+ const resolutionCache = createModuleResolutionCache ( "/" , ( f ) => f ) ;
199+ let cache = resolutionCache . getOrCreateCacheForModuleName ( "a" ) ;
200+ cache . set ( "/sub" , {
201+ resolvedModule : {
202+ originalPath : undefined ,
203+ resolvedFileName : "/sub/node_modules/a/index.ts" ,
204+ isExternalLibraryImport : true ,
205+ extension : Extension . Ts ,
206+ } ,
207+ failedLookupLocations : [ ] ,
208+ } ) ;
209+ assert . isDefined ( cache . get ( "/sub" ) ) ;
210+ assert . isUndefined ( cache . get ( "/" ) ) ;
211+
212+ cache = resolutionCache . getOrCreateCacheForModuleName ( "b" ) ;
213+ cache . set ( "/sub/dir/foo" , {
214+ resolvedModule : {
215+ originalPath : undefined ,
216+ resolvedFileName : "/sub/directory/node_modules/b/index.ts" ,
217+ isExternalLibraryImport : true ,
218+ extension : Extension . Ts ,
219+ } ,
220+ failedLookupLocations : [ ] ,
221+ } ) ;
222+ assert . isDefined ( cache . get ( "/sub/dir/foo" ) ) ;
223+ assert . isDefined ( cache . get ( "/sub/dir" ) ) ;
224+ assert . isDefined ( cache . get ( "/sub" ) ) ;
225+ assert . isUndefined ( cache . get ( "/" ) ) ;
226+
227+ cache = resolutionCache . getOrCreateCacheForModuleName ( "c" ) ;
228+ cache . set ( "/foo/bar" , {
229+ resolvedModule : {
230+ originalPath : undefined ,
231+ resolvedFileName : "/bar/node_modules/c/index.ts" ,
232+ isExternalLibraryImport : true ,
233+ extension : Extension . Ts ,
234+ } ,
235+ failedLookupLocations : [ ] ,
236+ } ) ;
237+ assert . isDefined ( cache . get ( "/foo/bar" ) ) ;
238+ assert . isDefined ( cache . get ( "/foo" ) ) ;
239+ assert . isDefined ( cache . get ( "/" ) ) ;
240+
241+ cache = resolutionCache . getOrCreateCacheForModuleName ( "d" ) ;
242+ cache . set ( "/foo" , {
243+ resolvedModule : {
244+ originalPath : undefined ,
245+ resolvedFileName : "/foo/index.ts" ,
246+ isExternalLibraryImport : true ,
247+ extension : Extension . Ts ,
248+ } ,
249+ failedLookupLocations : [ ] ,
250+ } ) ;
251+ assert . isDefined ( cache . get ( "/foo" ) ) ;
252+ assert . isUndefined ( cache . get ( "/" ) ) ;
253+
254+ cache = resolutionCache . getOrCreateCacheForModuleName ( "e" ) ;
255+ cache . set ( "c:/foo" , {
256+ resolvedModule : {
257+ originalPath : undefined ,
258+ resolvedFileName : "d:/bar/node_modules/e/index.ts" ,
259+ isExternalLibraryImport : true ,
260+ extension : Extension . Ts ,
261+ } ,
262+ failedLookupLocations : [ ] ,
263+ } ) ;
264+ assert . isDefined ( cache . get ( "c:/foo" ) ) ;
265+ assert . isDefined ( cache . get ( "c:/" ) ) ;
266+ assert . isUndefined ( cache . get ( "d:/" ) ) ;
267+
268+ cache = resolutionCache . getOrCreateCacheForModuleName ( "f" ) ;
269+ cache . set ( "/foo/bar/baz" , {
270+ resolvedModule : undefined ,
271+ failedLookupLocations : [ ] ,
272+ } ) ;
273+ assert . isDefined ( cache . get ( "/foo/bar/baz" ) ) ;
274+ assert . isDefined ( cache . get ( "/foo/bar" ) ) ;
275+ assert . isDefined ( cache . get ( "/foo" ) ) ;
276+ assert . isDefined ( cache . get ( "/" ) ) ;
277+ } ) ;
278+
197279 it ( "load module as file - ts files not loaded" , ( ) => {
198280 test ( /*hasDirectoryExists*/ false ) ;
199281 test ( /*hasDirectoryExists*/ true ) ;
0 commit comments