Skip to content

Commit 459679f

Browse files
committed
Support type alias syntax
"Support" in the sense of dropping these on the floor and compiling, rather than bailing out with a todo. We already don't make any guarantees about which type annotations we'll preserve through to the output, so it seems fine for now to just drop type aliases.
1 parent 1b5ae06 commit 459679f

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function lowerStatement(
227227
builder: HIRBuilder,
228228
stmtPath: NodePath<t.Statement>,
229229
label: string | null = null
230-
): undefined {
230+
): void {
231231
const stmtNode = stmtPath.node;
232232
switch (stmtNode.type) {
233233
case "ThrowStatement": {
@@ -1285,6 +1285,11 @@ function lowerStatement(
12851285

12861286
return;
12871287
}
1288+
case "TypeAlias":
1289+
case "TSTypeAliasDeclaration": {
1290+
// We do not preserve type annotations/syntax through transformation
1291+
return;
1292+
}
12881293
case "ClassDeclaration":
12891294
case "DeclareClass":
12901295
case "DeclareExportAllDeclaration":
@@ -1303,15 +1308,13 @@ function lowerStatement(
13031308
case "ImportDeclaration":
13041309
case "InterfaceDeclaration":
13051310
case "OpaqueType":
1306-
case "TypeAlias":
13071311
case "TSDeclareFunction":
13081312
case "TSEnumDeclaration":
13091313
case "TSExportAssignment":
13101314
case "TSImportEqualsDeclaration":
13111315
case "TSInterfaceDeclaration":
13121316
case "TSModuleDeclaration":
13131317
case "TSNamespaceExportDeclaration":
1314-
case "TSTypeAliasDeclaration":
13151318
case "WithStatement": {
13161319
builder.errors.push({
13171320
reason: `(BuildHIR::lowerStatement) Handle ${stmtPath.type} statements`,
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+
function Component(props) {
6+
type User = { name: string };
7+
const user: User = { name: props.name };
8+
return user;
9+
}
10+
11+
export const FIXTURE_ENTRYPOINT = {
12+
fn: Component,
13+
params: [{ name: "Mofei" }],
14+
};
15+
16+
```
17+
18+
## Code
19+
20+
```javascript
21+
import { unstable_useMemoCache as useMemoCache } from "react";
22+
function Component(props) {
23+
const $ = useMemoCache(2);
24+
let t0;
25+
if ($[0] !== props.name) {
26+
t0 = { name: props.name };
27+
$[0] = props.name;
28+
$[1] = t0;
29+
} else {
30+
t0 = $[1];
31+
}
32+
const user = t0;
33+
return user;
34+
}
35+
36+
export const FIXTURE_ENTRYPOINT = {
37+
fn: Component,
38+
params: [{ name: "Mofei" }],
39+
};
40+
41+
```
42+
43+
### Eval output
44+
(kind: ok) {"name":"Mofei"}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function Component(props) {
2+
type User = { name: string };
3+
const user: User = { name: props.name };
4+
return user;
5+
}
6+
7+
export const FIXTURE_ENTRYPOINT = {
8+
fn: Component,
9+
params: [{ name: "Mofei" }],
10+
};
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+
// @flow
6+
function Component(props) {
7+
type User = {name: string};
8+
const user: User = {name: props.name};
9+
return user;
10+
}
11+
12+
export const FIXTURE_ENTRYPOINT = {
13+
fn: Component,
14+
params: [{name: 'Mofei'}],
15+
};
16+
```
17+
18+
## Code
19+
20+
```javascript
21+
import { unstable_useMemoCache as useMemoCache } from "react";
22+
function Component(props) {
23+
const $ = useMemoCache(2);
24+
let t0;
25+
if ($[0] !== props.name) {
26+
t0 = { name: props.name };
27+
$[0] = props.name;
28+
$[1] = t0;
29+
} else {
30+
t0 = $[1];
31+
}
32+
const user = t0;
33+
return user;
34+
}
35+
36+
export const FIXTURE_ENTRYPOINT = {
37+
fn: Component,
38+
params: [{ name: "Mofei" }],
39+
};
40+
41+
```
42+
43+
### Eval output
44+
(kind: ok) {"name":"Mofei"}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
function Component(props) {
3+
type User = {name: string};
4+
const user: User = {name: props.name};
5+
return user;
6+
}
7+
8+
export const FIXTURE_ENTRYPOINT = {
9+
fn: Component,
10+
params: [{name: 'Mofei'}],
11+
};

0 commit comments

Comments
 (0)