diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 691ee4d6ca..85235ff528 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -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 { diff --git a/compiler/forget/src/HIR/PrintHIR.ts b/compiler/forget/src/HIR/PrintHIR.ts index c602e7701e..e5c92702c9 100644 --- a/compiler/forget/src/HIR/PrintHIR.ts +++ b/compiler/forget/src/HIR/PrintHIR.ts @@ -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": { diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.js b/compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.js deleted file mode 100644 index 63561f207c..0000000000 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.js +++ /dev/null @@ -1,4 +0,0 @@ -function Component(props) { - let x = foo(props.a?.b.c.d); - return x; -} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md similarity index 60% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.expect.md rename to compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md index 9d33267161..950ed1486a 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.nested-optional-member-expr.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md @@ -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 { diff --git a/compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.js b/compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.js new file mode 100644 index 0000000000..e9f800af8d --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/nested-optional-member-expr.js @@ -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; +}