Skip to content

Commit 6995d61

Browse files
committed
[compiler] Validate environment config while parsing plugin opts
Addresses a todo from a while back. We now validate environment options when parsing the plugin options, which means we can stop re-parsing/validating in later phases. ghstack-source-id: c835586 Pull Request resolved: #30726
1 parent b867691 commit 6995d61

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
import * as t from '@babel/types';
99
import {z} from 'zod';
10-
import {CompilerErrorDetailOptions} from '../CompilerError';
11-
import {ExternalFunction, PartialEnvironmentConfig} from '../HIR/Environment';
10+
import {CompilerError, CompilerErrorDetailOptions} from '../CompilerError';
11+
import {
12+
Environment,
13+
EnvironmentConfig,
14+
ExternalFunction,
15+
parseEnvironmentConfig,
16+
PartialEnvironmentConfig,
17+
} from '../HIR/Environment';
1218
import {hasOwnProperty} from '../Utils/utils';
1319

1420
const PanicThresholdOptionsSchema = z.enum([
@@ -32,7 +38,7 @@ const PanicThresholdOptionsSchema = z.enum([
3238
export type PanicThresholdOptions = z.infer<typeof PanicThresholdOptionsSchema>;
3339

3440
export type PluginOptions = {
35-
environment: PartialEnvironmentConfig | null;
41+
environment: EnvironmentConfig;
3642

3743
logger: Logger | null;
3844

@@ -188,7 +194,7 @@ export type Logger = {
188194
export const defaultOptions: PluginOptions = {
189195
compilationMode: 'infer',
190196
panicThreshold: 'none',
191-
environment: {},
197+
environment: parseEnvironmentConfig({}).unwrap(),
192198
logger: null,
193199
gating: null,
194200
noEmit: false,
@@ -212,7 +218,19 @@ export function parsePluginOptions(obj: unknown): PluginOptions {
212218
// normalize string configs to be case insensitive
213219
value = value.toLowerCase();
214220
}
215-
if (isCompilerFlag(key)) {
221+
if (key === 'environment') {
222+
const environmentResult = parseEnvironmentConfig(value);
223+
if (environmentResult.isErr()) {
224+
CompilerError.throwInvalidConfig({
225+
reason:
226+
'Error in validating environment config. This is an advanced setting and not meant to be used directly',
227+
description: environmentResult.unwrapErr().toString(),
228+
suggestions: null,
229+
loc: null,
230+
});
231+
}
232+
parsedOptions[key] = environmentResult.unwrap();
233+
} else if (isCompilerFlag(key)) {
216234
parsedOptions[key] = value;
217235
}
218236
}

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,7 @@ export function compileProgram(
296296
return;
297297
}
298298

299-
/*
300-
* TODO(lauren): Remove pass.opts.environment nullcheck once PluginOptions
301-
* is validated
302-
*/
303-
const environmentResult = parseEnvironmentConfig(pass.opts.environment ?? {});
304-
if (environmentResult.isErr()) {
305-
CompilerError.throwInvalidConfig({
306-
reason:
307-
'Error in validating environment config. This is an advanced setting and not meant to be used directly',
308-
description: environmentResult.unwrapErr().toString(),
309-
suggestions: null,
310-
loc: null,
311-
});
312-
}
313-
const environment = environmentResult.unwrap();
299+
const environment = pass.opts.environment;
314300
const restrictedImportsErr = validateRestrictedImports(program, environment);
315301
if (restrictedImportsErr) {
316302
handleError(restrictedImportsErr, pass, null);

0 commit comments

Comments
 (0)