Adding a test for nested ternaries

This commit is contained in:
Eli White
2025-02-05 18:08:14 -08:00
parent 196c40bbf2
commit cb1fef44f0
3 changed files with 120 additions and 6 deletions
@@ -175,14 +175,13 @@ function applyConstantPropagation(
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;
const targetBlockId = testValue.value
? /*
* Do I need to change these from being value blocks?
* Currently getting
* Invariant: Expected a fallthrough for value block (6:6)
*/
branchBlock.terminal.consequent
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
// I think I can only set this if the block isn't
// used in a value position by its predecessor
block.kind = 'block';
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
@@ -0,0 +1,83 @@
## Input
```javascript
// @enableTernaryConstantPropagation
import {Stringify} from 'shared-runtime';
function foo() {
let _b;
const b = true;
_b = !b ? 'bar' : b ? 'foo' : 'baz';
return (
<Stringify
value={{
_b,
b0: !true,
n0: !0,
n1: !1,
n2: !2,
n3: !-1,
s0: !'',
s1: !'a',
s2: !'ab',
u: !undefined,
n: !null,
}}
/>
);
}
export const FIXTURE_ENTRYPOINT = {
fn: foo,
params: [],
isComponent: false,
};
```
## Code
```javascript
import { c as _c } from "react/compiler-runtime"; // @enableTernaryConstantPropagation
import { Stringify } from "shared-runtime";
function foo() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = (
<Stringify
value={{
_b: "foo",
b0: false,
n0: true,
n1: false,
n2: false,
n3: !-1,
s0: true,
s1: false,
s2: false,
u: !undefined,
n: true,
}}
/>
);
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}
export const FIXTURE_ENTRYPOINT = {
fn: foo,
params: [],
isComponent: false,
};
```
### Eval output
(kind: ok) <div>{"value":{"_b":"foo","b0":false,"n0":true,"n1":false,"n2":false,"n3":false,"s0":true,"s1":false,"s2":false,"u":true,"n":true}}</div>
@@ -0,0 +1,32 @@
// @enableTernaryConstantPropagation
import {Stringify} from 'shared-runtime';
function foo() {
let _b;
const b = true;
_b = !b ? 'bar' : b ? 'foo' : 'baz';
return (
<Stringify
value={{
_b,
b0: !true,
n0: !0,
n1: !1,
n2: !2,
n3: !-1,
s0: !'',
s1: !'a',
s2: !'ab',
u: !undefined,
n: !null,
}}
/>
);
}
export const FIXTURE_ENTRYPOINT = {
fn: foo,
params: [],
isComponent: false,
};