11import debug from 'debug'
22
3- import type { ImportSettings , PluginSettings } from '../types.js'
3+ import type { NormalizedCacheSettings , PluginSettings } from '../types.js'
44
55const log = debug ( 'eslint-plugin-import-x:utils:ModuleCache' )
66
7- export type CacheKey = unknown
8-
97export interface CacheObject {
108 result : unknown
119 lastSeen : ReturnType < typeof process . hrtime >
1210}
1311
1412export class ModuleCache {
15- constructor ( public map : Map < CacheKey , CacheObject > = new Map ( ) ) { }
13+ constructor ( public map : Map < string , CacheObject > = new Map ( ) ) { }
1614
17- set ( cacheKey : CacheKey , result : unknown ) {
15+ set ( cacheKey : string , result : unknown ) {
1816 this . map . set ( cacheKey , {
1917 result,
2018 lastSeen : process . hrtime ( ) ,
@@ -23,13 +21,12 @@ export class ModuleCache {
2321 return result
2422 }
2523
26- get < T > ( cacheKey : CacheKey , settings : ImportSettings [ 'cache' ] ) : T | undefined {
27- if ( this . map . has ( cacheKey ) ) {
28- const f = this . map . get ( cacheKey )
24+ get < T > ( cacheKey : string , settings : NormalizedCacheSettings ) : T | undefined {
25+ const cache = this . map . get ( cacheKey )
26+ if ( cache ) {
2927 // check freshness
30- // @ts -expect-error TS can't narrow properly from `has` and `get`
31- if ( process . hrtime ( f . lastSeen ) [ 0 ] < settings . lifetime ) {
32- return f ! . result as T
28+ if ( process . hrtime ( cache . lastSeen ) [ 0 ] < settings . lifetime ) {
29+ return cache . result as T
3330 }
3431 } else {
3532 log ( 'cache miss for' , cacheKey )
@@ -51,8 +48,6 @@ export class ModuleCache {
5148 cacheSettings . lifetime = Number . POSITIVE_INFINITY
5249 }
5350
54- return cacheSettings as ImportSettings [ 'cache' ] & {
55- lifetime : number
56- }
51+ return cacheSettings as NormalizedCacheSettings
5752 }
5853}
0 commit comments