Skip to content

Commit

Permalink
[ESLint] Fix a bug causing a too coarse dependency suggestion (#19313)
Browse files Browse the repository at this point in the history
* Add regression test for ESLint rule

* Fix the issue
  • Loading branch information
gaearon authored Jul 10, 2020
1 parent d5d6590 commit 47915fd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,39 @@ const tests = {
}
`,
},
// Regression test.
{
code: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props.upperViewHeight;
}, [props.upperViewHeight]);
}
`,
},
// Regression test.
{
code: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props?.upperViewHeight;
}, [props?.upperViewHeight]);
}
`,
},
// Regression test.
{
code: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props?.upperViewHeight;
}, [props]);
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -7120,6 +7153,70 @@ const testsTypescript = {
},
],
},
// Regression test.
{
code: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props.upperViewHeight;
}, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'props.upperViewHeight'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc:
'Update the dependencies array to be: [props.upperViewHeight]',
output: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props.upperViewHeight;
}, [props.upperViewHeight]);
}
`,
},
],
},
],
},
// Regression test.
{
code: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props?.upperViewHeight;
}, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'props?.upperViewHeight'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc:
'Update the dependencies array to be: [props?.upperViewHeight]',
output: normalizeIndent`
function Example(props) {
useEffect(() => {
let topHeight = 0;
topHeight = props?.upperViewHeight;
}, [props?.upperViewHeight]);
}
`,
},
],
},
],
},
],
};

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,8 @@ function getDependency(node) {
// Note: we don't check OptionalMemberExpression because it can't be LHS.
node.type === 'MemberExpression' &&
node.parent &&
node.parent.type === 'AssignmentExpression'
node.parent.type === 'AssignmentExpression' &&
node.parent.left === node
) {
return node.object;
} else {
Expand Down

0 comments on commit 47915fd

Please sign in to comment.