[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.)
This commit is contained in:
Mofei Zhang
2023-03-30 18:43:55 -04:00
parent 0eeedee95e
commit c00e7a2af2
2 changed files with 46 additions and 0 deletions
@@ -0,0 +1,39 @@
## Input
```javascript
// Note that `a?.b.c` is semantically different from `(a?.b).c`
// We should codegen the correct member expressions
function Component(props) {
let x = props?.b.c;
let y = (props?.x).y;
return { x, y };
}
```
## Code
```javascript
// Note that `a?.b.c` is semantically different from `(a?.b).c`
// We should codegen the correct member expressions
function Component(props) {
const $ = React.unstable_useMemoCache(3);
const x = (props?.b).c;
const y = (props?.x).y;
const c_0 = $[0] !== x;
const c_1 = $[1] !== y;
let t0;
if (c_0 || c_1) {
t0 = { x, y };
$[0] = x;
$[1] = y;
$[2] = t0;
} else {
t0 = $[2];
}
return t0;
}
```
@@ -0,0 +1,7 @@
// Note that `a?.b.c` is semantically different from `(a?.b).c`
// We should codegen the correct member expressions
function Component(props) {
let x = props?.b.c;
let y = (props?.x).y;
return { x, y };
}