Skip to content

Commit ca54a0e

Browse files
committed
feat(compiler): Implement constant string concat propagation
Resolves #29617
1 parent 6f23540 commit ca54a0e

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

compiler/crates/react_optimization/src/constant_propagation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn apply_binary_operator(
232232
BinaryOperator::StrictEquals => Some(JsValue::Boolean(left.strictly_equals(right))),
233233
BinaryOperator::NotStrictEquals => {
234234
Some(JsValue::Boolean(left.not_strictly_equals(right)))
235-
}
235+
},
236236
_ => None,
237237
},
238238
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ function evaluateInstruction(
327327
case "+": {
328328
if (typeof lhs === "number" && typeof rhs === "number") {
329329
result = { kind: "Primitive", value: lhs + rhs, loc: value.loc };
330+
} else if (typeof lhs === "string" && typeof rhs === "string") {
331+
result = { kind: "Primitive", value: lhs + rhs, loc: value.loc };
330332
}
331333
break;
332334
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
## Input
3+
4+
```javascript
5+
function foo() {
6+
const a = "a" + "b";
7+
const c = "c";
8+
return a + c;
9+
}
10+
11+
export const FIXTURE_ENTRYPOINT = {
12+
fn: foo,
13+
params: [],
14+
isComponent: false,
15+
};
16+
17+
```
18+
19+
## Code
20+
21+
```javascript
22+
function foo() {
23+
return "abc";
24+
}
25+
26+
export const FIXTURE_ENTRYPOINT = {
27+
fn: foo,
28+
params: [],
29+
isComponent: false,
30+
};
31+
32+
```
33+
34+
### Eval output
35+
(kind: ok) "abc"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function foo() {
2+
const a = "a" + "b";
3+
const c = "c";
4+
return a + c;
5+
}
6+
7+
export const FIXTURE_ENTRYPOINT = {
8+
fn: foo,
9+
params: [],
10+
isComponent: false,
11+
};

0 commit comments

Comments
 (0)