Skip to content

Commit 27389f3

Browse files
committed
[compiler][be] Move test pragma to separate file
`Environment.ts` is getting complex so let's separate test / playground parsing logic from it
1 parent 489fee5 commit 27389f3

File tree

6 files changed

+211
-199
lines changed

6 files changed

+211
-199
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 2 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ import * as t from '@babel/types';
99
import {ZodError, z} from 'zod';
1010
import {fromZodError} from 'zod-validation-error';
1111
import {CompilerError} from '../CompilerError';
12-
import {
13-
CompilationMode,
14-
defaultOptions,
15-
Logger,
16-
PanicThresholdOptions,
17-
parsePluginOptions,
18-
PluginOptions,
19-
ProgramContext,
20-
} from '../Entrypoint';
12+
import {Logger, ProgramContext} from '../Entrypoint';
2113
import {Err, Ok, Result} from '../Utils/Result';
2214
import {
2315
DEFAULT_GLOBALS,
@@ -158,7 +150,7 @@ export type Hook = z.infer<typeof HookSchema>;
158150
* missing some recursive Object / Function shapeIds
159151
*/
160152

161-
const EnvironmentConfigSchema = z.object({
153+
export const EnvironmentConfigSchema = z.object({
162154
customHooks: z.map(z.string(), HookSchema).default(new Map()),
163155

164156
/**
@@ -640,191 +632,6 @@ const EnvironmentConfigSchema = z.object({
640632

641633
export type EnvironmentConfig = z.infer<typeof EnvironmentConfigSchema>;
642634

643-
/**
644-
* For test fixtures and playground only.
645-
*
646-
* Pragmas are straightforward to parse for boolean options (`:true` and
647-
* `:false`). These are 'enabled' config values for non-boolean configs (i.e.
648-
* what is used when parsing `:true`).
649-
*/
650-
const testComplexConfigDefaults: PartialEnvironmentConfig = {
651-
validateNoCapitalizedCalls: [],
652-
enableChangeDetectionForDebugging: {
653-
source: 'react-compiler-runtime',
654-
importSpecifierName: '$structuralCheck',
655-
},
656-
enableEmitFreeze: {
657-
source: 'react-compiler-runtime',
658-
importSpecifierName: 'makeReadOnly',
659-
},
660-
enableEmitInstrumentForget: {
661-
fn: {
662-
source: 'react-compiler-runtime',
663-
importSpecifierName: 'useRenderCounter',
664-
},
665-
gating: {
666-
source: 'react-compiler-runtime',
667-
importSpecifierName: 'shouldInstrument',
668-
},
669-
globalGating: 'DEV',
670-
},
671-
enableEmitHookGuards: {
672-
source: 'react-compiler-runtime',
673-
importSpecifierName: '$dispatcherGuard',
674-
},
675-
inlineJsxTransform: {
676-
elementSymbol: 'react.transitional.element',
677-
globalDevVar: 'DEV',
678-
},
679-
lowerContextAccess: {
680-
source: 'react-compiler-runtime',
681-
importSpecifierName: 'useContext_withSelector',
682-
},
683-
inferEffectDependencies: [
684-
{
685-
function: {
686-
source: 'react',
687-
importSpecifierName: 'useEffect',
688-
},
689-
numRequiredArgs: 1,
690-
},
691-
{
692-
function: {
693-
source: 'shared-runtime',
694-
importSpecifierName: 'useSpecialEffect',
695-
},
696-
numRequiredArgs: 2,
697-
},
698-
{
699-
function: {
700-
source: 'useEffectWrapper',
701-
importSpecifierName: 'default',
702-
},
703-
numRequiredArgs: 1,
704-
},
705-
],
706-
};
707-
708-
/**
709-
* For snap test fixtures and playground only.
710-
*/
711-
function parseConfigPragmaEnvironmentForTest(
712-
pragma: string,
713-
): EnvironmentConfig {
714-
const maybeConfig: any = {};
715-
// Get the defaults to programmatically check for boolean properties
716-
const defaultConfig = EnvironmentConfigSchema.parse({});
717-
718-
for (const token of pragma.split(' ')) {
719-
if (!token.startsWith('@')) {
720-
continue;
721-
}
722-
const keyVal = token.slice(1);
723-
let [key, val = undefined] = keyVal.split(':');
724-
const isSet = val === undefined || val === 'true';
725-
726-
if (isSet && key in testComplexConfigDefaults) {
727-
maybeConfig[key] =
728-
testComplexConfigDefaults[key as keyof PartialEnvironmentConfig];
729-
continue;
730-
}
731-
732-
if (key === 'customMacros' && val) {
733-
const valSplit = val.split('.');
734-
if (valSplit.length > 0) {
735-
const props = [];
736-
for (const elt of valSplit.slice(1)) {
737-
if (elt === '*') {
738-
props.push({type: 'wildcard'});
739-
} else if (elt.length > 0) {
740-
props.push({type: 'name', name: elt});
741-
}
742-
}
743-
maybeConfig[key] = [[valSplit[0], props]];
744-
}
745-
continue;
746-
}
747-
748-
if (
749-
key !== 'enableResetCacheOnSourceFileChanges' &&
750-
typeof defaultConfig[key as keyof EnvironmentConfig] !== 'boolean'
751-
) {
752-
// skip parsing non-boolean properties
753-
continue;
754-
}
755-
if (val === undefined || val === 'true') {
756-
maybeConfig[key] = true;
757-
} else {
758-
maybeConfig[key] = false;
759-
}
760-
}
761-
const config = EnvironmentConfigSchema.safeParse(maybeConfig);
762-
if (config.success) {
763-
/**
764-
* Unless explicitly enabled, do not insert HMR handling code
765-
* in test fixtures or playground to reduce visual noise.
766-
*/
767-
if (config.data.enableResetCacheOnSourceFileChanges == null) {
768-
config.data.enableResetCacheOnSourceFileChanges = false;
769-
}
770-
return config.data;
771-
}
772-
CompilerError.invariant(false, {
773-
reason: 'Internal error, could not parse config from pragma string',
774-
description: `${fromZodError(config.error)}`,
775-
loc: null,
776-
suggestions: null,
777-
});
778-
}
779-
export function parseConfigPragmaForTests(
780-
pragma: string,
781-
defaults: {
782-
compilationMode: CompilationMode;
783-
},
784-
): PluginOptions {
785-
const environment = parseConfigPragmaEnvironmentForTest(pragma);
786-
let compilationMode: CompilationMode = defaults.compilationMode;
787-
let panicThreshold: PanicThresholdOptions = 'all_errors';
788-
let noEmit: boolean = defaultOptions.noEmit;
789-
for (const token of pragma.split(' ')) {
790-
if (!token.startsWith('@')) {
791-
continue;
792-
}
793-
switch (token) {
794-
case '@compilationMode(annotation)': {
795-
compilationMode = 'annotation';
796-
break;
797-
}
798-
case '@compilationMode(infer)': {
799-
compilationMode = 'infer';
800-
break;
801-
}
802-
case '@compilationMode(all)': {
803-
compilationMode = 'all';
804-
break;
805-
}
806-
case '@compilationMode(syntax)': {
807-
compilationMode = 'syntax';
808-
break;
809-
}
810-
case '@panicThreshold(none)': {
811-
panicThreshold = 'none';
812-
break;
813-
}
814-
case '@noEmit': {
815-
noEmit = true;
816-
break;
817-
}
818-
}
819-
}
820-
return parsePluginOptions({
821-
environment,
822-
compilationMode,
823-
panicThreshold,
824-
noEmit,
825-
});
826-
}
827-
828635
export type PartialEnvironmentConfig = Partial<EnvironmentConfig>;
829636

830637
export type ReactFunctionType = 'Component' | 'Hook' | 'Other';

compiler/packages/babel-plugin-react-compiler/src/HIR/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export {buildReactiveScopeTerminalsHIR} from './BuildReactiveScopeTerminalsHIR';
1717
export {computeDominatorTree, computePostDominatorTree} from './Dominator';
1818
export {
1919
Environment,
20-
parseConfigPragmaForTests,
2120
validateEnvironmentConfig,
2221
type EnvironmentConfig,
2322
type ExternalFunction,

0 commit comments

Comments
 (0)