Skip to content

Commit 32f05c6

Browse files
authored
fix: don't error on circular eslint plugins (#277)
* fix: don't error on circular eslint plugins Replaces `JSON.stringify` with an implementation from `flatted` for cache keys. * fix: restrict circular plugin support to eslint ^9
1 parent 3761319 commit 32f05c6

File tree

5 files changed

+56
-12
lines changed

5 files changed

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

0 commit comments

Comments
 (0)