Skip to content

Commit

Permalink
Catch let/var constant constructions on initial assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Aug 13, 2020
1 parent 4b6e20e commit e0ca072
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
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) {
let handleNext = () => {
console.log('hello');
};
if (props.foo) {
handleNext = () => {
console.log('hello');
};
}
useEffect(() => {
return Store.subscribe(handleNext);
}, [handleNext]);
}
`,
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`
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

0 comments on commit e0ca072

Please sign in to comment.