Skip to content

Commit 32960d3

Browse files
authored
Fix: Error while loading configs with custom plugins (#805)
1 parent 6b4751c commit 32960d3

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

‎lib/xo.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ export class Xo {
166166

167167
this.xoConfig = [
168168
this.baseXoConfig,
169-
// Ensure resolved options do not mutate between runs
170-
...structuredClone(flatOptions),
169+
...flatOptions,
171170
];
172171

173172
// Split off the TS rules in a special case, so that you won't get errors
@@ -210,13 +209,16 @@ export class Xo {
210209
// Apply TS rules to all files
211210
tsConfig.files = [tsFilesGlob];
212211

213-
// Set the other rules to the original config
214-
config.rules = Object.fromEntries(otherRules);
212+
const otherConfig: XoConfigItem = {
213+
...config,
214+
// Set the other rules to the original config
215+
rules: Object.fromEntries(otherRules),
216+
};
215217

216218
// These rules should still apply to all files
217-
config.files = [allFilesGlob];
219+
otherConfig.files = [allFilesGlob];
218220

219-
return [tsConfig, config];
221+
return [tsConfig, otherConfig];
220222
});
221223

222224
this.prettier = this.xoConfig.some(config => config.prettier);

‎test/xo/lint-text.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,50 @@ test('lint-text can be ran multiple times in a row with top level typescript rul
404404
t.is(results3[0]?.errorCount, 0);
405405
t.true(results3[0]?.messages?.length === 0);
406406
});
407+
408+
test('config with custom plugin', async t => {
409+
const {cwd} = t.context;
410+
411+
await fs.writeFile(
412+
path.join(cwd, 'xo.config.js'),
413+
dedent`
414+
const testRule = {
415+
create(context) {
416+
return {
417+
Program(node) {
418+
context.report({
419+
node,
420+
message: 'Custom error',
421+
});
422+
},
423+
};
424+
},
425+
};
426+
427+
export default [
428+
{
429+
plugins: {
430+
'test-plugin': {
431+
rules: {
432+
'test-rule': testRule,
433+
},
434+
},
435+
},
436+
rules: {
437+
'test-plugin/test-rule': 'error',
438+
},
439+
},
440+
];\n
441+
`,
442+
'utf8',
443+
);
444+
445+
const {results} = await Xo.lintText(dedent`console.log('hello');\n`, {
446+
cwd,
447+
filePath: path.join(cwd, 'test.js'),
448+
});
449+
450+
t.is(results[0]?.messages?.length, 1);
451+
t.is(results[0]?.messages[0]?.ruleId, 'test-plugin/test-rule');
452+
t.is(results[0]?.messages[0]?.message, 'Custom error');
453+
});

0 commit comments

Comments
 (0)