diff --git a/packages/babel-core/src/config/caching.js b/packages/babel-core/src/config/caching.js index 7c3a28bda2..c8db5a166c 100644 --- a/packages/babel-core/src/config/caching.js +++ b/packages/babel-core/src/config/caching.js @@ -206,12 +206,29 @@ function makeSimpleConfigurator( return; } - return cache.using(val); + return cache.using(() => assertSimpleType(val())); } cacheFn.forever = () => cache.forever(); cacheFn.never = () => cache.never(); - cacheFn.using = cb => cache.using(() => cb()); - cacheFn.invalidate = cb => cache.invalidate(() => cb()); + cacheFn.using = cb => cache.using(() => assertSimpleType(cb())); + cacheFn.invalidate = cb => cache.invalidate(() => assertSimpleType(cb())); return (cacheFn: any); } + +// Types are limited here so that in the future these values can be used +// as part of Babel's caching logic. +type SimpleType = string | boolean | number | null | void; +export function assertSimpleType(value: mixed): SimpleType { + if ( + value != null && + typeof value !== "string" && + typeof value !== "boolean" && + typeof value !== "number" + ) { + throw new Error( + "Cache keys must be either string, boolean, number, null, or undefined.", + ); + } + return value; +} diff --git a/packages/babel-core/src/config/helpers/config-api.js b/packages/babel-core/src/config/helpers/config-api.js index da14c22eeb..a50320680f 100644 --- a/packages/babel-core/src/config/helpers/config-api.js +++ b/packages/babel-core/src/config/helpers/config-api.js @@ -2,7 +2,11 @@ import semver from "semver"; import { version as coreVersion } from "../../"; -import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching"; +import { + assertSimpleType, + type CacheConfigurator, + type SimpleCacheConfigurator, +} from "../caching"; type EnvFunction = { (): string, @@ -25,7 +29,9 @@ export default function makeAPI( const env: any = value => cache.using(data => { if (typeof value === "undefined") return data.envName; - if (typeof value === "function") return value(data.envName); + if (typeof value === "function") { + return assertSimpleType(value(data.envName)); + } if (!Array.isArray(value)) value = [value]; return value.some(entry => {