[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: b19806e843e1254716705b33dcf86afb7223f6c7 Pull Request resolved: https://github.com/facebook/react/pull/30726
Joe Savona committed
Aug 16, 2024 at 16:45 UTC
177b2419b2d8a3d14c3f3304bb7e300985d6f377
2 files changed
+22
-21
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts
+21
-5
@@ -7,8 +7,12 @@
7
8
import * as t from '@babel/types';
9
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
+ EnvironmentConfig,
13
+ ExternalFunction,
14
+ parseEnvironmentConfig,
15
+} from '../HIR/Environment';
16
import {hasOwnProperty} from '../Utils/utils';
17
18
const PanicThresholdOptionsSchema = z.enum([
@@ -32,7 +36,7 @@ const PanicThresholdOptionsSchema = z.enum([
36
export type PanicThresholdOptions = z.infer<typeof PanicThresholdOptionsSchema>;
37
38
export type PluginOptions = {
35
- environment: PartialEnvironmentConfig | null;
39
+ environment: EnvironmentConfig;
40
41
logger: Logger | null;
42
@@ -194,7 +198,7 @@ export type Logger = {
198
export const defaultOptions: PluginOptions = {
199
compilationMode: 'infer',
200
panicThreshold: 'none',
197
- environment: {},
201
+ environment: parseEnvironmentConfig({}).unwrap(),
202
logger: null,
203
gating: null,
204
noEmit: false,
@@ -218,7 +222,19 @@ export function parsePluginOptions(obj: unknown): PluginOptions {
222
// normalize string configs to be case insensitive
223
value = value.toLowerCase();
224
}
221
- if (isCompilerFlag(key)) {
225
+ if (key === 'environment') {
226
+ const environmentResult = parseEnvironmentConfig(value);
227
+ if (environmentResult.isErr()) {
228
+ CompilerError.throwInvalidConfig({
229
+ reason:
230
+ 'Error in validating environment config. This is an advanced setting and not meant to be used directly',
231
+ description: environmentResult.unwrapErr().toString(),
232
+ suggestions: null,
233
+ loc: null,
234
+ });
235
+ }
236
+ parsedOptions[key] = environmentResult.unwrap();
237
+ } else if (isCompilerFlag(key)) {
238
parsedOptions[key] = value;
239
}
240
}
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
+1
-16
@@ -16,7 +16,6 @@ import {
16
EnvironmentConfig,
17
ExternalFunction,
18
ReactFunctionType,
19
- parseEnvironmentConfig,
19
tryParseExternalFunction,
20
} from '../HIR/Environment';
21
import {CodegenFunction} from '../ReactiveScopes';
@@ -292,21 +291,7 @@ export function compileProgram(
291
return;
292
}
293
295
- /*
296
- * TODO(lauren): Remove pass.opts.environment nullcheck once PluginOptions
297
- * is validated
298
- */
299
- const environmentResult = parseEnvironmentConfig(pass.opts.environment ?? {});
300
- if (environmentResult.isErr()) {
301
- CompilerError.throwInvalidConfig({
302
- reason:
303
- 'Error in validating environment config. This is an advanced setting and not meant to be used directly',
304
- description: environmentResult.unwrapErr().toString(),
305
- suggestions: null,
306
- loc: null,
307
- });
308
- }
309
- const environment = environmentResult.unwrap();
294
+ const environment = pass.opts.environment;
295
const restrictedImportsErr = validateRestrictedImports(program, environment);
296
if (restrictedImportsErr) {
297
handleError(restrictedImportsErr, pass, null);