Fixing nested ternaries

This commit is contained in:
Eli White
2025-02-05 17:22:11 -08:00
parent 4e2c3a9068
commit 196c40bbf2
4 changed files with 22 additions and 5 deletions
@@ -189,9 +189,17 @@ function runWithEnvironment(
assertConsistentIdentifiers(hir);
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});
env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});
inferTypes(hir);
log({kind: 'hir', name: 'InferTypes', value: hir});
@@ -182,6 +182,7 @@ function applyConstantPropagation(
*/
branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
block.kind = 'block';
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
@@ -190,7 +191,13 @@ function applyConstantPropagation(
loc: terminal.loc,
};
block.kind = 'block';
const fallthrough = fn.body.blocks.get(
branchBlock.terminal.fallthrough,
);
if (fallthrough?.terminal.kind == 'goto') {
fallthrough.kind = 'block';
}
const consequent = fn.body.blocks.get(
branchBlock.terminal.consequent,
@@ -4,7 +4,7 @@ import {Stringify} from 'shared-runtime';
function foo() {
let _b;
const b = true;
_b = !b ? 'bar' : false ? 'foo' : 'baz';
_b = !b ? 'bar' : 'baz';
return (
<Stringify
@@ -22,12 +22,14 @@ import { c as _c } from "react/compiler-runtime";
function Foo(props) {
const $ = _c(1);
let x;
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
true ? (x = []) : (x = {});
$[0] = x;
t0 = [];
$[0] = t0;
} else {
x = $[0];
t0 = $[0];
}
x = t0;
return x;
}