From cb1fef44f056461c08a4b5db4efafde99da096cc Mon Sep 17 00:00:00 2001 From: Eli White Date: Wed, 5 Feb 2025 18:08:14 -0800 Subject: [PATCH] Adding a test for nested ternaries --- .../src/Optimization/ConstantPropagation.ts | 11 ++- ...propagation-ternary-nested-unary.expect.md | 83 +++++++++++++++++++ ...nstant-propagation-ternary-nested-unary.js | 32 +++++++ 3 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.js diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts index a447b0f71f..82e23e57e8 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts @@ -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, diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.expect.md new file mode 100644 index 0000000000..15e91fb923 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.expect.md @@ -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 ( + + ); +} + +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 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":{"_b":"foo","b0":false,"n0":true,"n1":false,"n2":false,"n3":false,"s0":true,"s1":false,"s2":false,"u":true,"n":true}}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.js new file mode 100644 index 0000000000..3ef5c72b5d --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-ternary-nested-unary.js @@ -0,0 +1,32 @@ +// @enableTernaryConstantPropagation +import {Stringify} from 'shared-runtime'; + +function foo() { + let _b; + const b = true; + _b = !b ? 'bar' : b ? 'foo' : 'baz'; + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +};