-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use optional chaining, optional catch binding. #21967
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only going to become a problem at the point when this file is properly type-checked, but:
isEndingEditWithFocus
is going to assume to be assigned a boolean value, which we were doing prior to these changes, but with these changes, it may also be assigned asundefined
ifwrapperNode.current
is absent.Technically it still works because it's falsey, but the previous
!!
existed for this purpose to always coerce it to a boolean.(
Boolean( /* ... */ )
works as well for coercion, if wanting to be more explicit)Aside: I think this is going to be another common pitfall with using optional chaining, where it will be easy to assign a falsey value where we might actually want a strictly-
false
(boolean) value, especially when the end of the chain is a function which we know to return a boolean (likeElement#contains
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed your suggestion.
By the way, how about adding these findings to the JavaScript section of our coding guideline?
I simply thought optional chaining is just a good way to remove
&&
and check validness of variables. But there are many pitfalls we have to avoid. These things are important but cannot be easily found in other documentations.I think it's good for us and other future developers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sainthkh For sure, I think it makes sense to be be documenting these. I'd actually planned to bring some of these up in next week's JavaScript chat, moreso as observations of common issues people may encounter, but documentation is a good action item. I think we're still so early on in its usage that we're still at a point of identifying these issues. That said, the coding guidelines are always a "working document" and be revised over time.
As with the other discussion, I think we'll ultimately want to come to rely on our tooling:
† Aside: I actually tried to test this in code by adding
@ts-check
to the top of the file, and it didn't exactly work as I expected. Not sure if it's an issue with JavaScript typing, with typing ofuseRef
, or other. Maybe something we need to figure out in the future, or if it's an upstream issue, to wait for a fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a pull request at #22029 to document the two gotchas observed here.