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-react-hooks] Report constant constructions #19590

Merged
merged 9 commits into from
Aug 13, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -1391,15 +1391,6 @@ const tests = {
}
`,
},
{
code: normalizeIndent`
function useFoo(){
let foo = {};
foo = 1;
return useMemo(() => foo, [foo]);
}
`,
},
{
code: normalizeIndent`
function useFoo(){
Expand Down Expand Up @@ -5805,6 +5796,56 @@ const tests = {
},
],
},
{
code: normalizeIndent`
function MyComponent(props) {
gaearon marked this conversation as resolved.
Show resolved Hide resolved
let handleNext = () => {
console.log('hello');
};
if (props.foo) {
handleNext = () => {
console.log('hello');
};
}
useEffect(() => {
return Store.subscribe(handleNext);
}, [handleNext]);
}
captbaritone marked this conversation as resolved.
Show resolved Hide resolved
`,
errors: [
{
message:
"The 'handleNext' function makes the dependencies of useEffect Hook " +
'(at line 13) change on every render. To fix this, wrap the construction of ' +
"'handleNext' in its own useCallback() Hook.",
// Normally we'd suggest moving handleNext inside an
// effect. But it's used more than once.
// TODO: our autofix here isn't quite sufficient because
// it only wraps the first definition. But seems ok.
suggestions: [
{
desc:
"Wrap the construction of 'handleNext' in its own useCallback() Hook.",
output: normalizeIndent`
gaearon marked this conversation as resolved.
Show resolved Hide resolved
function MyComponent(props) {
let handleNext = useCallback(() => {
console.log('hello');
});
if (props.foo) {
handleNext = () => {
console.log('hello');
};
}
useEffect(() => {
return Store.subscribe(handleNext);
}, [handleNext]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent(props) {
Expand Down
6 changes: 0 additions & 6 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1467,12 +1467,6 @@ function scanForConstructions({
return null;
}

if (ref.references.some(r => !r.init && r.isWrite())) {
// The variable gets reassigned. This complicates things so we won't
// try to reason about it for now.
return null;
}

const node = ref.defs[0];
if (node == null) {
return null;
Expand Down