Enforce a limited set of cache key values for plugins/presets for future caching.

This commit is contained in:
Logan Smyth 2018-08-19 15:47:50 -07:00
parent 25d2f59018
commit 2c3c12fdf7
2 changed files with 28 additions and 5 deletions

View File

@ -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;
}

View File

@ -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 => {