Skip to content

Commit ddc8577

Browse files
authored
fix: use flat config when eslintrc config does not exist (#288)
1 parent e1f55de commit ddc8577

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

packages/config-helpers/src/define-config.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ function findPluginConfig(config, pluginConfigName) {
256256
}
257257

258258
const directConfig = plugin.configs?.[configName];
259+
260+
// Prefer direct config, but fall back to flat config if available
259261
if (directConfig) {
260262
// Arrays are always flat configs, and non-legacy configs can be used directly
261263
if (Array.isArray(directConfig) || !isLegacyConfig(directConfig)) {
@@ -266,30 +268,28 @@ function findPluginConfig(config, pluginConfigName) {
266268
pluginConfigName,
267269
);
268270
}
271+
}
269272

270-
// If it's a legacy config, look for the flat version
271-
const flatConfig = plugin.configs?.[`flat/${configName}`];
272-
273-
if (
274-
flatConfig &&
275-
(Array.isArray(flatConfig) || !isLegacyConfig(flatConfig))
276-
) {
277-
return deepNormalizePluginConfig(
278-
userPluginNamespace,
279-
plugin,
280-
flatConfig,
281-
pluginConfigName,
282-
);
283-
}
284-
285-
throw new TypeError(
286-
`Plugin config "${configName}" in plugin "${userPluginNamespace}" is an eslintrc config and cannot be used in this context.`,
273+
// If it's a legacy config, or the config does not exist => look for the flat version
274+
const flatConfig = plugin.configs?.[`flat/${configName}`];
275+
if (
276+
flatConfig &&
277+
(Array.isArray(flatConfig) || !isLegacyConfig(flatConfig))
278+
) {
279+
return deepNormalizePluginConfig(
280+
userPluginNamespace,
281+
plugin,
282+
flatConfig,
283+
pluginConfigName,
287284
);
288285
}
289286

290-
throw new TypeError(
291-
`Plugin config "${configName}" not found in plugin "${userPluginNamespace}".`,
292-
);
287+
// If we get here, then the config was either not found or is a legacy config
288+
const message =
289+
directConfig || flatConfig
290+
? `Plugin config "${configName}" in plugin "${userPluginNamespace}" is an eslintrc config and cannot be used in this context.`
291+
: `Plugin config "${configName}" not found in plugin "${userPluginNamespace}".`;
292+
throw new TypeError(message);
293293
}
294294

295295
/**

packages/config-helpers/tests/define-config.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,22 @@ describe("defineConfig()", () => {
790790
});
791791
}, /Plugin config "config1" in plugin "test" is an eslintrc config and cannot be used in this context\./u);
792792
});
793+
it("should throw an error when a plugin config 'flat/recommended' is in eslintrc format", () => {
794+
const testPlugin = {
795+
configs: {
796+
"flat/recommended": { plugins: [] },
797+
},
798+
};
799+
800+
assert.throws(() => {
801+
defineConfig({
802+
plugins: {
803+
test: testPlugin,
804+
},
805+
extends: ["test/recommended"],
806+
});
807+
}, /Plugin config "recommended" in plugin "test" is an eslintrc config and cannot be used in this context\./u);
808+
});
793809

794810
it("should throw an error when a plugin config is in eslintrc format (`plugins` is an array)", () => {
795811
const testPlugin = {
@@ -845,6 +861,37 @@ describe("defineConfig()", () => {
845861
]);
846862
});
847863

864+
it("should use flat config when eslintrc does not exist", () => {
865+
const testPlugin = {
866+
configs: {
867+
"flat/recommended": {
868+
rules: { "no-console": "error" },
869+
},
870+
},
871+
};
872+
873+
const config = defineConfig({
874+
plugins: {
875+
test: testPlugin,
876+
},
877+
extends: ["test/recommended"],
878+
rules: {
879+
"no-debugger": "error",
880+
},
881+
});
882+
883+
assert.deepStrictEqual(config, [
884+
{
885+
name: "UserConfig[0] > test/recommended",
886+
rules: { "no-console": "error" },
887+
},
888+
{
889+
plugins: { test: testPlugin },
890+
rules: { "no-debugger": "error" },
891+
},
892+
]);
893+
});
894+
848895
it("should throw error when both base and flat configs are legacy", () => {
849896
const testPlugin = {
850897
configs: {

0 commit comments

Comments
 (0)