From 329f4d23b581d468eae41025829ac53f036850cd Mon Sep 17 00:00:00 2001 From: cpojer Date: Mon, 16 Oct 2023 15:02:06 +0900 Subject: [PATCH] Allow `useEffect(fn, undefined)` in `react-hooks/exhaustive-deps`. --- .../__tests__/ESLintRuleExhaustiveDeps-test.js | 9 +++++++++ packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js index a7b2abbe80d0b..eb58f8d4d1fbe 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -1452,6 +1452,15 @@ const tests = { } `, }, + { + code: normalizeIndent` + function MyComponent() { + useEffect(() => { + console.log('banana banana banana'); + }, undefined); + } + `, + }, ], invalid: [ { diff --git a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js index 0b8b61b14fa54..e754edabc4341 100644 --- a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js +++ b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js @@ -1161,7 +1161,12 @@ export default { const callback = node.arguments[callbackIndex]; const reactiveHook = node.callee; const reactiveHookName = getNodeWithoutReactNamespace(reactiveHook).name; - const declaredDependenciesNode = node.arguments[callbackIndex + 1]; + const maybeNode = node.arguments[callbackIndex + 1]; + const declaredDependenciesNode = + maybeNode && + !(maybeNode.type === 'Identifier' && maybeNode.name === 'undefined') + ? maybeNode + : undefined; const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName); // Check whether a callback is supplied. If there is no callback supplied