@samitouri / QOS-React-2 / commits / 9e64bf18e1

[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.

Hristo Kanchev committed Aug 14, 2019 at 15:44 UTC 9e64bf18e11828d6b4c0363bff5ed2eca1ccd838
2 files changed +29 -6
packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js
+22
@@ -1023,6 +1023,28 @@ const tests = {
1023 }
1024 `,
1025 },
1026 + // Ignore arguments keyword for arrow functions.
1027 + {
1028 + code: `
1029 + function Example() {
1030 + useEffect(() => {
1031 + arguments
1032 + }, [])
1033 + }
1034 + `,
1035 + },
1036 + {
1037 + code: `
1038 + function Example() {
1039 + useEffect(() => {
1040 + const bar = () => {
1041 + arguments;
1042 + };
1043 + bar();
1044 + }, [])
1045 + }
1046 + `,
1047 + },
1048 ],
1049 invalid: [
1050 {
packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
+7 -6
@@ -397,13 +397,14 @@ export default {
397 });
398 }
399
400 - // Ignore references to the function itself as it's not defined yet.
400 const def = reference.resolved.defs[0];
402 - if (
403 - def != null &&
404 - def.node != null &&
405 - def.node.init === node.parent
406 - ) {
401 +
402 + if (def == null) {
403 + continue;
404 + }
405 +
406 + // Ignore references to the function itself as it's not defined yet.
407 + if (def.node != null && def.node.init === node.parent) {
408 continue;
409 }
410