Skip to content

Commit b965c61

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 b965c61

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

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: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,45 @@
1010
import RulesOfHooks from './RulesOfHooks';
1111
import ExhaustiveDeps from './ExhaustiveDeps';
1212

13+
const {name, version} = require('../package.json');
14+
15+
// All rules
16+
export const rules = {
17+
'rules-of-hooks': RulesOfHooks,
18+
'exhaustive-deps': ExhaustiveDeps,
19+
};
20+
21+
// Rule configs
22+
const configRules = {
23+
'react-hooks/rules-of-hooks': 'error',
24+
'react-hooks/exhaustive-deps': 'warn',
25+
};
26+
27+
// Legacy configs
1328
export const configs = {
1429
recommended: {
1530
plugins: ['react-hooks'],
16-
rules: {
17-
'react-hooks/rules-of-hooks': 'error',
18-
'react-hooks/exhaustive-deps': 'warn',
19-
},
31+
rules: configRules,
2032
},
2133
};
2234

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

0 commit comments

Comments
 (0)