Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[eslint-plugin-exhaustive-deps] Fix exhaustive deps check for unstable vars #24343

Merged
merged 4 commits into from Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1095,6 +1095,22 @@ const tests = {
}
`,
},
{
code: normalizeIndent`
function Counter(unstableProp) {
let [count, setCount] = useState(0);
setCount = unstableProp
useEffect(() => {
let id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, [setCount]);

return <h1>{count}</h1>;
}
`,
},
{
code: normalizeIndent`
function Counter() {
Expand Down Expand Up @@ -1581,6 +1597,48 @@ const tests = {
},
],
},
{
code: normalizeIndent`
function Counter(unstableProp) {
let [count, setCount] = useState(0);
setCount = unstableProp
useEffect(() => {
let id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, []);

return <h1>{count}</h1>;
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'setCount'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [setCount]',
output: normalizeIndent`
function Counter(unstableProp) {
let [count, setCount] = useState(0);
setCount = unstableProp
useEffect(() => {
let id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, [setCount]);

return <h1>{count}</h1>;
}
`,
},
],
},
],
},
{
// Note: we *could* detect it's a primitive and never assigned
// even though it's not a constant -- but we currently don't.
Expand Down
9 changes: 6 additions & 3 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Expand Up @@ -234,6 +234,9 @@ export default {
if (id.elements[1] === resolved.identifiers[0]) {
if (name === 'useState') {
const references = resolved.references;
// console.log(references);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray log

if (references.filter(ref => ref.isWrite()).length > 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use curly braces for if statements

return false;
for (let i = 0; i < references.length; i++) {
setStateCallSites.set(
references[i].identifier,
Expand Down Expand Up @@ -321,7 +324,7 @@ export default {
pureScopes.has(ref.resolved.scope) &&
// Stable values are fine though,
// although we won't check functions deeper.
!memoizedIsStablecKnownHookValue(ref.resolved)
!memoizedIsStableKnownHookValue(ref.resolved)
) {
return false;
}
Expand All @@ -332,7 +335,7 @@ export default {
}

// Remember such values. Avoid re-running extra checks on them.
const memoizedIsStablecKnownHookValue = memoizeWithWeakMap(
const memoizedIsStableKnownHookValue = memoizeWithWeakMap(
isStableKnownHookValue,
stableKnownValueCache,
);
Expand Down Expand Up @@ -435,7 +438,7 @@ export default {
if (!dependencies.has(dependency)) {
const resolved = reference.resolved;
const isStable =
memoizedIsStablecKnownHookValue(resolved) ||
memoizedIsStableKnownHookValue(resolved) ||
memoizedIsFunctionWithoutCapturedValues(resolved);
dependencies.set(dependency, {
isStable,
Expand Down