Skip to content

Commit 2ff27ec

Browse files
authored
[eslint] strip tailing property in assignments (#16784)
* [eslint] strip tailing property in assignments * inline `stripTailingPropInAssignment`
1 parent f625fce commit 2ff27ec

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,16 @@ const tests = {
584584
}
585585
`,
586586
},
587+
{
588+
code: normalizeIndent`
589+
function MyComponent(props) {
590+
let foo = {}
591+
useEffect(() => {
592+
foo.bar.baz = 43;
593+
}, [foo.bar]);
594+
}
595+
`,
596+
},
587597
{
588598
// Valid because we assign ref.current
589599
// ourselves. Therefore it's likely not
@@ -6395,6 +6405,39 @@ const tests = {
63956405
// Keep this until major IDEs and VS Code FB ESLint plugin support Suggestions API.
63966406
options: [{enableDangerousAutofixThisMayCauseInfiniteLoops: true}],
63976407
},
6408+
{
6409+
code: normalizeIndent`
6410+
function MyComponent(props) {
6411+
let foo = {}
6412+
useEffect(() => {
6413+
foo.bar.baz = 43;
6414+
props.foo.bar.baz = 1;
6415+
}, []);
6416+
}
6417+
`,
6418+
errors: [
6419+
{
6420+
message:
6421+
"React Hook useEffect has missing dependencies: 'foo.bar' and 'props.foo.bar'. " +
6422+
'Either include them or remove the dependency array.',
6423+
suggestions: [
6424+
{
6425+
desc:
6426+
'Update the dependencies array to be: [foo.bar, props.foo.bar]',
6427+
output: normalizeIndent`
6428+
function MyComponent(props) {
6429+
let foo = {}
6430+
useEffect(() => {
6431+
foo.bar.baz = 43;
6432+
props.foo.bar.baz = 1;
6433+
}, [foo.bar, props.foo.bar]);
6434+
}
6435+
`,
6436+
},
6437+
],
6438+
},
6439+
],
6440+
},
63986441
],
63996442
};
64006443

packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,12 @@ function getDependency(node) {
14371437
)
14381438
) {
14391439
return getDependency(node.parent);
1440+
} else if (
1441+
node.type === 'MemberExpression' &&
1442+
node.parent &&
1443+
node.parent.type === 'AssignmentExpression'
1444+
) {
1445+
return node.object;
14401446
} else {
14411447
return node;
14421448
}

0 commit comments

Comments
 (0)