Disallow 'null' as a general blank option placeholder.

This commit is contained in:
Logan Smyth 2017-10-18 00:19:09 -07:00
parent 64abf75d1f
commit 0f31ce5686
2 changed files with 74 additions and 69 deletions

View File

@ -12,30 +12,36 @@ import type {
RootInputSourceMapOption, RootInputSourceMapOption,
} from "./options"; } from "./options";
export function assertSourceMaps(key: string, value: mixed): ?SourceMapsOption { export function assertSourceMaps(
key: string,
value: mixed,
): SourceMapsOption | void {
if ( if (
value != null && value !== undefined &&
typeof value !== "boolean" && typeof value !== "boolean" &&
value !== "inline" && value !== "inline" &&
value !== "both" value !== "both"
) { ) {
throw new Error( throw new Error(
`.${key} must be a boolean, "inline", "both", null, or undefined`, `.${key} must be a boolean, "inline", "both", or undefined`,
); );
} }
return value; return value;
} }
export function assertCompact(key: string, value: mixed): ?CompactOption { export function assertCompact(key: string, value: mixed): CompactOption | void {
if (value != null && typeof value !== "boolean" && value !== "auto") { if (value !== undefined && typeof value !== "boolean" && value !== "auto") {
throw new Error(`.${key} must be a boolean, "auto", null, or undefined`); throw new Error(`.${key} must be a boolean, "auto", or undefined`);
} }
return value; return value;
} }
export function assertSourceType(key: string, value: mixed): ?SourceTypeOption { export function assertSourceType(
if (value != null && value !== "module" && value !== "script") { key: string,
throw new Error(`.${key} must be "module", "script", null, or undefined`); value: mixed,
): SourceTypeOption | void {
if (value !== undefined && value !== "module" && value !== "script") {
throw new Error(`.${key} must be "module", "script", or undefined`);
} }
return value; return value;
} }
@ -43,48 +49,49 @@ export function assertSourceType(key: string, value: mixed): ?SourceTypeOption {
export function assertInputSourceMap( export function assertInputSourceMap(
key: string, key: string,
value: mixed, value: mixed,
): ?RootInputSourceMapOption { ): RootInputSourceMapOption | void {
if ( if (
value != null && value !== undefined &&
typeof value !== "boolean" && typeof value !== "boolean" &&
typeof value !== "object" (typeof value !== "object" || !value)
) { ) {
throw new Error( throw new Error(".inputSourceMap must be a boolean, object, or undefined");
".inputSourceMap must be a boolean, object, null, or undefined",
);
} }
return value; return value;
} }
export function assertString(key: string, value: mixed): ?string { export function assertString(key: string, value: mixed): string | void {
if (value != null && typeof value !== "string") { if (value !== undefined && typeof value !== "string") {
throw new Error(`.${key} must be a string, null, or undefined`); throw new Error(`.${key} must be a string, or undefined`);
} }
return value; return value;
} }
export function assertFunction(key: string, value: mixed): ?Function { export function assertFunction(key: string, value: mixed): Function | void {
if (value != null && typeof value !== "function") { if (value !== undefined && typeof value !== "function") {
throw new Error(`.${key} must be a function, null, or undefined`); throw new Error(`.${key} must be a function, or undefined`);
} }
return value; return value;
} }
export function assertBoolean(key: string, value: mixed): ?boolean { export function assertBoolean(key: string, value: mixed): boolean | void {
if (value != null && typeof value !== "boolean") { if (value !== undefined && typeof value !== "boolean") {
throw new Error(`.${key} must be a boolean, null, or undefined`); throw new Error(`.${key} must be a boolean, or undefined`);
} }
return value; return value;
} }
export function assertObject(key: string, value: mixed): ?{} { export function assertObject(key: string, value: mixed): {} | void {
if (value != null && (typeof value !== "object" || Array.isArray(value))) { if (
throw new Error(`.${key} must be an object, null, or undefined`); value !== undefined &&
(typeof value !== "object" || Array.isArray(value) || !value)
) {
throw new Error(`.${key} must be an object, or undefined`);
} }
return value; return value;
} }
export function assertIgnoreList(key: string, value: mixed): ?IgnoreList { export function assertIgnoreList(key: string, value: mixed): IgnoreList | void {
const arr = assertArray(key, value); const arr = assertArray(key, value);
if (arr) { if (arr) {
arr.forEach((item, i) => assertIgnoreItem(key, i, item)); arr.forEach((item, i) => assertIgnoreItem(key, i, item));
@ -102,13 +109,13 @@ function assertIgnoreItem(
!(value instanceof RegExp) !(value instanceof RegExp)
) { ) {
throw new Error( throw new Error(
`.${key}[${index}] must be an array of string/Funtion/RegExp values, or null, or undefined`, `.${key}[${index}] must be an array of string/Funtion/RegExp values, or or undefined`,
); );
} }
return value; return value;
} }
export function assertPluginList(key: string, value: mixed): ?PluginList { export function assertPluginList(key: string, value: mixed): PluginList | void {
const arr = assertArray(key, value); const arr = assertArray(key, value);
if (arr) { if (arr) {
// Loop instead of using `.map` in order to preserve object identity // Loop instead of using `.map` in order to preserve object identity
@ -135,9 +142,7 @@ function assertPluginItem(
if (value.length === 2) { if (value.length === 2) {
const opts = value[1]; const opts = value[1];
if (opts != null && (typeof opts !== "object" || Array.isArray(opts))) { if (opts != null && (typeof opts !== "object" || Array.isArray(opts))) {
throw new Error( throw new Error(`.${key}[${index}][1] must be an object, or undefined`);
`.${key}[${index}][1] must be an object, null, or undefined`,
);
} }
} }
} else { } else {
@ -168,7 +173,7 @@ function assertPluginTarget(
function assertArray(key: string, value: mixed): ?$ReadOnlyArray<mixed> { function assertArray(key: string, value: mixed): ?$ReadOnlyArray<mixed> {
if (value != null && !Array.isArray(value)) { if (value != null && !Array.isArray(value)) {
throw new Error(`.${key} must be an array, null, or undefined`); throw new Error(`.${key} must be an array, or undefined`);
} }
return value; return value;
} }

View File

@ -126,54 +126,54 @@ const COMMON_VALIDATORS: ValidatorSet = {
>), >),
}; };
export type ValidatedOptions = { export type ValidatedOptions = {
filename?: ?string, filename?: string,
filenameRelative?: ?string, filenameRelative?: string,
babelrc?: ?boolean, babelrc?: boolean,
code?: ?boolean, code?: boolean,
ast?: ?boolean, ast?: boolean,
inputSourceMap?: ?RootInputSourceMapOption, inputSourceMap?: RootInputSourceMapOption,
extends?: ?string, extends?: string,
env?: ?EnvSet<ValidatedOptions>, env?: EnvSet<ValidatedOptions>,
ignore?: ?IgnoreList, ignore?: IgnoreList,
only?: ?IgnoreList, only?: IgnoreList,
presets?: ?PluginList, presets?: PluginList,
plugins?: ?PluginList, plugins?: PluginList,
passPerPreset?: ?boolean, passPerPreset?: boolean,
// Options for @babel/generator // Options for @babel/generator
retainLines?: ?boolean, retainLines?: boolean,
comments?: ?boolean, comments?: boolean,
shouldPrintComment?: ?Function, shouldPrintComment?: Function,
compact?: ?CompactOption, compact?: CompactOption,
minified?: ?boolean, minified?: boolean,
auxiliaryCommentBefore?: ?string, auxiliaryCommentBefore?: string,
auxiliaryCommentAfter?: ?string, auxiliaryCommentAfter?: string,
// Parser // Parser
sourceType?: ?SourceTypeOption, sourceType?: SourceTypeOption,
wrapPluginVisitorMethod?: ?Function, wrapPluginVisitorMethod?: Function,
highlightCode?: ?boolean, highlightCode?: boolean,
// Sourcemap generation options. // Sourcemap generation options.
sourceMaps?: ?SourceMapsOption, sourceMaps?: SourceMapsOption,
sourceMap?: ?SourceMapsOption, sourceMap?: SourceMapsOption,
sourceMapTarget?: ?string, sourceMapTarget?: string,
sourceFileName?: ?string, sourceFileName?: string,
sourceRoot?: ?string, sourceRoot?: string,
// AMD/UMD/SystemJS module naming options. // AMD/UMD/SystemJS module naming options.
getModuleId?: ?Function, getModuleId?: Function,
moduleRoot?: ?string, moduleRoot?: string,
moduleIds?: ?boolean, moduleIds?: boolean,
moduleId?: ?string, moduleId?: string,
// Deprecate top level parserOpts // Deprecate top level parserOpts
parserOpts?: ?{}, parserOpts?: {},
// Deprecate top level generatorOpts // Deprecate top level generatorOpts
generatorOpts?: ?{}, generatorOpts?: {},
}; };
export type EnvSet<T> = { export type EnvSet<T> = {
@ -241,7 +241,7 @@ function assertNoDuplicateSourcemap(opts: {}): void {
} }
} }
function assertEnvSet(key: string, value: mixed): ?EnvSet<ValidatedOptions> { function assertEnvSet(key: string, value: mixed): EnvSet<ValidatedOptions> {
const obj = assertObject(key, value); const obj = assertObject(key, value);
if (obj) { if (obj) {
// Validate but don't copy the .env object in order to preserve // Validate but don't copy the .env object in order to preserve