Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] export flat configs from plugin root and fix flat config crash #3694

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 9 additions & 20 deletions README.md
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
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;
2 changes: 1 addition & 1 deletion lib/rules/index.js
Expand Up @@ -2,7 +2,7 @@

/* eslint global-require: 0 */

/** @type {Record<string, import('eslint').Rule.RuleModule>} */
/** @satisfies {Record<string, import('eslint').Rule.RuleModule>} */
module.exports = {
'boolean-prop-naming': require('./boolean-prop-naming'),
'button-has-type': require('./button-has-type'),
Expand Down