[test] Test case for optional chaining in codegen
```js // here, `a?.b.c` is a single optional chain // (evaluates to undefined if a is nullish) a?.b.c; // here, 'a?.b` is an optional chain, and `.c` is an unconditional load // (nullthrows if a is nullish) (a?.b).c; ``` --- Next PR in stack will add a bailout for `(a?.b).c`. (If we want to properly handle `(a?.b).c`, we might want to model optional chains explicitly in the HIR. We currently assume that any `PropertyLoad` whose lhs is an optional property load is read conditionally.)
Mofei Zhang committed
Mar 30, 2023 at 18:43 UTC
c00e7a2af2746061cdada5735b979bf04770aac9
2 files changed
+46
compiler/forget/src/__tests__/fixtures/compiler/_bug.optional-chaining-memberexpr.expect.md
new
+39
@@ -0,0 +1,39 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+// Note that `a?.b.c` is semantically different from `(a?.b).c`
6
+// We should codegen the correct member expressions
7
+function Component(props) {
8
+ let x = props?.b.c;
9
+ let y = (props?.x).y;
10
+ return { x, y };
11
+}
12
+
13
+```
14
+
15
+## Code
16
+
17
+```javascript
18
+// Note that `a?.b.c` is semantically different from `(a?.b).c`
19
+// We should codegen the correct member expressions
20
+function Component(props) {
21
+ const $ = React.unstable_useMemoCache(3);
22
+ const x = (props?.b).c;
23
+ const y = (props?.x).y;
24
+ const c_0 = $[0] !== x;
25
+ const c_1 = $[1] !== y;
26
+ let t0;
27
+ if (c_0 || c_1) {
28
+ t0 = { x, y };
29
+ $[0] = x;
30
+ $[1] = y;
31
+ $[2] = t0;
32
+ } else {
33
+ t0 = $[2];
34
+ }
35
+ return t0;
36
+}
37
+
38
+```
39
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/_bug.optional-chaining-memberexpr.js
new
+7
@@ -0,0 +1,7 @@
1
+// Note that `a?.b.c` is semantically different from `(a?.b).c`
2
+// We should codegen the correct member expressions
3
+function Component(props) {
4
+ let x = props?.b.c;
5
+ let y = (props?.x).y;
6
+ return { x, y };
7
+}