Skip to content

Commit 4c484ec

Browse files
committed
feat: support flat config
This change adds support for the new flat config format. In order to import from json, I had to add the rollup json plugin to the rollup build script. I also updated the README to include usage examples. Example usage: ```js export default [ { files: ['**/*.{js,jsx}'], languageOptions: { ecmaVersion: 2020, globals: globals.browser, parserOptions: { ecmaVersion: 'latest', ecmaFeatures: {jsx: true}, sourceType: 'module', }, }, settings: {react: {version: '18.3'}}, ...reactHooks.flatConfigs.recommended, }, ]; ```
1 parent 0911120 commit 4c484ec

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@babel/traverse": "^7.11.0",
3939
"@rollup/plugin-babel": "^6.0.3",
4040
"@rollup/plugin-commonjs": "^24.0.1",
41+
"@rollup/plugin-json": "^6.1.0",
4142
"@rollup/plugin-node-resolve": "^15.0.1",
4243
"@rollup/plugin-replace": "^5.0.2",
4344
"abortcontroller-polyfill": "^1.7.5",

packages/eslint-plugin-react-hooks/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ npm install eslint-plugin-react-hooks --save-dev
1818
yarn add eslint-plugin-react-hooks --dev
1919
```
2020

21-
Then extend the recommended eslint config:
21+
### Legacy Config (.eslintrc)
22+
23+
Extend the recommended eslint config:
2224

2325
```js
2426
{
@@ -29,10 +31,25 @@ Then extend the recommended eslint config:
2931
}
3032
```
3133

34+
### Flat Config (eslint.config.js)
35+
36+
Add the recommended config
37+
38+
```js
39+
import reactHooks from 'eslint-plugin-react-hooks';
40+
41+
export default [
42+
// ...
43+
reactHooks.flatConfigs.recommended,
44+
];
45+
```
46+
3247
### Custom Configuration
3348

3449
If you want more fine-grained configuration, you can instead add a snippet like this to your ESLint configuration file:
3550

51+
#### Legacy Config (.eslintrc)
52+
3653
```js
3754
{
3855
"plugins": [
@@ -47,6 +64,23 @@ If you want more fine-grained configuration, you can instead add a snippet like
4764
}
4865
```
4966

67+
#### Flat Config (eslint.config.js)
68+
69+
```js
70+
import reactHooks from 'eslint-plugin-react-hooks';
71+
72+
export default [
73+
{
74+
files: ['**/*.{js,jsx}'],
75+
plugins: { 'react-hooks': reactHooks },
76+
// ...
77+
rules: {
78+
'react-hooks/rules-of-hooks': 'error',
79+
'react-hooks/exhaustive-deps': 'warn',
80+
}
81+
},
82+
];
83+
```
5084

5185
## Advanced Configuration
5286

packages/eslint-plugin-react-hooks/src/index.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,45 @@
99

1010
import RulesOfHooks from './RulesOfHooks';
1111
import ExhaustiveDeps from './ExhaustiveDeps';
12+
import {name, version} from '../package.json';
1213

14+
// All rules
15+
export const rules = {
16+
'rules-of-hooks': RulesOfHooks,
17+
'exhaustive-deps': ExhaustiveDeps,
18+
};
19+
20+
// Rule configs
21+
const configRules = {
22+
'react-hooks/rules-of-hooks': 'error',
23+
'react-hooks/exhaustive-deps': 'warn',
24+
};
25+
26+
// Legacy configs
1327
export const configs = {
1428
recommended: {
1529
plugins: ['react-hooks'],
16-
rules: {
17-
'react-hooks/rules-of-hooks': 'error',
18-
'react-hooks/exhaustive-deps': 'warn',
19-
},
30+
rules: configRules,
2031
},
2132
};
2233

23-
export const rules = {
24-
'rules-of-hooks': RulesOfHooks,
25-
'exhaustive-deps': ExhaustiveDeps,
34+
// Base plugin object
35+
const reactHooksPlugin = {
36+
meta: {name, version},
37+
rules,
38+
};
39+
40+
// Flat configs
41+
export const flatConfigs = {
42+
recommended: {
43+
name: 'react-hooks/recommended',
44+
plugins: {'react-hooks': reactHooksPlugin},
45+
rules: configRules,
46+
},
47+
};
48+
49+
export default {
50+
...reactHooksPlugin,
51+
configs,
52+
flatConfigs,
2653
};

scripts/rollup/build.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const rollup = require('rollup');
44
const babel = require('@rollup/plugin-babel').babel;
55
const closure = require('./plugins/closure-plugin');
66
const flowRemoveTypes = require('flow-remove-types');
7+
const json = require('@rollup/plugin-json');
78
const prettier = require('rollup-plugin-prettier');
89
const replace = require('@rollup/plugin-replace');
910
const stripBanner = require('rollup-plugin-strip-banner');
@@ -377,6 +378,7 @@ function getPlugins(
377378
bundleType !== ESM_PROD && bundleType !== ESM_DEV;
378379

379380
return [
381+
json(),
380382
// Keep dynamic imports as externals
381383
dynamicImports(),
382384
{

yarn.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,13 @@
30213021
is-reference "1.2.1"
30223022
magic-string "^0.27.0"
30233023

3024+
"@rollup/plugin-json@^6.1.0":
3025+
version "6.1.0"
3026+
resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-6.1.0.tgz#fbe784e29682e9bb6dee28ea75a1a83702e7b805"
3027+
integrity sha512-EGI2te5ENk1coGeADSIwZ7G2Q8CJS2sF120T7jLw4xFw9n7wIOXHo+kIYRAoVpJAN+kmqZSoO3Fp4JtoNF4ReA==
3028+
dependencies:
3029+
"@rollup/pluginutils" "^5.1.0"
3030+
30243031
"@rollup/plugin-node-resolve@^15.0.1":
30253032
version "15.0.1"
30263033
resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.0.1.tgz#72be449b8e06f6367168d5b3cd5e2802e0248971"
@@ -3050,6 +3057,15 @@
30503057
estree-walker "^2.0.2"
30513058
picomatch "^2.3.1"
30523059

3060+
"@rollup/pluginutils@^5.1.0":
3061+
version "5.1.0"
3062+
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.1.0.tgz#7e53eddc8c7f483a4ad0b94afb1f7f5fd3c771e0"
3063+
integrity sha512-XTIWOPPcpvyKI6L1NHo0lFlCyznUEyPmPY1mc3KpPVDYulHSTvyeLNVW00QTLIAFNhR3kYnJTQHeGqU4M3n09g==
3064+
dependencies:
3065+
"@types/estree" "^1.0.0"
3066+
estree-walker "^2.0.2"
3067+
picomatch "^2.3.1"
3068+
30533069
"@sinclair/typebox@^0.25.16":
30543070
version "0.25.24"
30553071
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.25.24.tgz#8c7688559979f7079aacaf31aa881c3aa410b718"

0 commit comments

Comments
 (0)