-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Add the optional chaining operator `?.`, as specified in the stage 3 proposal: https://tc39.es/proposal-optional-chaining The JSLexer must check if the character after the `.` is a digit, and avoid lexing `TokenKind::questiondot` if it is. The optional nature of member access is encoded in ESTree as an `optional` property on the new `OptionalMemberExpression` and `OptionalCallExpression`. These nodes are emitted for every element of a member/call chain after encountering `?.`. There are some changes required to allow more ergonomic grouping of `OptionalMemberExpression` and `MemberExpression`, as well as of `OptionalCallExpression` and `CallExpression`. Now, `parseLeftHandSizeExpression` must attempt to parse a `NewExpression` or `OptionalExpression` (instead of a `MemberExpression`). Within `parseNewExpressionOrOptionalExpression`, we must disallow `?.` within a constructor call (`NewExpression`). So, we add an `AllowOptionalChain` flag to distinguish between the two cases. Now, the first token during `parseMemberSelect` can be `.`, `[`, or `?.`. Because `?.` can then be followed by `[`, `(`, *or* an identifier, we handle it separately in `parseMemberSelect`. We must also add a flag to `parseCallExpression` which indicates whether the resultant `CallExpression` should be marked as `optional`. IRGen results in some minor adjustments to avoid too much duplicate code. In particular, we extract a new `genMemberExpression` which can emit code for optional chains, but also provides access to the base object being accessed, so that call expressions can populate `this` properly when performing the call. We must also modify `genCallExpression` to handle the `optional` semantics. Then, we write new `genOptionalCallExpression` and `genOptionalMemberExpression` functions to make it clearer which is being called. These new functions also take a nullable `shortCircuitBB` argument, which is the block to branch to if their optional check upon `?.` were to fail. We special-case `OptionalCallExpression` and `OptionalMemberExpression` to avoid threading the `shortCircuitBB` through `genExpression`. Reviewed By: tmikov Differential Revision: D18358853 fbshipit-source-id: 4ef00db0b8acbea1de0b79bf33a824f7a2575b9f
- Loading branch information
1 parent
18341ed
commit f6577a4
Showing
15 changed files
with
1,221 additions
and
67 deletions.
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
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
Oops, something went wrong.
f6577a4
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.
Beautiful!