Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ESLint crash on empty react effect hook #20385

Merged
merged 5 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix ESLint crash on empty react effect hook
  • Loading branch information
ChrisRu committed Dec 5, 2020
commit 518e917af27e1cdc8f59ee2b7b1bc927c35c3b10
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,35 @@ const tests = {
},
],
},
{
code: normalizeIndent`
function MyComponent() {
useEffect()
useCallback()
useMemo()
}
`,
errors: [
{
message:
'React Hook useEffect will crash when called with no arguments. ' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we explain the requirement instead of the behavior? Something like:

React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's much better. Thanks!

'Did you forget to pass a function and an array of dependencies?',
suggestions: undefined,
},
{
message:
'React Hook useCallback will crash when called with no arguments. ' +
'Did you forget to pass a function and an array of dependencies?',
suggestions: undefined,
},
{
message:
'React Hook useMemo will crash when called with no arguments. ' +
'Did you forget to pass a function and an array of dependencies?',
suggestions: undefined,
},
],
},
{
// Regression test
code: normalizeIndent`
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,19 @@ export default {
const declaredDependenciesNode = node.arguments[callbackIndex + 1];
const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName);

// Check whether a callback is supplied to useEffect. If there is no
// callback supplied then the hook will not work and React will throw a TypeError.
// So no need to check for dependency inclusion.
if (!callback) {
reportProblem({
node: reactiveHook,
message:
`React Hook ${reactiveHookName} will crash when called with no arguments. ` +
`Did you forget to pass a function and an array of dependencies?`,
});
return;
}

// Check the declared dependencies for this reactive hook. If there is no
// second argument then the reactive callback will re-run on every render.
// So no need to check for dependency inclusion.
Expand Down