From 9e64bf18e11828d6b4c0363bff5ed2eca1ccd838 Mon Sep 17 00:00:00 2001 From: Hristo Kanchev Date: Wed, 14 Aug 2019 15:44:06 +0200 Subject: [PATCH] [eslint-plugin-react-hooks] Fixed crash when referencing arguments in arrow functions. (#16356) * Fixed issue with def being undefined while referencing arguments. * Removed todo comment. * Skip exhaustive deps check if def is null. * Fixed code formatting in ExhaustiveDeps. * Removed unneeded comment in ExhaustiveDeps. --- .../ESLintRuleExhaustiveDeps-test.js | 22 +++++++++++++++++++ .../src/ExhaustiveDeps.js | 13 ++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js index 72130f4b830a0..a9648151a2d77 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -1023,6 +1023,28 @@ const tests = { } `, }, + // Ignore arguments keyword for arrow functions. + { + code: ` + function Example() { + useEffect(() => { + arguments + }, []) + } + `, + }, + { + code: ` + function Example() { + useEffect(() => { + const bar = () => { + arguments; + }; + bar(); + }, []) + } + `, + }, ], invalid: [ { diff --git a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js index 4a2c4f16ce335..37e315b429b3f 100644 --- a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js +++ b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js @@ -397,13 +397,14 @@ export default { }); } - // Ignore references to the function itself as it's not defined yet. const def = reference.resolved.defs[0]; - if ( - def != null && - def.node != null && - def.node.init === node.parent - ) { + + if (def == null) { + continue; + } + + // Ignore references to the function itself as it's not defined yet. + if (def.node != null && def.node.init === node.parent) { continue; }