Skip to content
Open
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
40 changes: 20 additions & 20 deletions packages/config-helpers/src/define-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ function findPluginConfig(config, pluginConfigName) {
}

const directConfig = plugin.configs?.[configName];

// Prefer direct config, but fall back to flat config if available
if (directConfig) {
// Arrays are always flat configs, and non-legacy configs can be used directly
if (Array.isArray(directConfig) || !isLegacyConfig(directConfig)) {
Expand All @@ -261,30 +263,28 @@ function findPluginConfig(config, pluginConfigName) {
pluginConfigName,
);
}
}

// If it's a legacy config, look for the flat version
const flatConfig = plugin.configs?.[`flat/${configName}`];

if (
flatConfig &&
(Array.isArray(flatConfig) || !isLegacyConfig(flatConfig))
) {
return deepNormalizePluginConfig(
userPluginNamespace,
plugin,
flatConfig,
pluginConfigName,
);
}

throw new TypeError(
`Plugin config "${configName}" in plugin "${userPluginNamespace}" is an eslintrc config and cannot be used in this context.`,
// If it's a legacy config, or the config does not exist => look for the flat version
const flatConfig = plugin.configs?.[`flat/${configName}`];
if (
flatConfig &&
(Array.isArray(flatConfig) || !isLegacyConfig(flatConfig))
) {
return deepNormalizePluginConfig(
userPluginNamespace,
plugin,
flatConfig,
pluginConfigName,
);
}

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

/**
Expand Down
31 changes: 31 additions & 0 deletions packages/config-helpers/tests/define-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,37 @@ describe("defineConfig()", () => {
]);
});

it("should use flat config when eslintrc does not exist", () => {
const testPlugin = {
configs: {
"flat/recommended": {
rules: { "no-console": "error" },
},
},
};

const config = defineConfig({
plugins: {
test: testPlugin,
},
extends: ["test/recommended"],
rules: {
"no-debugger": "error",
},
});

assert.deepStrictEqual(config, [
{
name: "UserConfig[0] > test/recommended",
rules: { "no-console": "error" },
},
{
plugins: { test: testPlugin },
rules: { "no-debugger": "error" },
},
]);
});

it("should throw error when both base and flat configs are legacy", () => {
const testPlugin = {
configs: {
Expand Down
Loading