Skip to content

Commit 833c4a3

Browse files
authored
feat: defineConfig() supports "flat/" config prefix (#19533)
fixes #19513
1 parent cc3bd00 commit 833c4a3

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

docs/src/extend/plugins.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,6 @@ module.exports = plugin;
260260
261261
This plugin exports a `recommended` config that is an array with one config object. When there is just one config object, you can also export just the object without an enclosing array.
262262
263-
::: tip
264-
Your plugin can export both current (flat config) and legacy (eslintrc) config objects in the `configs` key. When exporting legacy configs, we recommend prefixing the name with `"legacy-"` (for example, `"legacy-recommended"`) to make it clear how the config should be used.
265-
:::
266-
267263
In order to use a config from a plugin in a configuration file, import the plugin and use the `extends` key to reference the name of the config, like this:
268264
269265
```js
@@ -286,6 +282,77 @@ export default defineConfig([
286282
Plugins cannot force a specific configuration to be used. Users must manually include a plugin's configurations in their configuration file.
287283
:::
288284
285+
#### Backwards Compatibility for Legacy Configs
286+
287+
If your plugin needs to export configs that work both with the current (flat config) system and the old (eslintrc) system, you can export both config types from the `configs` key. When exporting legacy configs, we recommend prefixing the name with `"legacy-"` (for example, `"legacy-recommended"`) to make it clear how the config should be used.
288+
289+
If you're working on a plugin that has existed prior to ESLint v9.0.0, then you may already have legacy configs with names such as `"recommended"`. If you don't want to update the config name, you can also create an additional entry in the `configs` object prefixed with `"flat/"` (for example, `"flat/recommended"`). Here's an example:
290+
291+
```js
292+
const plugin = {
293+
meta: {
294+
name: "eslint-plugin-example",
295+
version: "1.2.3"
296+
},
297+
configs: {},
298+
rules: {
299+
"dollar-sign": {
300+
create(context) {
301+
// rule implementation ...
302+
}
303+
}
304+
}
305+
};
306+
307+
// assign configs here so we can reference `plugin`
308+
Object.assign(plugin.configs, {
309+
310+
// flat config format
311+
"flat/recommended": [{
312+
plugins: {
313+
example: plugin
314+
},
315+
rules: {
316+
"example/dollar-sign": "error"
317+
},
318+
languageOptions: {
319+
globals: {
320+
myGlobal: "readonly"
321+
},
322+
parserOptions: {
323+
ecmaFeatures: {
324+
jsx: true
325+
}
326+
}
327+
}
328+
}],
329+
330+
// eslintrc format
331+
recommended: {
332+
plugins: ["example"],
333+
rules: {
334+
"example/dollar-sign": "error"
335+
},
336+
globals: {
337+
myGlobal: "readonly"
338+
},
339+
parserOptions: {
340+
ecmaFeatures: {
341+
jsx: true
342+
}
343+
}
344+
}
345+
});
346+
347+
// for ESM
348+
export default plugin;
349+
350+
// OR for CommonJS
351+
module.exports = plugin;
352+
```
353+
354+
With this approach, both configuration systems recognize `"recommended"`. The old config system uses the `recommended` key while the current config system uses the `flat/recommended` key. The `defineConfig()` helper first looks at the `recommended` key, and if that is not in the correct format, it looks for the `flat/recommended` key. This allows you an upgrade path if you'd later like to rename `flat/recommended` to `recommended` when you no longer need to support the old config system.
355+
289356
## Testing a Plugin
290357
291358
ESLint provides the [`RuleTester`](../integrate/nodejs-api#ruletester) utility to make it easy to test the rules of your plugin.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"@eslint-community/eslint-utils": "^4.2.0",
110110
"@eslint-community/regexpp": "^4.12.1",
111111
"@eslint/config-array": "^0.19.2",
112-
"@eslint/config-helpers": "^0.1.0",
112+
"@eslint/config-helpers": "^0.2.0",
113113
"@eslint/core": "^0.12.0",
114114
"@eslint/eslintrc": "^3.3.0",
115115
"@eslint/js": "9.22.0",

0 commit comments

Comments
 (0)