diff --git a/compiler/forget/src/Babel/BabelPlugin.ts b/compiler/forget/src/Babel/BabelPlugin.ts index fe68898c3a..12dfb82184 100644 --- a/compiler/forget/src/Babel/BabelPlugin.ts +++ b/compiler/forget/src/Babel/BabelPlugin.ts @@ -8,22 +8,23 @@ /// import type * as BabelCore from "@babel/core"; -import type { PluginObj } from "@babel/core"; import jsx from "@babel/plugin-syntax-jsx"; import { compile } from "../CompilerPipeline"; /** * The React Forget Babel Plugin - * @param {*} babel + * @param {*} _babel * @returns */ -export default function (babel: typeof BabelCore): PluginObj { +export default function ReactForgetBabelPlugin( + _babel: typeof BabelCore +): BabelCore.PluginObj { return { name: "react-forget", inherits: jsx, visitor: { FunctionDeclaration: { - enter(fn, pass) { + enter(fn, _pass) { if (fn.scope.getProgramParent() !== fn.scope.parent) { return; } diff --git a/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts b/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts new file mode 100644 index 0000000000..4971a9ced9 --- /dev/null +++ b/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts @@ -0,0 +1,40 @@ +import type * as BabelCore from "@babel/core"; +import { transformFromAstSync } from "@babel/core"; +import * as parser from "@babel/parser"; +import invariant from "invariant"; +import prettier from "prettier"; +import ReactForgetBabelPlugin from "./BabelPlugin"; + +type ReactForgetBabelPluginResult = { + ast: BabelCore.BabelFileResult["ast"]; + code: string; + map: BabelCore.BabelFileResult["map"]; +}; + +export default function runReactForgetBabelPlugin( + text: string, + file: string +): ReactForgetBabelPluginResult { + const ast = parser.parse(text, { + sourceFilename: file, + plugins: ["typescript", "jsx"], + }); + const result = transformFromAstSync(ast, text, { + filename: file, + highlightCode: false, + retainLines: true, + plugins: [ReactForgetBabelPlugin], + }); + invariant( + result?.code != null, + `Expected BabelPluginReactForget to codegen successfully, got: ${result}` + ); + return { + ast: result.ast, + code: prettier.format(result.code, { + semi: true, + parser: "babel-ts", + }), + map: result.map, + }; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md index 5aef9100c0..6f12db3046 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md @@ -16,6 +16,7 @@ function f() { ```javascript function f() { const x = 1; + const x$0 = 2; 4; return 6; diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md index 93183c72e4..c63453d5f4 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md @@ -20,8 +20,10 @@ function component() { const z = []; const y = {}; y.z = z; + const x = {}; x.y = y; + mutate(x.y.z); } diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md index 7ace2cdec6..0c334b707d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md @@ -25,10 +25,8 @@ function component() { } else { z = $[0]; } - const c_1 = $[1] !== z; let y; - if (c_1) { y = {}; y.z = z; @@ -37,10 +35,8 @@ function component() { } else { y = $[2]; } - const c_3 = $[3] !== y; let x; - if (c_3) { x = {}; x.y = y; @@ -49,7 +45,6 @@ function component() { } else { x = $[4]; } - return x; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md index 837819c87c..983b62fdcb 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md @@ -36,7 +36,6 @@ function foo(cond) { a = {}; b = {}; c = {}; - while (cond) { const z = a; a = b; @@ -44,7 +43,6 @@ function foo(cond) { c = z; mutate(a, b); } - $[0] = cond; $[1] = a; $[2] = b; @@ -54,17 +52,12 @@ function foo(cond) { b = $[2]; c = $[3]; } - a; b; c; return a; } -``` -## Code - -```javascript function mutate(x, y) {} ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue-array.expect.md b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue-array.expect.md index dad0b2d4a6..e28968752c 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue-array.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue-array.expect.md @@ -25,7 +25,6 @@ function foo() { } else { a = $[0]; } - return a; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue.expect.md b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue.expect.md index 52ee4eef1f..cf1a8dd7f8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue.expect.md @@ -18,18 +18,13 @@ function g() { const $ = React.useMemoCache(); let x; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - x = { - y: { - z: 1, - }, - }; + x = { y: { z: 1 } }; x.y.z = x.y.z + 1; x.y.z = x.y.z * 2; $[0] = x; } else { x = $[0]; } - return x; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/call.expect.md b/compiler/forget/src/__tests__/fixtures/hir/call.expect.md index 7875dd9fed..a7c6bca8b9 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/call.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/call.expect.md @@ -20,10 +20,6 @@ function Component(props) { ```javascript function foo() {} -``` -## Code - -```javascript function Component(props) { const $ = React.useMemoCache(); let a; @@ -32,9 +28,7 @@ function Component(props) { a = []; b = {}; foo(a, b); - const _ =