[compiler] Reordering of logical expressions
ghstack-source-id: ad484f97451c65a2642618bfb9d540d6a53a19a6 Pull Request resolved: https://github.com/facebook/react/pull/30678
Mike Vitousek committed
Aug 13, 2024 at 16:57 UTC
8e60bacd08215bd23f0bf05dde407cd133885aa1
3 files changed
+66
compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
+15
@@ -2847,6 +2847,21 @@ function isReorderableExpression(
2847
allowLocalIdentifiers,
2848
);
2849
}
2850
+ case 'LogicalExpression': {
2851
+ const logical = expr as NodePath<t.LogicalExpression>;
2852
+ return (
2853
+ isReorderableExpression(
2854
+ builder,
2855
+ logical.get('left'),
2856
+ allowLocalIdentifiers,
2857
+ ) &&
2858
+ isReorderableExpression(
2859
+ builder,
2860
+ logical.get('right'),
2861
+ allowLocalIdentifiers,
2862
+ )
2863
+ );
2864
+ }
2865
case 'ConditionalExpression': {
2866
const conditional = expr as NodePath<t.ConditionalExpression>;
2867
return (
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.expect.md
new
+39
@@ -0,0 +1,39 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+//@flow
6
+
7
+const foo = undefined;
8
+
9
+component C(...{scope = foo ?? null}: any) {
10
+ return scope;
11
+}
12
+
13
+export const FIXTURE_ENTRYPOINT = {
14
+ fn: C,
15
+ params: [{scope: undefined}],
16
+};
17
+
18
+```
19
+
20
+## Code
21
+
22
+```javascript
23
+const foo = undefined;
24
+
25
+function C(t0) {
26
+ const { scope: t1 } = t0;
27
+ const scope = t1 === undefined ? (foo ?? null) : t1;
28
+ return scope;
29
+}
30
+
31
+export const FIXTURE_ENTRYPOINT = {
32
+ fn: C,
33
+ params: [{ scope: undefined }],
34
+};
35
+
36
+```
37
+
38
+### Eval output
39
+(kind: ok) null
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.js
new
+12
@@ -0,0 +1,12 @@
1
+//@flow
2
+
3
+const foo = undefined;
4
+
5
+component C(...{scope = foo ?? null}: any) {
6
+ return scope;
7
+}
8
+
9
+export const FIXTURE_ENTRYPOINT = {
10
+ fn: C,
11
+ params: [{scope: undefined}],
12
+};