Skip to content

Commit 4fdc852

Browse files
committed
[compiler] Check TSAsExpression and TSNonNullExpression for reorderablity
The TSAsExpression and TSNonNullExpression nodes are supported by `lowerExpression()` but `isReorderableExpression()` does not check if they can be reordered.
1 parent 97cdd5d commit 4fdc852

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,6 +3001,8 @@ function isReorderableExpression(
30013001
}
30023002
}
30033003
}
3004+
case 'TSAsExpression':
3005+
case 'TSNonNullExpression':
30043006
case 'TypeCastExpression': {
30053007
return isReorderableExpression(
30063008
builder,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
## Input
3+
4+
```javascript
5+
type Status = 'pending' | 'success' | 'error';
6+
7+
const StatusIndicator = ({status}: {status: Status}) => {
8+
return <div className={`status-${status}`}>Status: {status}</div>;
9+
};
10+
11+
const Component = ({status = 'pending' as Status}) => {
12+
return <StatusIndicator status={status} />;
13+
};
14+
15+
export const FIXTURE_ENTRYPOINT = {
16+
fn: Component,
17+
params: [{status: 'success'}],
18+
};
19+
20+
```
21+
22+
## Code
23+
24+
```javascript
25+
import { c as _c } from "react/compiler-runtime";
26+
type Status = "pending" | "success" | "error";
27+
28+
const StatusIndicator = (t0) => {
29+
const $ = _c(3);
30+
const { status } = t0;
31+
const t1 = `status-${status}`;
32+
let t2;
33+
if ($[0] !== status || $[1] !== t1) {
34+
t2 = <div className={t1}>Status: {status}</div>;
35+
$[0] = status;
36+
$[1] = t1;
37+
$[2] = t2;
38+
} else {
39+
t2 = $[2];
40+
}
41+
return t2;
42+
};
43+
44+
const Component = (t0) => {
45+
const $ = _c(2);
46+
const { status: t1 } = t0;
47+
const status = t1 === undefined ? ("pending" as Status) : t1;
48+
let t2;
49+
if ($[0] !== status) {
50+
t2 = <StatusIndicator status={status} />;
51+
$[0] = status;
52+
$[1] = t2;
53+
} else {
54+
t2 = $[1];
55+
}
56+
return t2;
57+
};
58+
59+
export const FIXTURE_ENTRYPOINT = {
60+
fn: Component,
61+
params: [{ status: "success" }],
62+
};
63+
64+
```
65+
66+
### Eval output
67+
(kind: ok) <div class="status-success">Status: success</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Status = 'pending' | 'success' | 'error';
2+
3+
const StatusIndicator = ({status}: {status: Status}) => {
4+
return <div className={`status-${status}`}>Status: {status}</div>;
5+
};
6+
7+
const Component = ({status = 'pending' as Status}) => {
8+
return <StatusIndicator status={status} />;
9+
};
10+
11+
export const FIXTURE_ENTRYPOINT = {
12+
fn: Component,
13+
params: [{status: 'success'}],
14+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
## Input
3+
4+
```javascript
5+
export const Component = ({theme = localStorage.getItem('theme')!}) => {
6+
return <div className={`theme-${theme}`}>User preferences</div>;
7+
};
8+
9+
export const FIXTURE_ENTRYPOINT = {
10+
fn: Component,
11+
params: [{status: 'success'}],
12+
};
13+
14+
```
15+
16+
## Code
17+
18+
```javascript
19+
import { c as _c } from "react/compiler-runtime";
20+
export const Component = (t0) => {
21+
const $ = _c(2);
22+
const { theme: t1 } = t0;
23+
const theme = t1 === undefined ? localStorage.getItem("theme") : t1;
24+
const t2 = `theme-${theme}`;
25+
let t3;
26+
if ($[0] !== t2) {
27+
t3 = <div className={t2}>User preferences</div>;
28+
$[0] = t2;
29+
$[1] = t3;
30+
} else {
31+
t3 = $[1];
32+
}
33+
return t3;
34+
};
35+
36+
export const FIXTURE_ENTRYPOINT = {
37+
fn: Component,
38+
params: [{ status: "success" }],
39+
};
40+
41+
```
42+
43+
### Eval output
44+
(kind: exception) localStorage is not defined
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const Component = ({theme = localStorage.getItem('theme')!}) => {
2+
return <div className={`theme-${theme}`}>User preferences</div>;
3+
};
4+
5+
export const FIXTURE_ENTRYPOINT = {
6+
fn: Component,
7+
params: [{status: 'success'}],
8+
};

0 commit comments

Comments
 (0)