-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eslint-plugin-react-hooks: Add support for ESLint v9 (#28773)
- Loading branch information
Showing
6 changed files
with
618 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,14 @@ | |
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ESLintTester = require('eslint').RuleTester; | ||
const ESLintTesterV7 = require('eslint-v7').RuleTester; | ||
const ESLintTesterV9 = require('eslint-v9').RuleTester; | ||
const ReactHooksESLintPlugin = require('eslint-plugin-react-hooks'); | ||
const ReactHooksESLintRule = ReactHooksESLintPlugin.rules['exhaustive-deps']; | ||
|
||
|
@@ -4673,17 +4676,8 @@ const tests = { | |
return <div ref={myRef} />; | ||
} | ||
`, | ||
output: ` | ||
function MyComponent() { | ||
const myRef = useRef(); | ||
useLayoutEffect_SAFE_FOR_SSR(() => { | ||
const handleMove = () => {}; | ||
myRef.current.addEventListener('mousemove', handleMove); | ||
return () => myRef.current.removeEventListener('mousemove', handleMove); | ||
}); | ||
return <div ref={myRef} />; | ||
} | ||
`, | ||
// No changes | ||
output: null, | ||
errors: [ | ||
`The ref value 'myRef.current' will likely have changed by the time ` + | ||
`this effect cleanup function runs. If this ref points to a node ` + | ||
|
@@ -7101,6 +7095,19 @@ const tests = { | |
message: | ||
"React Hook useEffect has a missing dependency: 'local'. " + | ||
'Either include it or remove the dependency array.', | ||
suggestions: [ | ||
{ | ||
desc: 'Update the dependencies array to be: [local]', | ||
output: normalizeIndent` | ||
function MyComponent() { | ||
const local = {}; | ||
useEffect(() => { | ||
console.log(local); | ||
}, [local]); | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
// Keep this until major IDEs and VS Code FB ESLint plugin support Suggestions API. | ||
|
@@ -8217,30 +8224,45 @@ if (!process.env.CI) { | |
testsTypescript.invalid = testsTypescript.invalid.filter(predicate); | ||
} | ||
|
||
describe('react-hooks', () => { | ||
const parserOptions = { | ||
describe('rules-of-hooks/exhaustive-deps', () => { | ||
const parserOptionsV7 = { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}; | ||
const languageOptionsV9 = { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}; | ||
|
||
const testsBabelEslint = { | ||
valid: [...testsFlow.valid, ...tests.valid], | ||
invalid: [...testsFlow.invalid, ...tests.invalid], | ||
}; | ||
|
||
new ESLintTester({ | ||
new ESLintTesterV7({ | ||
parser: require.resolve('babel-eslint'), | ||
parserOptions, | ||
}).run('parser: babel-eslint', ReactHooksESLintRule, testsBabelEslint); | ||
parserOptions: parserOptionsV7, | ||
}).run( | ||
'eslint: v7, parser: babel-eslint', | ||
ReactHooksESLintRule, | ||
testsBabelEslint | ||
); | ||
|
||
new ESLintTester({ | ||
parser: require.resolve('@babel/eslint-parser'), | ||
parserOptions, | ||
new ESLintTesterV9({ | ||
languageOptions: { | ||
...languageOptionsV9, | ||
parser: require('@babel/eslint-parser'), | ||
}, | ||
}).run( | ||
'parser: @babel/eslint-parser', | ||
'eslint: v9, parser: @babel/eslint-parser', | ||
ReactHooksESLintRule, | ||
testsBabelEslint | ||
); | ||
|
@@ -8250,49 +8272,119 @@ describe('react-hooks', () => { | |
invalid: [...testsTypescript.invalid, ...tests.invalid], | ||
}; | ||
|
||
new ESLintTester({ | ||
new ESLintTesterV7({ | ||
parser: require.resolve('@typescript-eslint/parser-v2'), | ||
parserOptions, | ||
parserOptions: parserOptionsV7, | ||
}).run( | ||
'eslint: v7, parser: @typescript-eslint/[email protected]', | ||
ReactHooksESLintRule, | ||
testsTypescriptEslintParser | ||
); | ||
|
||
new ESLintTesterV9({ | ||
languageOptions: { | ||
...languageOptionsV9, | ||
parser: require('@typescript-eslint/parser-v2'), | ||
}, | ||
}).run( | ||
'parser: @typescript-eslint/[email protected]', | ||
'eslint: v9, parser: @typescript-eslint/[email protected]', | ||
ReactHooksESLintRule, | ||
testsTypescriptEslintParser | ||
); | ||
|
||
new ESLintTester({ | ||
new ESLintTesterV7({ | ||
parser: require.resolve('@typescript-eslint/parser-v3'), | ||
parserOptions, | ||
parserOptions: parserOptionsV7, | ||
}).run( | ||
'parser: @typescript-eslint/[email protected]', | ||
'eslint: v7, parser: @typescript-eslint/[email protected]', | ||
ReactHooksESLintRule, | ||
testsTypescriptEslintParser | ||
); | ||
|
||
new ESLintTester({ | ||
new ESLintTesterV9({ | ||
languageOptions: { | ||
...languageOptionsV9, | ||
parser: require('@typescript-eslint/parser-v3'), | ||
}, | ||
}).run( | ||
'eslint: v9, parser: @typescript-eslint/[email protected]', | ||
ReactHooksESLintRule, | ||
testsTypescriptEslintParser | ||
); | ||
|
||
new ESLintTesterV7({ | ||
parser: require.resolve('@typescript-eslint/parser-v4'), | ||
parserOptions, | ||
}).run('parser: @typescript-eslint/[email protected]', ReactHooksESLintRule, { | ||
valid: [ | ||
...testsTypescriptEslintParserV4.valid, | ||
...testsTypescriptEslintParser.valid, | ||
], | ||
invalid: [ | ||
...testsTypescriptEslintParserV4.invalid, | ||
...testsTypescriptEslintParser.invalid, | ||
], | ||
}); | ||
parserOptions: parserOptionsV7, | ||
}).run( | ||
'eslint: v7, parser: @typescript-eslint/[email protected]', | ||
ReactHooksESLintRule, | ||
{ | ||
valid: [ | ||
...testsTypescriptEslintParserV4.valid, | ||
...testsTypescriptEslintParser.valid, | ||
], | ||
invalid: [ | ||
...testsTypescriptEslintParserV4.invalid, | ||
...testsTypescriptEslintParser.invalid, | ||
], | ||
} | ||
); | ||
|
||
new ESLintTester({ | ||
new ESLintTesterV9({ | ||
languageOptions: { | ||
...languageOptionsV9, | ||
parser: require('@typescript-eslint/parser-v4'), | ||
}, | ||
}).run( | ||
'eslint: v9, parser: @typescript-eslint/[email protected]', | ||
ReactHooksESLintRule, | ||
{ | ||
valid: [ | ||
...testsTypescriptEslintParserV4.valid, | ||
...testsTypescriptEslintParser.valid, | ||
], | ||
invalid: [ | ||
...testsTypescriptEslintParserV4.invalid, | ||
...testsTypescriptEslintParser.invalid, | ||
], | ||
} | ||
); | ||
|
||
new ESLintTesterV7({ | ||
parser: require.resolve('@typescript-eslint/parser-v5'), | ||
parserOptions, | ||
}).run('parser: @typescript-eslint/parser@^5.0.0-0', ReactHooksESLintRule, { | ||
valid: [ | ||
...testsTypescriptEslintParserV4.valid, | ||
...testsTypescriptEslintParser.valid, | ||
], | ||
invalid: [ | ||
...testsTypescriptEslintParserV4.invalid, | ||
...testsTypescriptEslintParser.invalid, | ||
], | ||
}); | ||
parserOptions: parserOptionsV7, | ||
}).run( | ||
'eslint: v7, parser: @typescript-eslint/parser@^5.0.0-0', | ||
ReactHooksESLintRule, | ||
{ | ||
valid: [ | ||
...testsTypescriptEslintParserV4.valid, | ||
...testsTypescriptEslintParser.valid, | ||
], | ||
invalid: [ | ||
...testsTypescriptEslintParserV4.invalid, | ||
...testsTypescriptEslintParser.invalid, | ||
], | ||
} | ||
); | ||
|
||
new ESLintTesterV9({ | ||
languageOptions: { | ||
...languageOptionsV9, | ||
parser: require('@typescript-eslint/parser-v5'), | ||
}, | ||
}).run( | ||
'eslint: v9, parser: @typescript-eslint/parser@^5.0.0-0', | ||
ReactHooksESLintRule, | ||
{ | ||
valid: [ | ||
...testsTypescriptEslintParserV4.valid, | ||
...testsTypescriptEslintParser.valid, | ||
], | ||
invalid: [ | ||
...testsTypescriptEslintParserV4.invalid, | ||
...testsTypescriptEslintParser.invalid, | ||
], | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.