Support type casts and conditionals as default values

This commit is contained in:
Joe Savona
2023-09-07 17:07:34 -07:00
parent 0052f2e591
commit e8d130bd1d
5 changed files with 108 additions and 0 deletions
@@ -2238,6 +2238,20 @@ function isReorderableExpression(
case "BigIntLiteral": {
return true;
}
case "TypeCastExpression": {
return isReorderableExpression(
builder,
(expr as NodePath<t.TypeCastExpression>).get("expression")
);
}
case "ConditionalExpression": {
const conditional = expr as NodePath<t.ConditionalExpression>;
return (
isReorderableExpression(builder, conditional.get("test")) &&
isReorderableExpression(builder, conditional.get("consequent")) &&
isReorderableExpression(builder, conditional.get("alternate"))
);
}
case "ArrayExpression": {
return (expr as NodePath<t.ArrayExpression>)
.get("elements")
@@ -0,0 +1,32 @@
## Input
```javascript
function Component(props) {
const [x = true ? 1 : 0] = props.y;
return x;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ y: [] }],
};
```
## Code
```javascript
function Component(props) {
const [t23] = props.y;
const x = t23 === undefined ? (true ? 1 : 0) : t23;
return x;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ y: [] }],
};
```
@@ -0,0 +1,9 @@
function Component(props) {
const [x = true ? 1 : 0] = props.y;
return x;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ y: [] }],
};
@@ -0,0 +1,43 @@
## Input
```javascript
// @flow
function Component(props) {
const [x = ([]: Array<number>)] = props.y;
return x;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{y: []}],
};
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Component(props) {
const $ = useMemoCache(2);
const [t0] = props.y;
const c_0 = $[0] !== t0;
let t1;
if (c_0) {
t1 = t0 === undefined ? ([]: Array<number>) : t0;
$[0] = t0;
$[1] = t1;
} else {
t1 = $[1];
}
const x = t1;
return x;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ y: [] }],
};
```
@@ -0,0 +1,10 @@
// @flow
function Component(props) {
const [x = ([]: Array<number>)] = props.y;
return x;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{y: []}],
};