Skip to content

Commit 57d9058

Browse files
committed
Improve JSX tag handling in constant propagation and update related tests
1 parent 061c07d commit 57d9058

4 files changed

Lines changed: 22 additions & 15 deletions

File tree

compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
markInstructionIds,
2424
markPredecessors,
2525
mergeConsecutiveBlocks,
26+
promoteTemporaryJsxTag,
2627
reversePostorderBlocks,
2728
} from '../HIR';
2829
import {
@@ -598,14 +599,16 @@ function evaluateInstruction(
598599
return result;
599600
}
600601
case 'LoadLocal': {
601-
if (
602-
instr.lvalue != null &&
603-
jsxTagIdentifiers.has(instr.lvalue.identifier.id)
604-
) {
605-
return null;
606-
}
607602
const placeValue = read(constants, value.place);
608603
if (placeValue !== null) {
604+
if (
605+
instr.lvalue != null &&
606+
jsxTagIdentifiers.has(instr.lvalue.identifier.id) &&
607+
placeValue.kind === 'LoadGlobal' &&
608+
instr.lvalue.identifier.name == null
609+
) {
610+
promoteTemporaryJsxTag(instr.lvalue.identifier);
611+
}
609612
instr.value = placeValue;
610613
}
611614
return placeValue;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-local-tag-in-lambda.expect.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@ import { c as _c } from "react/compiler-runtime";
2525
import { Stringify } from "shared-runtime";
2626
function useFoo() {
2727
const $ = _c(1);
28-
const MyLocal = Stringify;
28+
29+
const callback = _temp;
2930
let t0;
3031
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
31-
const callback = () => <MyLocal value={4} />;
32-
3332
t0 = callback();
3433
$[0] = t0;
3534
} else {
3635
t0 = $[0];
3736
}
3837
return t0;
3938
}
39+
function _temp() {
40+
const T0 = Stringify;
41+
return <T0 value={4} />;
42+
}
4043

4144
export const FIXTURE_ENTRYPOINT = {
4245
fn: useFoo,

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.expect.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import { StaticText1, StaticText2 } from "shared-runtime";
3030

3131
function Component(props) {
3232
const $ = _c(3);
33-
let Tag = StaticText1;
3433

35-
const T0 = Tag;
36-
const t0 = ((Tag = StaticText2), props.value);
34+
const T0 = StaticText1;
35+
const t0 = props.value;
36+
const T1 = StaticText2;
3737
let t1;
3838
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
39-
t1 = <Tag />;
39+
t1 = <T1 />;
4040
$[0] = t1;
4141
} else {
4242
t1 = $[0];

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-jsx-dynamic-tag-alias.expect.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ const base = "div";
2727

2828
const TestComponent: React.FC = () => {
2929
const $ = _c(1);
30-
const Comp = base;
30+
31+
const T0 = base;
3132
let t0;
3233
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
33-
t0 = <Comp />;
34+
t0 = <T0 />;
3435
$[0] = t0;
3536
} else {
3637
t0 = $[0];

0 commit comments

Comments
 (0)