Skip to content

Commit eb58c39

Browse files
authored
react-hooks/exhaustive-deps: Handle optional chained methods as dependency (#20204) (#20247)
1 parent 7b84dbd commit eb58c39

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,6 +3629,36 @@ const tests = {
36293629
},
36303630
],
36313631
},
3632+
{
3633+
code: normalizeIndent`
3634+
function MyComponent(props) {
3635+
useEffect(() => {}, [props?.attribute.method()]);
3636+
}
3637+
`,
3638+
errors: [
3639+
{
3640+
message:
3641+
'React Hook useEffect has a complex expression in the dependency array. ' +
3642+
'Extract it to a separate variable so it can be statically checked.',
3643+
suggestions: undefined,
3644+
},
3645+
],
3646+
},
3647+
{
3648+
code: normalizeIndent`
3649+
function MyComponent(props) {
3650+
useEffect(() => {}, [props.method()]);
3651+
}
3652+
`,
3653+
errors: [
3654+
{
3655+
message:
3656+
'React Hook useEffect has a complex expression in the dependency array. ' +
3657+
'Extract it to a separate variable so it can be statically checked.',
3658+
suggestions: undefined,
3659+
},
3660+
],
3661+
},
36323662
{
36333663
code: normalizeIndent`
36343664
function MyComponent() {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,11 @@ function analyzePropertyChain(node, optionalChains) {
16561656
return result;
16571657
} else if (node.type === 'ChainExpression' && !node.computed) {
16581658
const expression = node.expression;
1659+
1660+
if (expression.type === 'CallExpression') {
1661+
throw new Error(`Unsupported node type: ${expression.type}`);
1662+
}
1663+
16591664
const object = analyzePropertyChain(expression.object, optionalChains);
16601665
const property = analyzePropertyChain(expression.property, null);
16611666
const result = `${object}.${property}`;

0 commit comments

Comments
 (0)