From 459679f91e94e868db7f1b5109a44b4bcce0351c Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 13 Mar 2024 22:17:19 -0700 Subject: [PATCH] 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. --- .../src/HIR/BuildHIR.ts | 9 ++-- .../compiler/type-alias-declaration.expect.md | 44 +++++++++++++++++++ .../compiler/type-alias-declaration.ts | 10 +++++ .../compiler/type-alias.flow.expect.md | 44 +++++++++++++++++++ .../fixtures/compiler/type-alias.flow.js | 11 +++++ 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.ts create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.js diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 77b860051d..5f26d0e6a9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -227,7 +227,7 @@ function lowerStatement( builder: HIRBuilder, stmtPath: NodePath, label: string | null = null -): undefined { +): void { const stmtNode = stmtPath.node; switch (stmtNode.type) { case "ThrowStatement": { @@ -1285,6 +1285,11 @@ function lowerStatement( return; } + case "TypeAlias": + case "TSTypeAliasDeclaration": { + // We do not preserve type annotations/syntax through transformation + return; + } case "ClassDeclaration": case "DeclareClass": case "DeclareExportAllDeclaration": @@ -1303,7 +1308,6 @@ function lowerStatement( case "ImportDeclaration": case "InterfaceDeclaration": case "OpaqueType": - case "TypeAlias": case "TSDeclareFunction": case "TSEnumDeclaration": case "TSExportAssignment": @@ -1311,7 +1315,6 @@ function lowerStatement( case "TSInterfaceDeclaration": case "TSModuleDeclaration": case "TSNamespaceExportDeclaration": - case "TSTypeAliasDeclaration": case "WithStatement": { builder.errors.push({ reason: `(BuildHIR::lowerStatement) Handle ${stmtPath.type} statements`, diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.expect.md new file mode 100644 index 0000000000..1648afe274 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function Component(props) { + type User = { name: string }; + const user: User = { name: props.name }; + return user; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Mofei" }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Component(props) { + const $ = useMemoCache(2); + let t0; + if ($[0] !== props.name) { + t0 = { name: props.name }; + $[0] = props.name; + $[1] = t0; + } else { + t0 = $[1]; + } + const user = t0; + return user; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Mofei" }], +}; + +``` + +### Eval output +(kind: ok) {"name":"Mofei"} \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.ts new file mode 100644 index 0000000000..0fa78d9001 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-declaration.ts @@ -0,0 +1,10 @@ +function Component(props) { + type User = { name: string }; + const user: User = { name: props.name }; + return user; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Mofei" }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.expect.md new file mode 100644 index 0000000000..a987e40e28 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +// @flow +function Component(props) { + type User = {name: string}; + const user: User = {name: props.name}; + return user; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{name: 'Mofei'}], +}; +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Component(props) { + const $ = useMemoCache(2); + let t0; + if ($[0] !== props.name) { + t0 = { name: props.name }; + $[0] = props.name; + $[1] = t0; + } else { + t0 = $[1]; + } + const user = t0; + return user; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Mofei" }], +}; + +``` + +### Eval output +(kind: ok) {"name":"Mofei"} \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.js new file mode 100644 index 0000000000..d5baed67ca --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias.flow.js @@ -0,0 +1,11 @@ +// @flow +function Component(props) { + type User = {name: string}; + const user: User = {name: props.name}; + return user; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{name: 'Mofei'}], +}; \ No newline at end of file