@samitouri / QOS-React / commits / 9fc5e55b56

[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.

Joe Savona committed Oct 3, 2023 at 11:46 UTC 9fc5e55b56fffdbbf5429121647fc386e8e16973
2 files changed +22 -4
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts
+18 -4
@@ -86,13 +86,27 @@ export function* run(
86 ): Generator<CompilerPipelineValue, CodegenFunction> {
87 const contextIdentifiers = findContextIdentifiers(func);
88 const env = new Environment(config ?? null, contextIdentifiers);
89 + const ast = yield* runWithEnvironment(func, env);
90 + return ast;
91 +}
92 +
93 +/**
94 + * Note: this is split from run() to make `config` out of scope, so that all
95 + * access to feature flags has to be through the Environment for consistency.
96 + */
97 +function* runWithEnvironment(
98 + func: NodePath<
99 + t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression
100 + >,
101 + env: Environment
102 +): Generator<CompilerPipelineValue, CodegenFunction> {
103 const hir = lower(func, env).unwrap();
104 yield log({ kind: "hir", name: "HIR", value: hir });
105
106 pruneMaybeThrows(hir);
107 yield log({ kind: "hir", name: "PruneMaybeThrows", value: hir });
108
95 - if (config?.inlineUseMemo) {
109 + if (env.inlineUseMemo) {
110 inlineUseMemo(hir);
111 yield log({ kind: "hir", name: "RewriteUseMemo", value: hir });
112 }
@@ -245,8 +259,8 @@ export function* run(
259 value: reactiveFunction,
260 });
261
248 - let memoizeJsxElements = config?.memoizeJsxElements ?? true;
249 - if (config?.enableForest) {
262 + let memoizeJsxElements = env.memoizeJsxElements;
263 + if (env.enableForest) {
264 memoizeJsxElements = false;
265 }
266 pruneNonEscapingScopes(reactiveFunction, { memoizeJsxElements });
@@ -279,7 +293,7 @@ export function* run(
293 });
294 }
295
282 - if (config?.enableForest) {
296 + if (env.enableForest) {
297 yield* lowerToForest(reactiveFunction);
298 }
299
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+4
@@ -258,6 +258,8 @@ export class Environment {
258 enableAssumeHooksFollowRulesOfReact: boolean;
259 enableTreatHooksAsFunctions: boolean;
260 enableNoAliasOptimizations: boolean;
261 + inlineUseMemo: boolean;
262 + memoizeJsxElements: boolean;
263 disableAllMemoization: boolean;
264 enableEmitFreeze: ExternalFunction | null;
265 enableMergeConsecutiveScopes: boolean;
@@ -320,6 +322,8 @@ export class Environment {
322 this.assertValidMutableRanges = config?.assertValidMutableRanges ?? false;
323 this.validateNoSetStateInRender =
324 config?.validateNoSetStateInRender ?? false;
325 + this.inlineUseMemo = config?.inlineUseMemo ?? false;
326 + this.memoizeJsxElements = config?.memoizeJsxElements ?? true;
327 this.bailoutOnHoleyArrays = config?.bailoutOnHoleyArrays ?? false;
328 this.enableForest = config?.enableForest ?? false;
329