From 9fc5e55b56fffdbbf5429121647fc386e8e16973 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 3 Oct 2023 11:46:33 -0700 Subject: [PATCH] [be] Access all features flags through Environment Most of our feature flags are accessed via the `Environment`, but a few cases have slipped in where we look at the `config` object directly. The problem is that the config object doesn't set defaults, so the check is effectively encoding what the default is. This PR moves to always accessing flags off of the environment, and adds a few flags that weren't yet defined there. --- .../src/Entrypoint/Pipeline.ts | 22 +++++++++++++++---- .../src/HIR/Environment.ts | 4 ++++ 2 files changed, 22 insertions(+), 4 deletions(-) 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;