From cc0b12008e2bff9dc2640bf76fb122274240dd6f Mon Sep 17 00:00:00 2001 From: Hristo Kanchev Date: Sun, 11 Aug 2019 11:42:57 +0200 Subject: [PATCH 1/5] Fixed issue with def being undefined while referencing arguments. --- .../ESLintRuleExhaustiveDeps-test.js | 31 +++++++++++++++++++ .../src/ExhaustiveDeps.js | 8 ++++- 2 files changed, 38 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 8a72692335ed2..94f648a6f201e 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -1024,6 +1024,37 @@ const tests = { } `, }, + // TODO: Write description - arguments in arrow functions + { + code: ` + function Example() { + useEffect(function() { + arguments + }, []) + } + `, + }, + { + 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..9c26f0034eed8 100644 --- a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js +++ b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js @@ -397,8 +397,14 @@ export default { }); } - // Ignore references to the function itself as it's not defined yet. const def = reference.resolved.defs[0]; + + // Ignore if def is undefined - e.g. `arguments` binding. + if (def === undefined) { + continue; + } + + // Ignore references to the function itself as it's not defined yet. if ( def != null && def.node != null && From 979389c74984d9f184bdb8499bb5a1d4c25a454a Mon Sep 17 00:00:00 2001 From: Hristo Kanchev Date: Sun, 11 Aug 2019 11:47:06 +0200 Subject: [PATCH 2/5] Removed todo comment. --- .../__tests__/ESLintRuleExhaustiveDeps-test.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js index 94f648a6f201e..96dac14f4777d 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -1024,16 +1024,7 @@ const tests = { } `, }, - // TODO: Write description - arguments in arrow functions - { - code: ` - function Example() { - useEffect(function() { - arguments - }, []) - } - `, - }, + // Ignore arguments keyword for arrow functions. { code: ` function Example() { From 0d23e7bd30c3ada4b04619e0553222e2c6d2f415 Mon Sep 17 00:00:00 2001 From: Hristo Kanchev Date: Wed, 14 Aug 2019 09:42:41 +0200 Subject: [PATCH 3/5] Skip exhaustive deps check if def is null. --- .../__tests__/ESLintRuleExhaustiveDeps-test.js | 2 +- packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js index 96dac14f4777d..8c3aa19d907d0 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -1024,7 +1024,7 @@ const tests = { } `, }, - // Ignore arguments keyword for arrow functions. + // Ignore arguments keyword for arrow functions. { code: ` function Example() { diff --git a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js index 9c26f0034eed8..06e4b7292285c 100644 --- a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js +++ b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js @@ -400,13 +400,12 @@ export default { const def = reference.resolved.defs[0]; // Ignore if def is undefined - e.g. `arguments` binding. - if (def === undefined) { + if (def == null) { continue; } // Ignore references to the function itself as it's not defined yet. if ( - def != null && def.node != null && def.node.init === node.parent ) { From 8b1f5ef208b55647fe2c051b54e1fd82aebcd7fe Mon Sep 17 00:00:00 2001 From: Hristo Kanchev Date: Wed, 14 Aug 2019 09:46:13 +0200 Subject: [PATCH 4/5] Fixed code formatting in ExhaustiveDeps. --- packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js index 06e4b7292285c..3d7e9286e7b88 100644 --- a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js +++ b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js @@ -405,10 +405,7 @@ export default { } // Ignore references to the function itself as it's not defined yet. - if ( - def.node != null && - def.node.init === node.parent - ) { + if (def.node != null && def.node.init === node.parent) { continue; } From 52cd6a01bffcdea4cd7e40e634dd2aea15d9a7d6 Mon Sep 17 00:00:00 2001 From: Hristo Kanchev Date: Wed, 14 Aug 2019 09:47:32 +0200 Subject: [PATCH 5/5] Removed unneeded comment in ExhaustiveDeps. --- packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js index 3d7e9286e7b88..37e315b429b3f 100644 --- a/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js +++ b/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js @@ -399,7 +399,6 @@ export default { const def = reference.resolved.defs[0]; - // Ignore if def is undefined - e.g. `arguments` binding. if (def == null) { continue; }