[buildhir] Patch: lower nested OptionalMemberExpr
Mofei Zhang committed
Mar 24, 2023 at 17:53 UTC
b07bbc36ae013cb61417ed5823cae8feda935cfb
5 files changed
+15
-9
compiler/forget/src/HIR/BuildHIR.ts
+1
-1
@@ -1871,7 +1871,7 @@ function lowerMemberExpression(
1871
object: { ...object },
1872
property: property.node.name,
1873
loc: exprLoc,
1874
- optional: t.isOptionalMemberExpression(expr),
1874
+ optional: expr.node.optional ?? false,
1875
};
1876
return { object, property: property.node.name, value };
1877
} else {
compiler/forget/src/HIR/PrintHIR.ts
+3
-3
@@ -349,9 +349,9 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
349
break;
350
}
351
case "PropertyLoad": {
352
- value = `PropertyLoad ${printPlace(instrValue.object)}.${
353
- instrValue.property
354
- }`;
352
+ value = `PropertyLoad ${printPlace(instrValue.object)}${
353
+ instrValue.optional ? "?" : ""
354
+ }.${instrValue.property}`;
355
break;
356
}
357
case "PropertyStore": {
compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.js
deleted
-4
@@ -1,4 +0,0 @@
1
-function Component(props) {
2
- let x = foo(props.a?.b.c.d);
3
- return x;
4
-}
compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md
renamed
+5
-1
@@ -2,6 +2,8 @@
2
## Input
3
4
```javascript
5
+// We should codegen nested optional properties correctly
6
+// (i.e. placing `?` in the correct PropertyLoad)
7
function Component(props) {
8
let x = foo(props.a?.b.c.d);
9
return x;
@@ -12,12 +14,14 @@ function Component(props) {
14
## Code
15
16
```javascript
17
+// We should codegen nested optional properties correctly
18
+// (i.e. placing `?` in the correct PropertyLoad)
19
function Component(props) {
20
const $ = React.unstable_useMemoCache(2);
21
const c_0 = $[0] !== props.a.b.c.d;
22
let t0;
23
if (c_0) {
20
- t0 = foo(props.a?.b?.c?.d);
24
+ t0 = foo((props.a?.b).c.d);
25
$[0] = props.a.b.c.d;
26
$[1] = t0;
27
} else {
compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.js
new
+6
@@ -0,0 +1,6 @@
1
+// We should codegen nested optional properties correctly
2
+// (i.e. placing `?` in the correct PropertyLoad)
3
+function Component(props) {
4
+ let x = foo(props.a?.b.c.d);
5
+ return x;
6
+}