Enforce a limited set of cache key values for plugins/presets for future caching.
This commit is contained in:
parent
25d2f59018
commit
2c3c12fdf7
@ -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;
|
||||
}
|
||||
|
||||
@ -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 => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user