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.
Joe Savona committed
Mar 13, 2024 at 22:17 UTC
459679f91e94e868db7f1b5109a44b4bcce0351c
5 files changed
+115
-3
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+6
-3
@@ -227,7 +227,7 @@ function lowerStatement(
227
builder: HIRBuilder,
228
stmtPath: NodePath<t.Statement>,
229
label: string | null = null
230
-): undefined {
230
+): void {
231
const stmtNode = stmtPath.node;
232
switch (stmtNode.type) {
233
case "ThrowStatement": {
@@ -1285,6 +1285,11 @@ function lowerStatement(
1285
1286
return;
1287
}
1288
+ case "TypeAlias":
1289
+ case "TSTypeAliasDeclaration": {
1290
+ // We do not preserve type annotations/syntax through transformation
1291
+ return;
1292
+ }
1293
case "ClassDeclaration":
1294
case "DeclareClass":
1295
case "DeclareExportAllDeclaration":
@@ -1303,7 +1308,6 @@ function lowerStatement(
1308
case "ImportDeclaration":
1309
case "InterfaceDeclaration":
1310
case "OpaqueType":
1306
- case "TypeAlias":
1311
case "TSDeclareFunction":
1312
case "TSEnumDeclaration":
1313
case "TSExportAssignment":
@@ -1311,7 +1315,6 @@ function lowerStatement(
1315
case "TSInterfaceDeclaration":
1316
case "TSModuleDeclaration":
1317
case "TSNamespaceExportDeclaration":
1314
- case "TSTypeAliasDeclaration":
1318
case "WithStatement": {
1319
builder.errors.push({
1320
reason: `(BuildHIR::lowerStatement) Handle ${stmtPath.type} statements`,
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.expect.md
new
+44
@@ -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"}
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.ts
new
+10
@@ -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
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.expect.md
new
+44
@@ -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"}
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.js
new
+11
@@ -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
+};
\ No newline at end of file