Skip to content
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 3 commits into from
May 1, 2020

Conversation

sainthkh
Copy link
Contributor

Description

Now that docgen parser has been changed to babel (#21853), we can freely use ES2020 features! 🎉

How has this been tested?

npm run docs:build doesn't fail.

Screenshots

N/A

Types of changes

Bug fix.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • [N/A] My code follows the accessibility standards.
  • [N/A] My code has proper inline documentation.
  • [N/A] I've included developer documentation if appropriate.
  • [N/A] I've updated all React Native files affected by any refactorings/renamings in this PR.

wrapperNode.current &&
! wrapperNode.current.contains( document.activeElement );
! wrapperNode.current?.contains( document.activeElement );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall reflecting on this at #19931 (comment) , and observing that this sort of change will be a very common pitfall, and not an equivalent behavior to what would have existed previously.

Which is: If wrapperNode.current is undefined, then the negation would yield a value of true, which is not what we expect.

I'd recommend to just leave this change out.

I think we could use it here though, in the same file:

!! wrapperNode.current &&
wrapperNode.current.contains( document.activeElement );

Copy link
Contributor Author

@sainthkh sainthkh Apr 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! wrapperNode.current?.contains()
=== ! (wrapperNode.current && wrapperNode.current.contains())
!== wrapperNode.current && ! wrapperNode.current.contains().

As you said in the comment, we need a rule, no-optional-chaining-with-negation.

EDIT: opened an issue. #21984

isEndingEditWithFocus.current =
!! wrapperNode.current &&
wrapperNode.current.contains( document.activeElement );
isEndingEditWithFocus.current = wrapperNode.current?.contains(
Copy link
Member

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 as undefined if wrapperNode.current is absent.

const wrapperNode = { current: undefined };
const current = wrapperNode.current?.contains( document.activeElement );
console.log( typeof current );
// "undefined"

Technically it still works because it's falsey, but the previous !! existed for this purpose to always coerce it to a boolean.

Suggested change
isEndingEditWithFocus.current = wrapperNode.current?.contains(
isEndingEditWithFocus.current = !! wrapperNode.current?.contains(

(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 (like Element#contains).

Copy link
Contributor Author

@sainthkh sainthkh May 1, 2020

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.

Copy link
Member

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 of useRef, or other. Maybe something we need to figure out in the future, or if it's an upstream issue, to wait for a fix.

Copy link
Member

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.

Co-authored-by: Andrew Duthie <andrew@andrewduthie.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants