Skip to content

Commit 02793cb

Browse files
committed
fix: don't error on circular eslint plugins
Replaces `JSON.stringify` with an implementation from `flatted` for cache keys.
1 parent e23dd77 commit 02793cb

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
},
5252
"dependencies": {
5353
"@types/eslint": "^9.6.1",
54+
"flatted": "^3.3.3",
5455
"jest-worker": "^29.7.0",
5556
"micromatch": "^4.0.8",
5657
"normalize-path": "^3.0.0",

src/getESLint.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { Worker: JestWorker } = require('jest-worker');
66
const { setup, lintFiles } = require('./worker');
77
const { getESLintOptions } = require('./options');
88
const { jsonStringifyReplacerSortKeys } = require('./utils');
9+
const { stringify } = require('flatted');
910

1011
/** @type {{[key: string]: any}} */
1112
const cache = {};
@@ -115,7 +116,7 @@ async function getESLint(key, { threads, ...options }) {
115116
* @returns {string}
116117
*/
117118
function getCacheKey(key, options) {
118-
return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys);
119+
return stringify({ key, options }, jsonStringifyReplacerSortKeys);
119120
}
120121

121122
module.exports = {

src/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ const jsonStringifyReplacerSortKeys = (_, value) => {
104104
return sorted;
105105
};
106106

107-
return value instanceof Object && !(value instanceof Array)
108-
? Object.keys(value).sort().reduce(insert, {})
109-
: value;
107+
if (value instanceof Object && !(value instanceof Array)) {
108+
let sorted = Object.keys(value).sort().reduce(insert, {});
109+
Object.keys(value).forEach((key) => delete value[key]);
110+
Object.assign(value, sorted);
111+
}
112+
113+
return value;
110114
};
111115

112116
module.exports = {

test/circular-plugin.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pack from './utils/pack';
2+
3+
describe('circular plugin', () => {
4+
it('should support plugins with circular configs', async () => {
5+
const plugin = {
6+
configs: {},
7+
rules: {},
8+
processors: {},
9+
};
10+
11+
Object.assign(plugin.configs, {
12+
recommended: {
13+
plugins: {
14+
self: plugin,
15+
},
16+
rules: {},
17+
},
18+
});
19+
20+
const loaderOptions = {
21+
configType: 'flat',
22+
overrideConfig: {
23+
plugins: { plugin: plugin },
24+
},
25+
overrideConfigFile: true,
26+
};
27+
28+
const compiler = pack('good', loaderOptions);
29+
30+
const stats = await compiler.runAsync();
31+
expect(stats.hasWarnings()).toBe(false);
32+
expect(stats.hasErrors()).toBe(false);
33+
});
34+
});

0 commit comments

Comments
 (0)