From 06376b906d173ee03e49bbbb6277393b9015cf0a Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Thu, 14 Dec 2023 19:42:24 -0500 Subject: [PATCH] [patch] Generate hook guards as function expressions --- Arrow functions are not compatible with es3, which we sometimes target. This helps us make babel transform logic less complex --- .../src/ReactiveScopes/CodegenReactiveFunction.ts | 3 ++- .../compiler/flag-enable-emit-hook-guards.expect.md | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 00f4012838..701554d469 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -1019,7 +1019,8 @@ function createCallExpression( const hookGuard = config.enableEmitHookGuards; if (hookGuard != null && isHook) { - const iife = t.arrowFunctionExpression( + const iife = t.functionExpression( + null, [], t.blockStatement([ createHookGuard( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/flag-enable-emit-hook-guards.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/flag-enable-emit-hook-guards.expect.md index cd5e0374b6..813cc62bb5 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/flag-enable-emit-hook-guards.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/flag-enable-emit-hook-guards.expect.md @@ -66,7 +66,7 @@ function Component(t47) { } else { t0 = $[0]; } - const [state, setState] = (() => { + const [state, setState] = (function () { try { $dispatcherGuard(2); return useState(t0); @@ -92,7 +92,7 @@ function Component(t47) { t1 = $[2]; t2 = $[3]; } - (() => { + (function () { try { $dispatcherGuard(2); return useEffect(t1, t2); @@ -101,11 +101,11 @@ function Component(t47) { } })(); print(identity(value + state)); - return (() => { + return (function () { try { $dispatcherGuard(2); return ObjectWithHooks.useIdentity( - (() => { + (function () { try { $dispatcherGuard(2); return useContext(MyContext);