mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
@@ -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({
|
||||
|
||||
+10
-11
@@ -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 |
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user