@@ -5,10 +5,20 @@ const { Worker: JestWorker } = require('jest-worker');
55// @ts -ignore
66const { setup, lintFiles } = require ( './worker' ) ;
77const { getESLintOptions } = require ( './options' ) ;
8- const { jsonStringifyReplacerSortKeys } = require ( './utils' ) ;
98
10- /** @type {{[key: string]: any} } */
11- const cache = { } ;
9+ /** @type {Map<CacheKey, any> } */
10+ const cache = new Map ( ) ;
11+
12+ class CacheKey {
13+ /**
14+ * @param {string|undefined } key
15+ * @param {Options } options
16+ */
17+ constructor ( key , options ) {
18+ this . key = key ;
19+ this . options = options ;
20+ }
21+ }
1222
1323/** @typedef {import('eslint').ESLint } ESLint */
1424/** @typedef {import('eslint').ESLint.LintResult } LintResult */
@@ -46,7 +56,7 @@ async function loadESLint(options) {
4656 * @returns {Promise<Linter> }
4757 */
4858async function loadESLintThreaded ( key , poolSize , options ) {
49- const cacheKey = getCacheKey ( key , options ) ;
59+ const cacheKey = new CacheKey ( key , options ) ;
5060 const { eslintPath = 'eslint' } = options ;
5161 const source = require . resolve ( './worker' ) ;
5262 const workerOptions = {
@@ -73,7 +83,7 @@ async function loadESLintThreaded(key, poolSize, options) {
7383 ( worker && ( await worker . lintFiles ( files ) ) ) ||
7484 /* istanbul ignore next */ [ ] ,
7585 cleanup : async ( ) => {
76- cache [ cacheKey ] = local ;
86+ cache . set ( cacheKey , local ) ;
7787 context . lintFiles = ( files ) => local . lintFiles ( files ) ;
7888 if ( worker ) {
7989 worker . end ( ) ;
@@ -99,23 +109,16 @@ async function getESLint(key, { threads, ...options }) {
99109 : /* istanbul ignore next */
100110 threads ;
101111
102- const cacheKey = getCacheKey ( key , { threads, ...options } ) ;
103- if ( ! cache [ cacheKey ] ) {
104- cache [ cacheKey ] =
112+ const cacheKey = new CacheKey ( key , { threads, ...options } ) ;
113+ if ( ! cache . has ( cacheKey ) ) {
114+ cache . set (
115+ cacheKey ,
105116 max > 1
106117 ? await loadESLintThreaded ( key , max , options )
107- : await loadESLint ( options ) ;
118+ : await loadESLint ( options ) ,
119+ ) ;
108120 }
109- return cache [ cacheKey ] ;
110- }
111-
112- /**
113- * @param {string|undefined } key
114- * @param {Options } options
115- * @returns {string }
116- */
117- function getCacheKey ( key , options ) {
118- return JSON . stringify ( { key, options } , jsonStringifyReplacerSortKeys ) ;
121+ return cache . get ( cacheKey ) ;
119122}
120123
121124module . exports = {
0 commit comments