Need an ESLint rule that restricts optional chaining with negation. #21984
Labels
[Package] ESLint plugin
/packages/eslint-plugin
[Status] In Progress
Tracking issues with work in progress
Copied from #19931(comment)
I guess as of #19831 I could simplify this using optional chaining:
What's interesting to me is looking at the other, negated instance of this condition, I sense there's going to be a very common pitfall associated with optional chaining:
gutenberg/packages/block-editor/src/components/link-control/index.js
Lines 112 to 113 in 5b72b3a
If someone treats optional chaining as a way to get to the value by any means necessary, it might be easy to overlook that negating would produce
true
if the optional chaining aborts early:😯 It's definitely not the expected result.
It seems like in the same category of issues we restrict with this ESLint rule:
https://eslint.org/docs/rules/no-unsafe-negation
I could imagine some improvement to that rule which forbids negation of optional chains.
! wrapperNode.current?.contains()
is! ( wrapperNode.current && wrapperNode.current.contains() )
.It's not
wrapperNode.current && ! wrapperNode.current.contains()
.We need an eslint rule to help us to not fall into this pitfall.
The text was updated successfully, but these errors were encountered: