[buildhir] Patch: lower nested OptionalMemberExpr

This commit is contained in:
Mofei Zhang
2023-03-24 17:53:04 -04:00
parent 5e95967c7c
commit b07bbc36ae
5 changed files with 15 additions and 9 deletions
+1 -1
View File
@@ -1871,7 +1871,7 @@ function lowerMemberExpression(
object: { ...object },
property: property.node.name,
loc: exprLoc,
optional: t.isOptionalMemberExpression(expr),
optional: expr.node.optional ?? false,
};
return { object, property: property.node.name, value };
} else {
+3 -3
View File
@@ -349,9 +349,9 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
break;
}
case "PropertyLoad": {
value = `PropertyLoad ${printPlace(instrValue.object)}.${
instrValue.property
}`;
value = `PropertyLoad ${printPlace(instrValue.object)}${
instrValue.optional ? "?" : ""
}.${instrValue.property}`;
break;
}
case "PropertyStore": {
@@ -1,4 +0,0 @@
function Component(props) {
let x = foo(props.a?.b.c.d);
return x;
}
@@ -2,6 +2,8 @@
## Input
```javascript
// We should codegen nested optional properties correctly
// (i.e. placing `?` in the correct PropertyLoad)
function Component(props) {
let x = foo(props.a?.b.c.d);
return x;
@@ -12,12 +14,14 @@ function Component(props) {
## Code
```javascript
// We should codegen nested optional properties correctly
// (i.e. placing `?` in the correct PropertyLoad)
function Component(props) {
const $ = React.unstable_useMemoCache(2);
const c_0 = $[0] !== props.a.b.c.d;
let t0;
if (c_0) {
t0 = foo(props.a?.b?.c?.d);
t0 = foo((props.a?.b).c.d);
$[0] = props.a.b.c.d;
$[1] = t0;
} else {
@@ -0,0 +1,6 @@
// We should codegen nested optional properties correctly
// (i.e. placing `?` in the correct PropertyLoad)
function Component(props) {
let x = foo(props.a?.b.c.d);
return x;
}