Skip to content

Commit 0030784

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 dc32c7f commit 0030784

File tree

5 files changed

+163
-22
lines changed

5 files changed

+163
-22
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: 91 additions & 14 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"
@@ -7366,7 +7382,7 @@ eslint-utils@^2.0.0, eslint-utils@^2.1.0:
73667382
dependencies:
73677383
eslint-visitor-keys "^1.1.0"
73687384

7369-
"eslint-v7@npm:eslint@^7.7.0", eslint@^7.7.0:
7385+
"eslint-v7@npm:eslint@^7.7.0":
73707386
version "7.32.0"
73717387
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.32.0.tgz#c6d328a14be3fb08c8d1d21e12c02fdb7a2a812d"
73727388
integrity sha512-VHZ8gX+EDfz+97jGcgyGCyRia/dPOd6Xh9yPv8Bl1+SoaIwD+a/vlrOmGRUyOYu7MwUhc7CxqeaDZU13S4+EpA==
@@ -7526,6 +7542,52 @@ [email protected]:
75267542
strip-ansi "^6.0.1"
75277543
text-table "^0.2.0"
75287544

7545+
eslint@^7.7.0:
7546+
version "7.32.0"
7547+
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.32.0.tgz#c6d328a14be3fb08c8d1d21e12c02fdb7a2a812d"
7548+
integrity sha512-VHZ8gX+EDfz+97jGcgyGCyRia/dPOd6Xh9yPv8Bl1+SoaIwD+a/vlrOmGRUyOYu7MwUhc7CxqeaDZU13S4+EpA==
7549+
dependencies:
7550+
"@babel/code-frame" "7.12.11"
7551+
"@eslint/eslintrc" "^0.4.3"
7552+
"@humanwhocodes/config-array" "^0.5.0"
7553+
ajv "^6.10.0"
7554+
chalk "^4.0.0"
7555+
cross-spawn "^7.0.2"
7556+
debug "^4.0.1"
7557+
doctrine "^3.0.0"
7558+
enquirer "^2.3.5"
7559+
escape-string-regexp "^4.0.0"
7560+
eslint-scope "^5.1.1"
7561+
eslint-utils "^2.1.0"
7562+
eslint-visitor-keys "^2.0.0"
7563+
espree "^7.3.1"
7564+
esquery "^1.4.0"
7565+
esutils "^2.0.2"
7566+
fast-deep-equal "^3.1.3"
7567+
file-entry-cache "^6.0.1"
7568+
functional-red-black-tree "^1.0.1"
7569+
glob-parent "^5.1.2"
7570+
globals "^13.6.0"
7571+
ignore "^4.0.6"
7572+
import-fresh "^3.0.0"
7573+
imurmurhash "^0.1.4"
7574+
is-glob "^4.0.0"
7575+
js-yaml "^3.13.1"
7576+
json-stable-stringify-without-jsonify "^1.0.1"
7577+
levn "^0.4.1"
7578+
lodash.merge "^4.6.2"
7579+
minimatch "^3.0.4"
7580+
natural-compare "^1.4.0"
7581+
optionator "^0.9.1"
7582+
progress "^2.0.0"
7583+
regexpp "^3.1.0"
7584+
semver "^7.2.1"
7585+
strip-ansi "^6.0.0"
7586+
strip-json-comments "^3.1.0"
7587+
table "^6.0.9"
7588+
text-table "^0.2.0"
7589+
v8-compile-cache "^2.0.3"
7590+
75297591
[email protected], espree@^10.0.1:
75307592
version "10.0.1"
75317593
resolved "https://registry.yarnpkg.com/espree/-/espree-10.0.1.tgz#600e60404157412751ba4a6f3a2ee1a42433139f"
@@ -11347,11 +11409,6 @@ [email protected]:
1134711409
resolved "https://registry.yarnpkg.com/lodash.omitby/-/lodash.omitby-4.6.0.tgz#5c15ff4754ad555016b53c041311e8f079204791"
1134811410
integrity sha1-XBX/R1StVVAWtTwEExHo8HkgR5E=
1134911411

11350-
lodash.throttle@^4.1.1:
11351-
version "4.1.1"
11352-
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
11353-
integrity sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ=
11354-
1135511412
lodash.truncate@^4.4.2:
1135611413
version "4.4.2"
1135711414
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
@@ -11603,11 +11660,6 @@ memfs@^3.4.3:
1160311660
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.2.1.tgz#8337aa3c4335581839ec01c3d594090cebe8f00e"
1160411661
integrity sha512-zYiwtZUcYyXKo/np96AGZAckk+FWWsUdJ3cHGGmld7+AhvcWmQyGCYUh1hc4Q/pkOhb65dQR/pqCyK0cOaHz4Q==
1160511662

11606-
memoize-one@^3.1.1:
11607-
version "3.1.1"
11608-
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-3.1.1.tgz#ef609811e3bc28970eac2884eece64d167830d17"
11609-
integrity sha512-YqVh744GsMlZu6xkhGslPSqSurOv6P+kLN2J3ysBZfagLcL5FdRK/0UpgLoL8hwjjEvvAVkjJZyFP+1T6p1vgA==
11610-
1161111663
memoize-one@^5.1.1:
1161211664
version "5.1.1"
1161311665
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.1.1.tgz#047b6e3199b508eaec03504de71229b8eb1d75c0"
@@ -15093,7 +15145,7 @@ string-natural-compare@^3.0.1:
1509315145
resolved "https://registry.yarnpkg.com/string-natural-compare/-/string-natural-compare-3.0.1.tgz#7a42d58474454963759e8e8b7ae63d71c1e7fdf4"
1509415146
integrity sha512-n3sPwynL1nwKi3WJ6AIsClwBMa0zTi54fn2oLU6ndfTSIO05xaznjSf15PcBZU6FNWbmN5Q6cxT4V5hGvB4taw==
1509515147

15096-
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
15148+
"string-width-cjs@npm:string-width@^4.2.0":
1509715149
version "4.2.3"
1509815150
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
1509915151
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -15128,6 +15180,15 @@ string-width@^4.0.0:
1512815180
is-fullwidth-code-point "^3.0.0"
1512915181
strip-ansi "^6.0.0"
1513015182

15183+
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
15184+
version "4.2.3"
15185+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
15186+
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
15187+
dependencies:
15188+
emoji-regex "^8.0.0"
15189+
is-fullwidth-code-point "^3.0.0"
15190+
strip-ansi "^6.0.1"
15191+
1513115192
string-width@^5.0.1, string-width@^5.1.2:
1513215193
version "5.1.2"
1513315194
resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794"
@@ -15188,7 +15249,7 @@ string_decoder@~1.1.1:
1518815249
dependencies:
1518915250
safe-buffer "~5.1.0"
1519015251

15191-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
15252+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
1519215253
version "6.0.1"
1519315254
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
1519415255
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -15216,6 +15277,13 @@ strip-ansi@^5.1.0:
1521615277
dependencies:
1521715278
ansi-regex "^4.1.0"
1521815279

15280+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
15281+
version "6.0.1"
15282+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
15283+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
15284+
dependencies:
15285+
ansi-regex "^5.0.1"
15286+
1521915287
strip-ansi@^7.0.1:
1522015288
version "7.1.0"
1522115289
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -16683,7 +16751,7 @@ workerize-loader@^2.0.2:
1668316751
dependencies:
1668416752
loader-utils "^2.0.0"
1668516753

16686-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
16754+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
1668716755
version "7.0.0"
1668816756
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1668916757
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -16701,6 +16769,15 @@ wrap-ansi@^6.2.0:
1670116769
string-width "^4.1.0"
1670216770
strip-ansi "^6.0.0"
1670316771

16772+
wrap-ansi@^7.0.0:
16773+
version "7.0.0"
16774+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
16775+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
16776+
dependencies:
16777+
ansi-styles "^4.0.0"
16778+
string-width "^4.1.0"
16779+
strip-ansi "^6.0.0"
16780+
1670416781
wrap-ansi@^8.1.0:
1670516782
version "8.1.0"
1670616783
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"

0 commit comments

Comments
 (0)