@samitouri / QOS-React / commits / cadcf3de2d

Reusable function for parsing config strings

Adds a helper function for parsing pragma strings to the compiler itself, and exports it. This will be used in follow-ups to make Snap, Sprout, and Playground all use the same pragma parser. The helper also starts from the default values, so adopting this will also make it easy for all those places to have the same defaults automatically.

Joe Savona committed Oct 5, 2023 at 10:18 UTC cadcf3de2d062d78c755a1d7672377e5933656be
4 files changed +67 -3
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+24 -1
@@ -212,7 +212,7 @@ export type EnvironmentConfig = {
212 bailoutOnHoleyArrays: boolean;
213 };
214
215 -const DEFAULT_ENVIRONMENT_CONFIG: Readonly<EnvironmentConfig> = {
215 +export const DEFAULT_ENVIRONMENT_CONFIG: Readonly<EnvironmentConfig> = {
216 customHooks: null,
217
218 memoizeJsxElements: true,
@@ -231,6 +231,29 @@ const DEFAULT_ENVIRONMENT_CONFIG: Readonly<EnvironmentConfig> = {
231 validateRefAccessDuringRender: false,
232 };
233
234 +export function parseConfigPragma(pragma: string): EnvironmentConfig {
235 + const config = { ...DEFAULT_ENVIRONMENT_CONFIG };
236 + for (const key of Object.keys(DEFAULT_ENVIRONMENT_CONFIG)) {
237 + if (!isEnvironmentConfigKey(key)) {
238 + continue;
239 + }
240 + const value = config[key];
241 + if (typeof value !== "boolean") {
242 + // We only support setting boolean flags via pragma strings
243 + continue;
244 + }
245 + if (pragma.includes(`@${key}:true`)) {
246 + config[key] = true as any;
247 + } else if (pragma.includes(`@${key}:false`)) {
248 + config[key] = false as any;
249 + } else if (pragma.includes(`@${key}`)) {
250 + config[key] = true as any;
251 + }
252 + }
253 +
254 + return config;
255 +}
256 +
257 function isEnvironmentConfigKey(key: string): key is keyof EnvironmentConfig {
258 return Object.prototype.hasOwnProperty.call(DEFAULT_ENVIRONMENT_CONFIG, key);
259 }
compiler/packages/babel-plugin-react-forget/src/HIR/index.ts
+6 -1
@@ -10,7 +10,12 @@ export { assertTerminalSuccessorsExist } from "./AssertTerminalSuccessorsExist";
10 export { assertValidMutableRanges } from "./AssertValidMutableRanges";
11 export { lower } from "./BuildHIR";
12 export { computeDominatorTree, computePostDominatorTree } from "./Dominator";
13 -export { Environment, Hook } from "./Environment";
13 +export {
14 + DEFAULT_ENVIRONMENT_CONFIG,
15 + Environment,
16 + Hook,
17 + parseConfigPragma,
18 +} from "./Environment";
19 export * from "./HIR";
20 export {
21 markInstructionIds,
compiler/packages/babel-plugin-react-forget/src/__tests__/parseConfigPragma-test.ts new
+28
@@ -0,0 +1,28 @@
1 +/**
2 + * Copyright (c) Meta Platforms, Inc. and affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +
8 +import { DEFAULT_ENVIRONMENT_CONFIG, parseConfigPragma } from "..";
9 +
10 +describe("parseConfigPragma()", () => {
11 + it("parses flags in various forms", () => {
12 + const config = parseConfigPragma(
13 + "@enableForest @validateFrozenLambdas:true @memoizeJsxElements:false"
14 + );
15 + // Validate defaults first to make sure that the parser is getting the value from the pragma,
16 + // and not just missing it and getting the default value
17 + expect(DEFAULT_ENVIRONMENT_CONFIG.enableForest).toBe(false);
18 + expect(DEFAULT_ENVIRONMENT_CONFIG.validateFrozenLambdas).toBe(false);
19 + expect(DEFAULT_ENVIRONMENT_CONFIG.memoizeJsxElements).toBe(true);
20 +
21 + expect(config).toEqual({
22 + ...DEFAULT_ENVIRONMENT_CONFIG,
23 + enableForest: true,
24 + validateFrozenLambdas: true,
25 + memoizeJsxElements: false,
26 + });
27 + });
28 +});
compiler/packages/babel-plugin-react-forget/src/index.ts
+9 -1
@@ -19,7 +19,15 @@ export {
19 parsePluginOptions,
20 run,
21 } from "./Entrypoint";
22 -export { Effect, Hook, SourceLocation, ValueKind, printHIR } from "./HIR";
22 +export {
23 + DEFAULT_ENVIRONMENT_CONFIG,
24 + Effect,
25 + Hook,
26 + SourceLocation,
27 + ValueKind,
28 + parseConfigPragma,
29 + printHIR,
30 +} from "./HIR";
31 export { printReactiveFunction } from "./ReactiveScopes";
32
33 declare global {