3
3
*
4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @jest -environment node
6
8
*/
7
9
8
10
'use strict' ;
9
11
10
- const ESLintTester = require ( 'eslint' ) . RuleTester ;
12
+ const ESLintTesterV7 = require ( 'eslint-v7' ) . RuleTester ;
13
+ const ESLintTesterV9 = require ( 'eslint-v9' ) . RuleTester ;
11
14
const ReactHooksESLintPlugin = require ( 'eslint-plugin-react-hooks' ) ;
12
15
const ReactHooksESLintRule = ReactHooksESLintPlugin . rules [ 'exhaustive-deps' ] ;
13
16
@@ -4673,17 +4676,8 @@ const tests = {
4673
4676
return <div ref={myRef} />;
4674
4677
}
4675
4678
` ,
4676
- output : `
4677
- function MyComponent() {
4678
- const myRef = useRef();
4679
- useLayoutEffect_SAFE_FOR_SSR(() => {
4680
- const handleMove = () => {};
4681
- myRef.current.addEventListener('mousemove', handleMove);
4682
- return () => myRef.current.removeEventListener('mousemove', handleMove);
4683
- });
4684
- return <div ref={myRef} />;
4685
- }
4686
- ` ,
4679
+ // No changes
4680
+ output : null ,
4687
4681
errors : [
4688
4682
`The ref value 'myRef.current' will likely have changed by the time ` +
4689
4683
`this effect cleanup function runs. If this ref points to a node ` +
@@ -7101,6 +7095,19 @@ const tests = {
7101
7095
message :
7102
7096
"React Hook useEffect has a missing dependency: 'local'. " +
7103
7097
'Either include it or remove the dependency array.' ,
7098
+ suggestions : [
7099
+ {
7100
+ desc : 'Update the dependencies array to be: [local]' ,
7101
+ output : normalizeIndent `
7102
+ function MyComponent() {
7103
+ const local = {};
7104
+ useEffect(() => {
7105
+ console.log(local);
7106
+ }, [local]);
7107
+ }
7108
+ ` ,
7109
+ } ,
7110
+ ] ,
7104
7111
} ,
7105
7112
] ,
7106
7113
// Keep this until major IDEs and VS Code FB ESLint plugin support Suggestions API.
@@ -8217,30 +8224,45 @@ if (!process.env.CI) {
8217
8224
testsTypescript . invalid = testsTypescript . invalid . filter ( predicate ) ;
8218
8225
}
8219
8226
8220
- describe ( 'react- hooks' , ( ) => {
8221
- const parserOptions = {
8227
+ describe ( 'rules-of- hooks/exhaustive-deps ' , ( ) => {
8228
+ const parserOptionsV7 = {
8222
8229
ecmaFeatures : {
8223
8230
jsx : true ,
8224
8231
} ,
8225
8232
ecmaVersion : 6 ,
8226
8233
sourceType : 'module' ,
8227
8234
} ;
8235
+ const languageOptionsV9 = {
8236
+ ecmaVersion : 6 ,
8237
+ sourceType : 'module' ,
8238
+ parserOptions : {
8239
+ ecmaFeatures : {
8240
+ jsx : true ,
8241
+ } ,
8242
+ } ,
8243
+ } ;
8228
8244
8229
8245
const testsBabelEslint = {
8230
8246
valid : [ ...testsFlow . valid , ...tests . valid ] ,
8231
8247
invalid : [ ...testsFlow . invalid , ...tests . invalid ] ,
8232
8248
} ;
8233
8249
8234
- new ESLintTester ( {
8250
+ new ESLintTesterV7 ( {
8235
8251
parser : require . resolve ( 'babel-eslint' ) ,
8236
- parserOptions,
8237
- } ) . run ( 'parser: babel-eslint' , ReactHooksESLintRule , testsBabelEslint ) ;
8252
+ parserOptions : parserOptionsV7 ,
8253
+ } ) . run (
8254
+ 'eslint: v7, parser: babel-eslint' ,
8255
+ ReactHooksESLintRule ,
8256
+ testsBabelEslint
8257
+ ) ;
8238
8258
8239
- new ESLintTester ( {
8240
- parser : require . resolve ( '@babel/eslint-parser' ) ,
8241
- parserOptions,
8259
+ new ESLintTesterV9 ( {
8260
+ languageOptions : {
8261
+ ...languageOptionsV9 ,
8262
+ parser : require ( '@babel/eslint-parser' ) ,
8263
+ } ,
8242
8264
} ) . run (
8243
- 'parser: @babel/eslint-parser' ,
8265
+ 'eslint: v9, parser: @babel/eslint-parser' ,
8244
8266
ReactHooksESLintRule ,
8245
8267
testsBabelEslint
8246
8268
) ;
@@ -8250,49 +8272,119 @@ describe('react-hooks', () => {
8250
8272
invalid : [ ...testsTypescript . invalid , ...tests . invalid ] ,
8251
8273
} ;
8252
8274
8253
- new ESLintTester ( {
8275
+ new ESLintTesterV7 ( {
8254
8276
parser : require . resolve ( '@typescript-eslint/parser-v2' ) ,
8255
- parserOptions,
8277
+ parserOptions : parserOptionsV7 ,
8278
+ } ) . run (
8279
+ 'eslint: v7, parser: @typescript-eslint/parser@2.x' ,
8280
+ ReactHooksESLintRule ,
8281
+ testsTypescriptEslintParser
8282
+ ) ;
8283
+
8284
+ new ESLintTesterV9 ( {
8285
+ languageOptions : {
8286
+ ...languageOptionsV9 ,
8287
+ parser : require ( '@typescript-eslint/parser-v2' ) ,
8288
+ } ,
8256
8289
} ) . run (
8257
- 'parser: @typescript-eslint/parser@2.x' ,
8290
+ 'eslint: v9, parser: @typescript-eslint/parser@2.x' ,
8258
8291
ReactHooksESLintRule ,
8259
8292
testsTypescriptEslintParser
8260
8293
) ;
8261
8294
8262
- new ESLintTester ( {
8295
+ new ESLintTesterV7 ( {
8263
8296
parser : require . resolve ( '@typescript-eslint/parser-v3' ) ,
8264
- parserOptions,
8297
+ parserOptions : parserOptionsV7 ,
8265
8298
} ) . run (
8266
- 'parser: @typescript-eslint/parser@3.x' ,
8299
+ 'eslint: v7, parser: @typescript-eslint/parser@3.x' ,
8267
8300
ReactHooksESLintRule ,
8268
8301
testsTypescriptEslintParser
8269
8302
) ;
8270
8303
8271
- new ESLintTester ( {
8304
+ new ESLintTesterV9 ( {
8305
+ languageOptions : {
8306
+ ...languageOptionsV9 ,
8307
+ parser : require ( '@typescript-eslint/parser-v3' ) ,
8308
+ } ,
8309
+ } ) . run (
8310
+ 'eslint: v9, parser: @typescript-eslint/parser@3.x' ,
8311
+ ReactHooksESLintRule ,
8312
+ testsTypescriptEslintParser
8313
+ ) ;
8314
+
8315
+ new ESLintTesterV7 ( {
8272
8316
parser : require . resolve ( '@typescript-eslint/parser-v4' ) ,
8273
- parserOptions,
8274
- } ) . run ( 'parser: @typescript-eslint/parser@4.x' , ReactHooksESLintRule , {
8275
- valid : [
8276
- ...testsTypescriptEslintParserV4 . valid ,
8277
- ...testsTypescriptEslintParser . valid ,
8278
- ] ,
8279
- invalid : [
8280
- ...testsTypescriptEslintParserV4 . invalid ,
8281
- ...testsTypescriptEslintParser . invalid ,
8282
- ] ,
8283
- } ) ;
8317
+ parserOptions : parserOptionsV7 ,
8318
+ } ) . run (
8319
+ 'eslint: v7, parser: @typescript-eslint/parser@4.x' ,
8320
+ ReactHooksESLintRule ,
8321
+ {
8322
+ valid : [
8323
+ ...testsTypescriptEslintParserV4 . valid ,
8324
+ ...testsTypescriptEslintParser . valid ,
8325
+ ] ,
8326
+ invalid : [
8327
+ ...testsTypescriptEslintParserV4 . invalid ,
8328
+ ...testsTypescriptEslintParser . invalid ,
8329
+ ] ,
8330
+ }
8331
+ ) ;
8284
8332
8285
- new ESLintTester ( {
8333
+ new ESLintTesterV9 ( {
8334
+ languageOptions : {
8335
+ ...languageOptionsV9 ,
8336
+ parser : require ( '@typescript-eslint/parser-v4' ) ,
8337
+ } ,
8338
+ } ) . run (
8339
+ 'eslint: v9, parser: @typescript-eslint/parser@4.x' ,
8340
+ ReactHooksESLintRule ,
8341
+ {
8342
+ valid : [
8343
+ ...testsTypescriptEslintParserV4 . valid ,
8344
+ ...testsTypescriptEslintParser . valid ,
8345
+ ] ,
8346
+ invalid : [
8347
+ ...testsTypescriptEslintParserV4 . invalid ,
8348
+ ...testsTypescriptEslintParser . invalid ,
8349
+ ] ,
8350
+ }
8351
+ ) ;
8352
+
8353
+ new ESLintTesterV7 ( {
8286
8354
parser : require . resolve ( '@typescript-eslint/parser-v5' ) ,
8287
- parserOptions,
8288
- } ) . run ( 'parser: @typescript-eslint/parser@^5.0.0-0' , ReactHooksESLintRule , {
8289
- valid : [
8290
- ...testsTypescriptEslintParserV4 . valid ,
8291
- ...testsTypescriptEslintParser . valid ,
8292
- ] ,
8293
- invalid : [
8294
- ...testsTypescriptEslintParserV4 . invalid ,
8295
- ...testsTypescriptEslintParser . invalid ,
8296
- ] ,
8297
- } ) ;
8355
+ parserOptions : parserOptionsV7 ,
8356
+ } ) . run (
8357
+ 'eslint: v7, parser: @typescript-eslint/parser@^5.0.0-0' ,
8358
+ ReactHooksESLintRule ,
8359
+ {
8360
+ valid : [
8361
+ ...testsTypescriptEslintParserV4 . valid ,
8362
+ ...testsTypescriptEslintParser . valid ,
8363
+ ] ,
8364
+ invalid : [
8365
+ ...testsTypescriptEslintParserV4 . invalid ,
8366
+ ...testsTypescriptEslintParser . invalid ,
8367
+ ] ,
8368
+ }
8369
+ ) ;
8370
+
8371
+ new ESLintTesterV9 ( {
8372
+ languageOptions : {
8373
+ ...languageOptionsV9 ,
8374
+ parser : require ( '@typescript-eslint/parser-v5' ) ,
8375
+ } ,
8376
+ } ) . run (
8377
+ 'eslint: v9, parser: @typescript-eslint/parser@^5.0.0-0' ,
8378
+ ReactHooksESLintRule ,
8379
+ {
8380
+ valid : [
8381
+ ...testsTypescriptEslintParserV4 . valid ,
8382
+ ...testsTypescriptEslintParser . valid ,
8383
+ ] ,
8384
+ invalid : [
8385
+ ...testsTypescriptEslintParserV4 . invalid ,
8386
+ ...testsTypescriptEslintParser . invalid ,
8387
+ ] ,
8388
+ }
8389
+ ) ;
8298
8390
} ) ;
0 commit comments