Skip to content

Commit 40380f2

Browse files
committed
eslint-plugin-react-hooks: allow OptionalMemberExpression in deps (facebook#18819)
1 parent 9751935 commit 40380f2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ function scanForDeclaredBareFunctions({
14291429
*/
14301430
function getDependency(node) {
14311431
if (
1432-
node.parent.type === 'MemberExpression' &&
1432+
(node.parent.type === 'MemberExpression' || node.parent.type === 'OptionalMemberExpression') &&
14331433
node.parent.object === node &&
14341434
node.parent.property.name !== 'current' &&
14351435
!node.parent.computed &&
@@ -1456,6 +1456,7 @@ function getDependency(node) {
14561456
* (foo) -> 'foo'
14571457
* foo.(bar) -> 'foo.bar'
14581458
* foo.bar.(baz) -> 'foo.bar.baz'
1459+
* foo?.(bar) -> 'foo?.bar'
14591460
* Otherwise throw.
14601461
*/
14611462
function toPropertyAccessString(node) {
@@ -1465,6 +1466,10 @@ function toPropertyAccessString(node) {
14651466
const object = toPropertyAccessString(node.object);
14661467
const property = toPropertyAccessString(node.property);
14671468
return `${object}.${property}`;
1469+
} else if (node.type === 'OptionalMemberExpression' && !node.computed) {
1470+
const object = toPropertyAccessString(node.object);
1471+
const property = toPropertyAccessString(node.property);
1472+
return `${object}?.${property}`;
14681473
} else {
14691474
throw new Error(`Unsupported node type: ${node.type}`);
14701475
}

0 commit comments

Comments
 (0)