Skip to content

Commit

Permalink
[compiler] Validate environment config while parsing plugin opts
Browse files Browse the repository at this point in the history
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: acb52a9dd58576fde29613486882c410471a8c24
Pull Request resolved: #30726
  • Loading branch information
josephsavona committed Aug 16, 2024
1 parent b867691 commit 8c2cb78
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

import * as t from '@babel/types';
import {z} from 'zod';
import {CompilerErrorDetailOptions} from '../CompilerError';
import {ExternalFunction, PartialEnvironmentConfig} from '../HIR/Environment';
import {CompilerError, CompilerErrorDetailOptions} from '../CompilerError';
import {
EnvironmentConfig,
ExternalFunction,
parseEnvironmentConfig,
} from '../HIR/Environment';
import {hasOwnProperty} from '../Utils/utils';

const PanicThresholdOptionsSchema = z.enum([
Expand All @@ -32,7 +36,7 @@ const PanicThresholdOptionsSchema = z.enum([
export type PanicThresholdOptions = z.infer<typeof PanicThresholdOptionsSchema>;

export type PluginOptions = {
environment: PartialEnvironmentConfig | null;
environment: EnvironmentConfig;

logger: Logger | null;

Expand Down Expand Up @@ -188,7 +192,7 @@ export type Logger = {
export const defaultOptions: PluginOptions = {
compilationMode: 'infer',
panicThreshold: 'none',
environment: {},
environment: parseEnvironmentConfig({}).unwrap(),
logger: null,
gating: null,
noEmit: false,
Expand All @@ -212,7 +216,19 @@ export function parsePluginOptions(obj: unknown): PluginOptions {
// normalize string configs to be case insensitive
value = value.toLowerCase();
}
if (isCompilerFlag(key)) {
if (key === 'environment') {
const environmentResult = parseEnvironmentConfig(value);
if (environmentResult.isErr()) {
CompilerError.throwInvalidConfig({
reason:
'Error in validating environment config. This is an advanced setting and not meant to be used directly',
description: environmentResult.unwrapErr().toString(),
suggestions: null,
loc: null,
});
}
parsedOptions[key] = environmentResult.unwrap();
} else if (isCompilerFlag(key)) {
parsedOptions[key] = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
EnvironmentConfig,
ExternalFunction,
ReactFunctionType,
parseEnvironmentConfig,
tryParseExternalFunction,
} from '../HIR/Environment';
import {CodegenFunction} from '../ReactiveScopes';
Expand Down Expand Up @@ -296,21 +295,7 @@ export function compileProgram(
return;
}

/*
* TODO(lauren): Remove pass.opts.environment nullcheck once PluginOptions
* is validated
*/
const environmentResult = parseEnvironmentConfig(pass.opts.environment ?? {});
if (environmentResult.isErr()) {
CompilerError.throwInvalidConfig({
reason:
'Error in validating environment config. This is an advanced setting and not meant to be used directly',
description: environmentResult.unwrapErr().toString(),
suggestions: null,
loc: null,
});
}
const environment = environmentResult.unwrap();
const environment = pass.opts.environment;
const restrictedImportsErr = validateRestrictedImports(program, environment);
if (restrictedImportsErr) {
handleError(restrictedImportsErr, pass, null);
Expand Down

0 comments on commit 8c2cb78

Please sign in to comment.