mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Support type casts and conditionals as default values
This commit is contained in:
@@ -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")
|
||||
|
||||
+32
@@ -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: [] }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
function Component(props) {
|
||||
const [x = true ? 1 : 0] = props.y;
|
||||
return x;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ y: [] }],
|
||||
};
|
||||
+43
@@ -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: [] }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// @flow
|
||||
function Component(props) {
|
||||
const [x = ([]: Array<number>)] = props.y;
|
||||
return x;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{y: []}],
|
||||
};
|
||||
Reference in New Issue
Block a user