diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts index c99cec63ac..547783769d 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts @@ -86,13 +86,27 @@ export function* run( ): Generator { const contextIdentifiers = findContextIdentifiers(func); const env = new Environment(config ?? null, contextIdentifiers); + const ast = yield* runWithEnvironment(func, env); + return ast; +} + +/** + * Note: this is split from run() to make `config` out of scope, so that all + * access to feature flags has to be through the Environment for consistency. + */ +function* runWithEnvironment( + func: NodePath< + t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression + >, + env: Environment +): Generator { const hir = lower(func, env).unwrap(); yield log({ kind: "hir", name: "HIR", value: hir }); pruneMaybeThrows(hir); yield log({ kind: "hir", name: "PruneMaybeThrows", value: hir }); - if (config?.inlineUseMemo) { + if (env.inlineUseMemo) { inlineUseMemo(hir); yield log({ kind: "hir", name: "RewriteUseMemo", value: hir }); } @@ -245,8 +259,8 @@ export function* run( value: reactiveFunction, }); - let memoizeJsxElements = config?.memoizeJsxElements ?? true; - if (config?.enableForest) { + let memoizeJsxElements = env.memoizeJsxElements; + if (env.enableForest) { memoizeJsxElements = false; } pruneNonEscapingScopes(reactiveFunction, { memoizeJsxElements }); @@ -279,7 +293,7 @@ export function* run( }); } - if (config?.enableForest) { + if (env.enableForest) { yield* lowerToForest(reactiveFunction); } diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts index 0e1f2662d5..c0909c2b13 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts @@ -258,6 +258,8 @@ export class Environment { enableAssumeHooksFollowRulesOfReact: boolean; enableTreatHooksAsFunctions: boolean; enableNoAliasOptimizations: boolean; + inlineUseMemo: boolean; + memoizeJsxElements: boolean; disableAllMemoization: boolean; enableEmitFreeze: ExternalFunction | null; enableMergeConsecutiveScopes: boolean; @@ -320,6 +322,8 @@ export class Environment { this.assertValidMutableRanges = config?.assertValidMutableRanges ?? false; this.validateNoSetStateInRender = config?.validateNoSetStateInRender ?? false; + this.inlineUseMemo = config?.inlineUseMemo ?? false; + this.memoizeJsxElements = config?.memoizeJsxElements ?? true; this.bailoutOnHoleyArrays = config?.bailoutOnHoleyArrays ?? false; this.enableForest = config?.enableForest ?? false;