diff --git a/packages/babel-core/src/config/build-config-chain.js b/packages/babel-core/src/config/build-config-chain.js index 9f8f17e574..53d4e88fa3 100644 --- a/packages/babel-core/src/config/build-config-chain.js +++ b/packages/babel-core/src/config/build-config-chain.js @@ -9,7 +9,7 @@ import { type PluginItem, type PluginList, type IgnoreList, -} from "./options"; +} from "./validation/options"; const debug = buildDebug("babel:config:config-chain"); diff --git a/packages/babel-core/src/config/index.js b/packages/babel-core/src/config/index.js index e988ed02da..0bdd1184bb 100644 --- a/packages/babel-core/src/config/index.js +++ b/packages/babel-core/src/config/index.js @@ -3,7 +3,7 @@ import type Plugin from "./plugin"; import manageOptions from "./option-manager"; -export type { InputOptions } from "./options"; +export type { InputOptions } from "./validation/options"; export type ResolvedConfig = { options: Object, diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index c25c037905..37e54aa879 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -2,7 +2,7 @@ import path from "path"; import * as context from "../index"; -import Plugin, { validatePluginObject } from "./plugin"; +import Plugin from "./plugin"; import merge from "lodash/merge"; import { buildRootChain, @@ -15,7 +15,8 @@ import traverse from "@babel/traverse"; import clone from "lodash/clone"; import { makeWeakCache, type CacheConfigurator } from "./caching"; import { getEnv } from "./helpers/environment"; -import { validate } from "./options"; +import { validate } from "./validation/options"; +import { validatePluginObject } from "./validation/plugins"; export default function manageOptions(inputOpts: {}): { options: Object, diff --git a/packages/babel-core/src/config/plugin.js b/packages/babel-core/src/config/plugin.js index 97b1364777..6da0fe6360 100644 --- a/packages/babel-core/src/config/plugin.js +++ b/packages/babel-core/src/config/plugin.js @@ -1,100 +1,6 @@ // @flow -import { - assertString, - assertFunction, - assertObject, - type ValidatorSet, - type Validator, -} from "./option-assertions"; - -// Note: The casts here are just meant to be static assertions to make sure -// that the assertion functions actually assert that the value's type matches -// the declared types. -const VALIDATORS: ValidatorSet = { - name: (assertString: Validator<$PropertyType>), - manipulateOptions: (assertFunction: Validator< - $PropertyType, - >), - pre: (assertFunction: Validator<$PropertyType>), - post: (assertFunction: Validator<$PropertyType>), - inherits: (assertFunction: Validator< - $PropertyType, - >), - visitor: (assertVisitorMap: Validator< - $PropertyType, - >), - - parserOverride: (assertFunction: Validator< - $PropertyType, - >), - generatorOverride: (assertFunction: Validator< - $PropertyType, - >), -}; - -function assertVisitorMap(key: string, value: mixed): VisitorMap { - const obj = assertObject(key, value); - if (obj) { - Object.keys(obj).forEach(prop => assertVisitorHandler(prop, obj[prop])); - - if (obj.enter || obj.exit) { - throw new Error( - `.${key} cannot contain catch-all "enter" or "exit" handlers. Please target individual nodes.`, - ); - } - } - return (obj: any); -} - -function assertVisitorHandler( - key: string, - value: mixed, -): VisitorHandler | void { - if (value && typeof value === "object") { - Object.keys(value).forEach(handler => { - if (handler !== "enter" && handler !== "exit") { - throw new Error( - `.visitor["${key}"] may only have .enter and/or .exit handlers.`, - ); - } - }); - } else if (typeof value !== "function") { - throw new Error(`.visitor["${key}"] must be a function`); - } - - return (value: any); -} - -type VisitorHandler = Function | { enter?: Function, exit?: Function }; -export type VisitorMap = { - [string]: VisitorHandler, -}; - -export type PluginObject = { - name?: string, - manipulateOptions?: Function, - - pre?: Function, - post?: Function, - - inherits?: Function, - visitor?: VisitorMap, - - parserOverride?: Function, - generatorOverride?: Function, -}; - -export function validatePluginObject(obj: {}): PluginObject { - Object.keys(obj).forEach(key => { - const validator = VALIDATORS[key]; - - if (validator) validator(key, obj[key]); - else throw new Error(`.${key} is not a valid Plugin property`); - }); - - return (obj: any); -} +import type { PluginObject } from "./validation/plugins"; export default class Plugin { key: ?string; diff --git a/packages/babel-core/src/config/option-assertions.js b/packages/babel-core/src/config/validation/option-assertions.js similarity index 100% rename from packages/babel-core/src/config/option-assertions.js rename to packages/babel-core/src/config/validation/option-assertions.js diff --git a/packages/babel-core/src/config/options.js b/packages/babel-core/src/config/validation/options.js similarity index 100% rename from packages/babel-core/src/config/options.js rename to packages/babel-core/src/config/validation/options.js diff --git a/packages/babel-core/src/config/validation/plugins.js b/packages/babel-core/src/config/validation/plugins.js new file mode 100644 index 0000000000..7800de965e --- /dev/null +++ b/packages/babel-core/src/config/validation/plugins.js @@ -0,0 +1,95 @@ +import { + assertString, + assertFunction, + assertObject, + type ValidatorSet, + type Validator, +} from "./option-assertions"; + +// Note: The casts here are just meant to be static assertions to make sure +// that the assertion functions actually assert that the value's type matches +// the declared types. +const VALIDATORS: ValidatorSet = { + name: (assertString: Validator<$PropertyType>), + manipulateOptions: (assertFunction: Validator< + $PropertyType, + >), + pre: (assertFunction: Validator<$PropertyType>), + post: (assertFunction: Validator<$PropertyType>), + inherits: (assertFunction: Validator< + $PropertyType, + >), + visitor: (assertVisitorMap: Validator< + $PropertyType, + >), + + parserOverride: (assertFunction: Validator< + $PropertyType, + >), + generatorOverride: (assertFunction: Validator< + $PropertyType, + >), +}; + +function assertVisitorMap(key: string, value: mixed): VisitorMap { + const obj = assertObject(key, value); + if (obj) { + Object.keys(obj).forEach(prop => assertVisitorHandler(prop, obj[prop])); + + if (obj.enter || obj.exit) { + throw new Error( + `.${key} cannot contain catch-all "enter" or "exit" handlers. Please target individual nodes.`, + ); + } + } + return (obj: any); +} + +function assertVisitorHandler( + key: string, + value: mixed, +): VisitorHandler | void { + if (value && typeof value === "object") { + Object.keys(value).forEach(handler => { + if (handler !== "enter" && handler !== "exit") { + throw new Error( + `.visitor["${key}"] may only have .enter and/or .exit handlers.`, + ); + } + }); + } else if (typeof value !== "function") { + throw new Error(`.visitor["${key}"] must be a function`); + } + + return (value: any); +} + +type VisitorHandler = Function | { enter?: Function, exit?: Function }; +export type VisitorMap = { + [string]: VisitorHandler, +}; + +export type PluginObject = { + name?: string, + manipulateOptions?: Function, + + pre?: Function, + post?: Function, + + inherits?: Function, + visitor?: VisitorMap, + + parserOverride?: Function, + generatorOverride?: Function, +}; + +export function validatePluginObject(obj: {}): PluginObject { + Object.keys(obj).forEach(key => { + const validator = VALIDATORS[key]; + + if (validator) validator(key, obj[key]); + else throw new Error(`.${key} is not a valid Plugin property`); + }); + + return (obj: any); +} diff --git a/packages/babel-core/src/config/removed.js b/packages/babel-core/src/config/validation/removed.js similarity index 100% rename from packages/babel-core/src/config/removed.js rename to packages/babel-core/src/config/validation/removed.js