Skip to content

Commit

Permalink
Fix ExhaustiveDeps ESLint rule throwing with optional chaining (#19260)
Browse files Browse the repository at this point in the history
Certain code patterns using optional chaining syntax causes
eslint-plugin-react-hooks to throw an error.

We can avoid the throw by adding some guards. I didn't read through the
code to understand how it works, I just added a guard to every place
where it threw, so maybe there is a better fix closer to the root cause
than what I have here.

In my test case, I noticed that the optional chaining that was used in
the code was not included in the suggestions description or output,
but it seems like it should be. This might make a nice future
improvement on top of this fix, so I left a TODO comment to that effect.

Fixes #19243
  • Loading branch information
lencioni authored Jul 6, 2020
1 parent 670c037 commit 0f84b0f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6628,6 +6628,44 @@ const testsTypescript = {
},
],
},
{
// https://github.com/facebook/react/issues/19243
code: normalizeIndent`
function MyComponent() {
const pizza = {};
useEffect(() => ({
crust: pizza.crust,
toppings: pizza?.toppings,
}), []);
}
`,
errors: [
{
message:
"React Hook useEffect has missing dependencies: 'pizza.crust' and 'pizza.toppings'. " +
'Either include them or remove the dependency array.',
suggestions: [
{
// TODO the description and suggestions should probably also
// preserve the optional chaining.
desc:
'Update the dependencies array to be: [pizza.crust, pizza.toppings]',
output: normalizeIndent`
function MyComponent() {
const pizza = {};
useEffect(() => ({
crust: pizza.crust,
toppings: pizza?.toppings,
}), [pizza.crust, pizza.toppings]);
}
`,
},
],
},
],
},
],
};

Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,11 @@ export default {
// Is this a variable from top scope?
const topScopeRef = componentScope.set.get(missingDep);
const usedDep = dependencies.get(missingDep);
if (usedDep.references[0].resolved !== topScopeRef) {
if (usedDep && usedDep.references[0].resolved !== topScopeRef) {
return;
}
// Is this a destructured prop?
const def = topScopeRef.defs[0];
const def = topScopeRef && topScopeRef.defs[0];
if (def == null || def.name == null || def.type !== 'Parameter') {
return;
}
Expand Down Expand Up @@ -1062,7 +1062,7 @@ export default {
return;
}
const usedDep = dependencies.get(missingDep);
const references = usedDep.references;
const references = usedDep ? usedDep.references : [];
let id;
let maybeCall;
for (let i = 0; i < references.length; i++) {
Expand Down

0 comments on commit 0f84b0f

Please sign in to comment.