@samitouri / QOS-React / commits / e8d130bd1d

Support type casts and conditionals as default values

Joe Savona committed Sep 7, 2023 at 17:07 UTC e8d130bd1dcb093c28ff8197278e49cf9a671419
5 files changed +108
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+14
@@ -2238,6 +2238,20 @@ function isReorderableExpression(
2238 case "BigIntLiteral": {
2239 return true;
2240 }
2241 + case "TypeCastExpression": {
2242 + return isReorderableExpression(
2243 + builder,
2244 + (expr as NodePath<t.TypeCastExpression>).get("expression")
2245 + );
2246 + }
2247 + case "ConditionalExpression": {
2248 + const conditional = expr as NodePath<t.ConditionalExpression>;
2249 + return (
2250 + isReorderableExpression(builder, conditional.get("test")) &&
2251 + isReorderableExpression(builder, conditional.get("consequent")) &&
2252 + isReorderableExpression(builder, conditional.get("alternate"))
2253 + );
2254 + }
2255 case "ArrayExpression": {
2256 return (expr as NodePath<t.ArrayExpression>)
2257 .get("elements")
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.expect.md new
+32
@@ -0,0 +1,32 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const [x = true ? 1 : 0] = props.y;
7 + return x;
8 +}
9 +
10 +export const FIXTURE_ENTRYPOINT = {
11 + fn: Component,
12 + params: [{ y: [] }],
13 +};
14 +
15 +```
16 +
17 +## Code
18 +
19 +```javascript
20 +function Component(props) {
21 + const [t23] = props.y;
22 + const x = t23 === undefined ? (true ? 1 : 0) : t23;
23 + return x;
24 +}
25 +
26 +export const FIXTURE_ENTRYPOINT = {
27 + fn: Component,
28 + params: [{ y: [] }],
29 +};
30 +
31 +```
32 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.js new
+9
@@ -0,0 +1,9 @@
1 +function Component(props) {
2 + const [x = true ? 1 : 0] = props.y;
3 + return x;
4 +}
5 +
6 +export const FIXTURE_ENTRYPOINT = {
7 + fn: Component,
8 + params: [{ y: [] }],
9 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.expect.md new
+43
@@ -0,0 +1,43 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @flow
6 +function Component(props) {
7 + const [x = ([]: Array<number>)] = props.y;
8 + return x;
9 +}
10 +
11 +export const FIXTURE_ENTRYPOINT = {
12 + fn: Component,
13 + params: [{y: []}],
14 +};
15 +```
16 +
17 +## Code
18 +
19 +```javascript
20 +import { unstable_useMemoCache as useMemoCache } from "react";
21 +function Component(props) {
22 + const $ = useMemoCache(2);
23 + const [t0] = props.y;
24 + const c_0 = $[0] !== t0;
25 + let t1;
26 + if (c_0) {
27 + t1 = t0 === undefined ? ([]: Array<number>) : t0;
28 + $[0] = t0;
29 + $[1] = t1;
30 + } else {
31 + t1 = $[1];
32 + }
33 + const x = t1;
34 + return x;
35 +}
36 +
37 +export const FIXTURE_ENTRYPOINT = {
38 + fn: Component,
39 + params: [{ y: [] }],
40 +};
41 +
42 +```
43 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.js new
+10
@@ -0,0 +1,10 @@
1 +// @flow
2 +function Component(props) {
3 + const [x = ([]: Array<number>)] = props.y;
4 + return x;
5 +}
6 +
7 +export const FIXTURE_ENTRYPOINT = {
8 + fn: Component,
9 + params: [{y: []}],
10 +};
\ No newline at end of file