@samitouri / QOS-React-2 / commits / 2ff27ec112

[eslint] strip tailing property in assignments (#16784)

* [eslint] strip tailing property in assignments * inline `stripTailingPropInAssignment`

Zen committed Apr 4, 2020 at 21:26 UTC 2ff27ec1128fca8defb71d912e501647cf58f6f8
2 files changed +49
packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js
+43
@@ -584,6 +584,16 @@ const tests = {
584 }
585 `,
586 },
587 + {
588 + code: normalizeIndent`
589 + function MyComponent(props) {
590 + let foo = {}
591 + useEffect(() => {
592 + foo.bar.baz = 43;
593 + }, [foo.bar]);
594 + }
595 + `,
596 + },
597 {
598 // Valid because we assign ref.current
599 // ourselves. Therefore it's likely not
@@ -6395,6 +6405,39 @@ const tests = {
6405 // Keep this until major IDEs and VS Code FB ESLint plugin support Suggestions API.
6406 options: [{enableDangerousAutofixThisMayCauseInfiniteLoops: true}],
6407 },
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 + },
6441 ],
6442 };
6443
packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
+6
@@ -1437,6 +1437,12 @@ function getDependency(node) {
1437 )
1438 ) {
1439 return getDependency(node.parent);
1440 + } else if (
1441 + node.type === 'MemberExpression' &&
1442 + node.parent &&
1443 + node.parent.type === 'AssignmentExpression'
1444 + ) {
1445 + return node.object;
1446 } else {
1447 return node;
1448 }