Skip to content

Commit

Permalink
Handle the unknown case
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Mar 31, 2020
1 parent c7251b0 commit 4bd4339
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6318,6 +6318,24 @@ const tests = {
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
useEffect(debounce(() => {
console.log(local);
}, delay), []);
}
`,
errors: [
{
message:
'React Hook useEffect received a function whose dependencies ' +
'are unknown. Pass an inline function instead.',
suggestions: [],
},
],
},
],
};

Expand Down
12 changes: 11 additions & 1 deletion packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
}
const callback = node.arguments[callbackIndex];
const reactiveHook = node.callee;
const reactiveHookName = getNodeWithoutReactNamespace(reactiveHook).name;
const declaredDependenciesNode = node.arguments[callbackIndex + 1];

switch (callback.type) {
Expand Down Expand Up @@ -137,9 +138,18 @@ export default {
break; // Unhandled
}
break; // Unhandled
default:
// useEffect(generateEffectBody(), []);
context.report({
node: reactiveHook,
message:
`React Hook ${reactiveHookName} received a function whose dependencies ` +
`are unknown. Pass an inline function instead.`,
});
return; // Handled
}

// Something unusual. Fall back to suggesting to add the body itself as a dep.
const reactiveHookName = getNodeWithoutReactNamespace(reactiveHook).name;
context.report({
node: reactiveHook,
message:
Expand Down

0 comments on commit 4bd4339

Please sign in to comment.