Skip to content

Commit

Permalink
Adding a test for nested ternaries
Browse files Browse the repository at this point in the history
  • Loading branch information
elicwhite committed Feb 6, 2025
1 parent 196c40b commit cb1fef4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit cb1fef4

Please sign in to comment.