Skip to content

Commit 26aa562

Browse files
committed
react-hooks/exhaustive-deps: Handle optional chained methods as dependency (#20204)
1 parent 15df051 commit 26aa562

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
@@ -3593,6 +3593,36 @@ const tests = {
35933593
},
35943594
],
35953595
},
3596+
{
3597+
code: normalizeIndent`
3598+
function MyComponent(props) {
3599+
useEffect(() => {}, [props?.attribute.method()]);
3600+
}
3601+
`,
3602+
errors: [
3603+
{
3604+
message:
3605+
'React Hook useEffect has a complex expression in the dependency array. ' +
3606+
'Extract it to a separate variable so it can be statically checked.',
3607+
suggestions: undefined,
3608+
},
3609+
],
3610+
},
3611+
{
3612+
code: normalizeIndent`
3613+
function MyComponent(props) {
3614+
useEffect(() => {}, [props.method()]);
3615+
}
3616+
`,
3617+
errors: [
3618+
{
3619+
message:
3620+
'React Hook useEffect has a complex expression in the dependency array. ' +
3621+
'Extract it to a separate variable so it can be statically checked.',
3622+
suggestions: undefined,
3623+
},
3624+
],
3625+
},
35963626
{
35973627
code: normalizeIndent`
35983628
function MyComponent() {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,11 @@ function analyzePropertyChain(node, optionalChains) {
16431643
return result;
16441644
} else if (node.type === 'ChainExpression' && !node.computed) {
16451645
const expression = node.expression;
1646+
1647+
if (expression.type === 'CallExpression') {
1648+
throw new Error(`Unsupported node type: ${expression.type}`);
1649+
}
1650+
16461651
const object = analyzePropertyChain(expression.object, optionalChains);
16471652
const property = analyzePropertyChain(expression.property, null);
16481653
const result = `${object}.${property}`;

0 commit comments

Comments
 (0)