Remove restriction on optional nesting

We previously disallowed OptionalMemberExpression inside a normal 
MemberExpression, eg `(a?.b).c`. The new representation handles this case 
correctly so we can remove the restriction.
This commit is contained in:
Joe Savona
2023-05-03 17:10:31 -07:00
parent 7ecee6a091
commit 253b05f28d
3 changed files with 10 additions and 37 deletions
-26
View File
@@ -2113,32 +2113,6 @@ function lowerMemberExpression(
const object =
loweredObject ?? lowerExpressionToTemporary(builder, objectNode);
if (
objectNode.isOptionalMemberExpression() &&
!expr.isOptionalMemberExpression()
) {
// Babel's `isOptionalMemberExpression` indicates whether this property load itself
// is conditional (i.e. within an "optional chain"). This is different from the
// `optional` property, which is only true for property loads at the start of an
// optional chain. e.g. `a.b?.c.d` decomposes into
// [0] MemberExpr a.b; // not in an optional chain
// [1] OptionalMemberExpr [0]?.c
// [2] OptionalMemberExpr [1].c
// [3] OptionalMemberExpr [2].d
// We currently do not handle non-conditional loads from an optional memberexpr
// e.g. `(a?.b).c`
// See error.nonoptional-load-from-optional-memberexpr test fixture for details
builder.errors.push({
reason: `(BuildHIR::lowerMemberExpression) Handle optional chaining for non-optional member expr.`,
severity: ErrorSeverity.Todo,
nodePath: propertyNode,
});
return {
object,
property: propertyNode.toString(),
value: { kind: "UnsupportedNode", node: exprNode, loc: exprLoc },
};
}
if (!expr.node.computed) {
if (!propertyNode.isIdentifier()) {
builder.errors.push({
@@ -13,18 +13,17 @@ function Component(props) {
```
## Code
## Error
```javascript
// Note that `a?.b.c` is semantically different from `(a?.b).c`
// Here, 'props?.a` is an optional chain, and `.b` is an unconditional load
// (nullthrows if a is nullish)
function Component(props) {
const x = (props?.a).b;
return x;
}
```
[ReactForget] TodoError: (BuildHIR::lowerMemberExpression) Handle optional chaining for non-optional member expr.
4 |
5 | function Component(props) {
> 6 | let x = (props?.a).b;
| ^
7 | return x;
8 | }
9 |
```