Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/getESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ const { Worker: JestWorker } = require('jest-worker');
// @ts-ignore
const { setup, lintFiles } = require('./worker');
const { getESLintOptions } = require('./options');
const { jsonStringifyReplacerSortKeys } = require('./utils');

/** @type {{[key: string]: any}} */
const cache = {};
/** @type {Map<CacheKey, any>} */
const cache = new Map();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use WeakMap? Otherwise it is memory leak for incremental builds


class CacheKey {
/**
* @param {string|undefined} key
* @param {Options} options
*/
constructor(key, options) {
this.key = key;
this.options = options;
}
}

/** @typedef {import('eslint').ESLint} ESLint */
/** @typedef {import('eslint').ESLint.LintResult} LintResult */
Expand Down Expand Up @@ -46,7 +56,7 @@ async function loadESLint(options) {
* @returns {Promise<Linter>}
*/
async function loadESLintThreaded(key, poolSize, options) {
const cacheKey = getCacheKey(key, options);
const cacheKey = new CacheKey(key, options);
const { eslintPath = 'eslint' } = options;
const source = require.resolve('./worker');
const workerOptions = {
Expand All @@ -73,7 +83,7 @@ async function loadESLintThreaded(key, poolSize, options) {
(worker && (await worker.lintFiles(files))) ||
/* istanbul ignore next */ [],
cleanup: async () => {
cache[cacheKey] = local;
cache.set(cacheKey, local);
context.lintFiles = (files) => local.lintFiles(files);
if (worker) {
worker.end();
Expand All @@ -99,23 +109,16 @@ async function getESLint(key, { threads, ...options }) {
: /* istanbul ignore next */
threads;

const cacheKey = getCacheKey(key, { threads, ...options });
if (!cache[cacheKey]) {
cache[cacheKey] =
const cacheKey = new CacheKey(key, { threads, ...options });
if (!cache.has(cacheKey)) {
cache.set(
cacheKey,
max > 1
? await loadESLintThreaded(key, max, options)
: await loadESLint(options);
: await loadESLint(options),
);
}
return cache[cacheKey];
}

/**
* @param {string|undefined} key
* @param {Options} options
* @returns {string}
*/
function getCacheKey(key, options) {
return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys);
return cache.get(cacheKey);
}

module.exports = {
Expand Down
21 changes: 0 additions & 21 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,8 @@ function parseFoldersToGlobs(patterns, extensions = []) {
});
}

/**
* @param {string} _ key, but unused
* @param {any} value
*/
const jsonStringifyReplacerSortKeys = (_, value) => {
/**
* @param {{ [x: string]: any; }} sorted
* @param {string | number} key
*/
const insert = (sorted, key) => {
// eslint-disable-next-line no-param-reassign
sorted[key] = value[key];
return sorted;
};

return value instanceof Object && !(value instanceof Array)
? Object.keys(value).sort().reduce(insert, {})
: value;
};

module.exports = {
arrify,
parseFiles,
parseFoldersToGlobs,
jsonStringifyReplacerSortKeys,
};
34 changes: 34 additions & 0 deletions test/circular-plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pack from './utils/pack';

describe('circular plugin', () => {
it('should support plugins with circular configs', async () => {
const plugin = {
configs: {},
rules: {},
processors: {},
};

Object.assign(plugin.configs, {
recommended: {
plugins: {
self: plugin,
},
rules: {},
},
});

const loaderOptions = {
configType: 'flat',
overrideConfig: {
plugins: { plugin: plugin },
},
overrideConfigFile: true,
};

const compiler = pack('good', loaderOptions);

const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
});
});
Loading