feat(compiler): Implement constant string concat propagation (#29621)
## Summary Resolves #29617 ## How did you test this change? I verified the implementation using the test.
Niklas Mollenhauer committed
May 29, 2024 at 01:15 UTC
a9a01068084550c0c71d8da222eb67eb7024c5b3
3 files changed
+48
compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
+2
@@ -327,6 +327,8 @@ function evaluateInstruction(
327
case "+": {
328
if (typeof lhs === "number" && typeof rhs === "number") {
329
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 };
332
}
333
break;
334
}
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.expect.md
new
+35
@@ -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"
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.js
new
+11
@@ -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
+};