mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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.
This commit is contained in:
@@ -212,7 +212,7 @@ export type EnvironmentConfig = {
|
||||
bailoutOnHoleyArrays: boolean;
|
||||
};
|
||||
|
||||
const DEFAULT_ENVIRONMENT_CONFIG: Readonly<EnvironmentConfig> = {
|
||||
export const DEFAULT_ENVIRONMENT_CONFIG: Readonly<EnvironmentConfig> = {
|
||||
customHooks: null,
|
||||
|
||||
memoizeJsxElements: true,
|
||||
@@ -231,6 +231,29 @@ const DEFAULT_ENVIRONMENT_CONFIG: Readonly<EnvironmentConfig> = {
|
||||
validateRefAccessDuringRender: false,
|
||||
};
|
||||
|
||||
export function parseConfigPragma(pragma: string): EnvironmentConfig {
|
||||
const config = { ...DEFAULT_ENVIRONMENT_CONFIG };
|
||||
for (const key of Object.keys(DEFAULT_ENVIRONMENT_CONFIG)) {
|
||||
if (!isEnvironmentConfigKey(key)) {
|
||||
continue;
|
||||
}
|
||||
const value = config[key];
|
||||
if (typeof value !== "boolean") {
|
||||
// We only support setting boolean flags via pragma strings
|
||||
continue;
|
||||
}
|
||||
if (pragma.includes(`@${key}:true`)) {
|
||||
config[key] = true as any;
|
||||
} else if (pragma.includes(`@${key}:false`)) {
|
||||
config[key] = false as any;
|
||||
} else if (pragma.includes(`@${key}`)) {
|
||||
config[key] = true as any;
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
function isEnvironmentConfigKey(key: string): key is keyof EnvironmentConfig {
|
||||
return Object.prototype.hasOwnProperty.call(DEFAULT_ENVIRONMENT_CONFIG, key);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,12 @@ export { assertTerminalSuccessorsExist } from "./AssertTerminalSuccessorsExist";
|
||||
export { assertValidMutableRanges } from "./AssertValidMutableRanges";
|
||||
export { lower } from "./BuildHIR";
|
||||
export { computeDominatorTree, computePostDominatorTree } from "./Dominator";
|
||||
export { Environment, Hook } from "./Environment";
|
||||
export {
|
||||
DEFAULT_ENVIRONMENT_CONFIG,
|
||||
Environment,
|
||||
Hook,
|
||||
parseConfigPragma,
|
||||
} from "./Environment";
|
||||
export * from "./HIR";
|
||||
export {
|
||||
markInstructionIds,
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import { DEFAULT_ENVIRONMENT_CONFIG, parseConfigPragma } from "..";
|
||||
|
||||
describe("parseConfigPragma()", () => {
|
||||
it("parses flags in various forms", () => {
|
||||
const config = parseConfigPragma(
|
||||
"@enableForest @validateFrozenLambdas:true @memoizeJsxElements:false"
|
||||
);
|
||||
// Validate defaults first to make sure that the parser is getting the value from the pragma,
|
||||
// and not just missing it and getting the default value
|
||||
expect(DEFAULT_ENVIRONMENT_CONFIG.enableForest).toBe(false);
|
||||
expect(DEFAULT_ENVIRONMENT_CONFIG.validateFrozenLambdas).toBe(false);
|
||||
expect(DEFAULT_ENVIRONMENT_CONFIG.memoizeJsxElements).toBe(true);
|
||||
|
||||
expect(config).toEqual({
|
||||
...DEFAULT_ENVIRONMENT_CONFIG,
|
||||
enableForest: true,
|
||||
validateFrozenLambdas: true,
|
||||
memoizeJsxElements: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -19,7 +19,15 @@ export {
|
||||
parsePluginOptions,
|
||||
run,
|
||||
} from "./Entrypoint";
|
||||
export { Effect, Hook, SourceLocation, ValueKind, printHIR } from "./HIR";
|
||||
export {
|
||||
DEFAULT_ENVIRONMENT_CONFIG,
|
||||
Effect,
|
||||
Hook,
|
||||
SourceLocation,
|
||||
ValueKind,
|
||||
parseConfigPragma,
|
||||
printHIR,
|
||||
} from "./HIR";
|
||||
export { printReactiveFunction } from "./ReactiveScopes";
|
||||
|
||||
declare global {
|
||||
|
||||
Reference in New Issue
Block a user