69
export const InstrumentationSchema = z
70
.object({
71
fn: ExternalFunctionSchema,
72
- gating: ExternalFunctionSchema.nullish(),
73
- globalGating: z.string().nullish(),
72
+ gating: ExternalFunctionSchema.nullable(),
73
+ globalGating: z.string().nullable(),
74
})
75
.refine(
76
opts => opts.gating != null || opts.globalGating != null,
147
*/
148
149
const EnvironmentConfigSchema = z.object({
150
- customHooks: z.map(z.string(), HookSchema).optional().default(new Map()),
150
+ customHooks: z.map(z.string(), HookSchema).default(new Map()),
151
152
/**
153
* A function that, given the name of a module, can optionally return a description
248
*
249
* The symbol configuration is set for backwards compatability with pre-React 19 transforms
250
*/
251
- inlineJsxTransform: ReactElementSymbolSchema.nullish(),
251
+ inlineJsxTransform: ReactElementSymbolSchema.nullable().default(null),
252
253
/*
254
* Enable validation of hooks to partially check that the component honors the rules of hooks.
339
* }
340
* }
341
*/
342
- enableEmitFreeze: ExternalFunctionSchema.nullish(),
342
+ enableEmitFreeze: ExternalFunctionSchema.nullable().default(null),
343
344
- enableEmitHookGuards: ExternalFunctionSchema.nullish(),
344
+ enableEmitHookGuards: ExternalFunctionSchema.nullable().default(null),
345
346
/**
347
* Enable instruction reordering. See InstructionReordering.ts for the details
425
* }
426
*
427
*/
428
- enableEmitInstrumentForget: InstrumentationSchema.nullish(),
428
+ enableEmitInstrumentForget: InstrumentationSchema.nullable().default(null),
429
430
// Enable validation of mutable ranges
431
assertValidMutableRanges: z.boolean().default(false),
464
*/
465
throwUnknownException__testonly: z.boolean().default(false),
466
467
- enableSharedRuntime__testonly: z.boolean().default(false),
468
-
467
/**
468
* Enables deps of a function epxression to be treated as conditional. This
469
* makes sure we don't load a dep when it's a property (to check if it has
501
* computed one. This detects cases where rules of react violations may cause the
502
* compiled code to behave differently than the original.
503
*/
506
- enableChangeDetectionForDebugging: ExternalFunctionSchema.nullish(),
504
+ enableChangeDetectionForDebugging:
505
+ ExternalFunctionSchema.nullable().default(null),
506
507
/**
508
* The react native re-animated library uses custom Babel transforms that
542
*
543
* Here the variables `ref` and `myRef` will be typed as Refs.
544
*/
546
- enableTreatRefLikeIdentifiersAsRefs: z.boolean().nullable().default(false),
545
+ enableTreatRefLikeIdentifiersAsRefs: z.boolean().default(false),
546
547
/*
548
* If specified a value, the compiler lowers any calls to `useContext` to use
564
* const {foo, bar} = useCompiledContext(MyContext, (c) => [c.foo, c.bar]);
565
* ```
566
*/
568
- lowerContextAccess: ExternalFunctionSchema.nullish(),
567
+ lowerContextAccess: ExternalFunctionSchema.nullable().default(null),
568
});
569
570
export type EnvironmentConfig = z.infer<typeof EnvironmentConfigSchema>;
571
573
-export function parseConfigPragma(pragma: string): EnvironmentConfig {
572
+/**
573
+ * For test fixtures and playground only.
574
+ *
575
+ * Pragmas are straightforward to parse for boolean options (`:true` and
576
+ * `:false`). These are 'enabled' config values for non-boolean configs (i.e.
577
+ * what is used when parsing `:true`).
578
+ */
579
+const testComplexConfigDefaults: PartialEnvironmentConfig = {
580
+ validateNoCapitalizedCalls: [],
581
+ enableChangeDetectionForDebugging: {
582
+ source: 'react-compiler-runtime',
583
+ importSpecifierName: '$structuralCheck',
584
+ },
585
+ enableEmitFreeze: {
586
+ source: 'react-compiler-runtime',
587
+ importSpecifierName: 'makeReadOnly',
588
+ },
589
+ enableEmitInstrumentForget: {
590
+ fn: {
591
+ source: 'react-compiler-runtime',
592
+ importSpecifierName: 'useRenderCounter',
593
+ },
594
+ gating: {
595
+ source: 'react-compiler-runtime',
596
+ importSpecifierName: 'shouldInstrument',
597
+ },
598
+ globalGating: '__DEV__',
599
+ },
600
+ enableEmitHookGuards: {
601
+ source: 'react-compiler-runtime',
602
+ importSpecifierName: '$dispatcherGuard',
603
+ },
604
+ inlineJsxTransform: {
605
+ elementSymbol: 'react.transitional.element',
606
+ globalDevVar: 'DEV',
607
+ },
608
+ lowerContextAccess: {
609
+ source: 'react-compiler-runtime',
610
+ importSpecifierName: 'useContext_withSelector',
611
+ },
612
+};
613
+
614
+/**
615
+ * For snap test fixtures and playground only.
616
+ */
617
+export function parseConfigPragmaForTests(pragma: string): EnvironmentConfig {
618
const maybeConfig: any = {};
619
// Get the defaults to programmatically check for boolean properties
620
const defaultConfig = EnvironmentConfigSchema.parse({});
624
continue;
625
}
626
const keyVal = token.slice(1);
583
- let [key, val]: any = keyVal.split(':');
584
-
585
- if (key === 'validateNoCapitalizedCalls') {
586
- maybeConfig[key] = [];
587
- continue;
588
- }
627
+ let [key, val = undefined] = keyVal.split(':');
628
+ const isSet = val === undefined || val === 'true';
629
590
- if (
591
- key === 'enableChangeDetectionForDebugging' &&
592
- (val === undefined || val === 'true')
593
- ) {
594
- maybeConfig[key] = {
595
- source: 'react-compiler-runtime',
596
- importSpecifierName: '$structuralCheck',
597
- };
630
+ if (isSet && key in testComplexConfigDefaults) {
631
+ maybeConfig[key] =
632
+ testComplexConfigDefaults[key as keyof PartialEnvironmentConfig];
633
continue;
634
}
635
644
props.push({type: 'name', name: elt});
645
}
646
}
612
- console.log([valSplit[0], props.map(x => x.name ?? '*').join('.')]);
647
maybeConfig[key] = [[valSplit[0], props]];
648
}
649
continue;
654
continue;
655
}
656
if (val === undefined || val === 'true') {
623
- val = true;
657
+ maybeConfig[key] = true;
658
} else {
625
- val = false;
659
+ maybeConfig[key] = false;
660
}
627
- maybeConfig[key] = val;
661
}
662
663
const config = EnvironmentConfigSchema.safeParse(maybeConfig);