Skip to content

Commit

Permalink
[Fix] export flat configs from plugin root and fix flat config crash
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher authored and ljharb committed Mar 3, 2024
1 parent 2124d13 commit e0df0de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
29 changes: 9 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,16 @@ Refer to the [official docs](https://eslint.org/docs/latest/user-guide/configuri
The schema of the `settings.react` object would be identical to that of what's already described above in the legacy config section.

<!-- markdownlint-disable-next-line no-duplicate-heading -->
### Shareable configs

There're also 3 shareable configs.

- `eslint-plugin-react/configs/all`
- `eslint-plugin-react/configs/recommended`
- `eslint-plugin-react/configs/jsx-runtime`

If your eslint.config.js is ESM, include the `.js` extension (e.g. `eslint-plugin-react/recommended.js`). Note that the next semver-major will require omitting the extension for these imports.

**Note**: These configurations will import `eslint-plugin-react` and enable JSX in [`languageOptions.parserOptions`](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#configuration-objects).
### Flat Configs

In the new config system, `plugin:` protocol(e.g. `plugin:react/recommended`) is no longer valid.
As eslint does not automatically import the preset config (shareable config), you explicitly do it by yourself.
The flat configs are available via the root plugin import. They will configure the plugin under the `react/` namespace and enable JSX in [`languageOptions.parserOptions`](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#configuration-objects).

```js
const reactRecommended = require('eslint-plugin-react/configs/recommended');
const reactPlugin = require('eslint-plugin-react');

module.exports = [
reactRecommended, // This is not a plugin object, but a shareable config object
reactPlugin.configs['flat/recommended'], // This is not a plugin object, but a shareable config object
];
```
Expand All @@ -234,16 +223,16 @@ You can of course add/override some properties.
For most of the cases, you probably want to configure some properties by yourself.

```js
const reactRecommended = require('eslint-plugin-react/configs/recommended');
const reactPlugin = require('eslint-plugin-react');
const globals = require('globals');

module.exports = [
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
...reactRecommended,
...reactPlugin.configs['flat/recommended'],
languageOptions: {
...reactRecommended.languageOptions,
...reactPlugin.configs['flat/recommended'].languageOptions,
globals: {
...globals.serviceworker,
...globals.browser,
Expand All @@ -257,14 +246,14 @@ module.exports = [
The above example is same as the example below, as the new config system is based on chaining.

```js
const reactRecommended = require('eslint-plugin-react/configs/recommended');
const reactPlugin = require('eslint-plugin-react');
const globals = require('globals');

module.exports = [
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
...reactRecommended,
...reactPlugin.configs['flat/recommended'],
},
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const plugins = [
'react',
];

module.exports = {
const plugin = {
deprecatedRules: configAll.plugins.react.deprecatedRules,
rules: allRules,
configs: {
Expand All @@ -27,5 +27,16 @@ module.exports = {
parserOptions: configRuntime.languageOptions.parserOptions,
plugins,
}),

'flat/recommended': Object.assign({}, configRecommended),
'flat/all': Object.assign({}, configAll),
'flat/jsx-runtime': Object.assign({}, configRuntime),
},
};

// need to ensure the flat configs reference the same plugin identity
plugin.configs['flat/recommended'].plugins.react = plugin;
plugin.configs['flat/all'].plugins.react = plugin;
plugin.configs['flat/recommended'].plugins.react = plugin;

module.exports = plugin;

0 comments on commit e0df0de

Please sign in to comment.