@@ -4,26 +4,26 @@ exports.__esModule = true;
4
4
5
5
const log = require ( 'debug' ) ( 'eslint-module-utils:ModuleCache' ) ;
6
6
7
+ /** @type {import('./ModuleCache').ModuleCache } */
7
8
class ModuleCache {
9
+ /** @param {typeof import('./ModuleCache').ModuleCache.prototype.map } map */
8
10
constructor ( map ) {
9
- this . map = map || new Map ( ) ;
11
+ this . map = map || /** @type {{typeof import('./ModuleCache').ModuleCache.prototype.map} */ new Map ( ) ;
10
12
}
11
13
12
- /**
13
- * returns value for returning inline
14
- * @param {[type] } cacheKey [description]
15
- * @param {[type] } result [description]
16
- */
14
+ /** @type {typeof import('./ModuleCache').ModuleCache.prototype.set } */
17
15
set ( cacheKey , result ) {
18
16
this . map . set ( cacheKey , { result, lastSeen : process . hrtime ( ) } ) ;
19
17
log ( 'setting entry for' , cacheKey ) ;
20
18
return result ;
21
19
}
22
20
21
+ /** @type {typeof import('./ModuleCache').ModuleCache.prototype.get } */
23
22
get ( cacheKey , settings ) {
24
23
if ( this . map . has ( cacheKey ) ) {
25
24
const f = this . map . get ( cacheKey ) ;
26
25
// check freshness
26
+ // @ts -expect-error TS can't narrow properly from `has` and `get`
27
27
if ( process . hrtime ( f . lastSeen ) [ 0 ] < settings . lifetime ) { return f . result ; }
28
28
} else {
29
29
log ( 'cache miss for' , cacheKey ) ;
@@ -32,19 +32,21 @@ class ModuleCache {
32
32
return undefined ;
33
33
}
34
34
35
- }
36
-
37
- ModuleCache . getSettings = function ( settings ) {
38
- const cacheSettings = Object . assign ( {
39
- lifetime : 30 , // seconds
40
- } , settings [ 'import/cache' ] ) ;
35
+ /** @type {typeof import('./ModuleCache').ModuleCache.getSettings } */
36
+ static getSettings ( settings ) {
37
+ /** @type {ReturnType<typeof ModuleCache.getSettings> } */
38
+ const cacheSettings = Object . assign ( {
39
+ lifetime : 30 , // seconds
40
+ } , settings [ 'import/cache' ] ) ;
41
+
42
+ // parse infinity
43
+ // @ts -expect-error the lack of type overlap is because we're abusing `cacheSettings` as a temporary object
44
+ if ( cacheSettings . lifetime === '∞' || cacheSettings . lifetime === 'Infinity' ) {
45
+ cacheSettings . lifetime = Infinity ;
46
+ }
41
47
42
- // parse infinity
43
- if ( cacheSettings . lifetime === '∞' || cacheSettings . lifetime === 'Infinity' ) {
44
- cacheSettings . lifetime = Infinity ;
48
+ return cacheSettings ;
45
49
}
46
-
47
- return cacheSettings ;
48
- } ;
50
+ }
49
51
50
52
exports . default = ModuleCache ;
0 commit comments