Add more types around descriptor processing.

This commit is contained in:
Logan Smyth 2017-10-05 21:24:52 -04:00
parent 2ee45bd04d
commit 1e12bb6a23

View File

@ -220,6 +220,11 @@ type BasicDescriptor = {
loc: string, loc: string,
}; };
type LoadedDescriptor = {
value: {},
descriptor: BasicDescriptor,
};
/** /**
* Load and validate the given config into a set of options, plugins, and presets. * Load and validate the given config into a set of options, plugins, and presets.
*/ */
@ -282,7 +287,8 @@ const loadConfig = makeWeakCache((config): {
/** /**
* Load a generic plugin/preset from the given descriptor loaded from the config object. * Load a generic plugin/preset from the given descriptor loaded from the config object.
*/ */
const loadDescriptor = makeWeakCache((descriptor, cache) => { const loadDescriptor = makeWeakCache(
(descriptor: BasicDescriptor, cache): LoadedDescriptor => {
if (typeof descriptor.value !== "function") { if (typeof descriptor.value !== "function") {
return { value: descriptor.value, descriptor }; return { value: descriptor.value, descriptor };
} }
@ -308,12 +314,13 @@ const loadDescriptor = makeWeakCache((descriptor, cache) => {
} }
return { value: item, descriptor }; return { value: item, descriptor };
}); },
);
/** /**
* Instantiate a plugin for the given descriptor, returning the plugin/options pair. * Instantiate a plugin for the given descriptor, returning the plugin/options pair.
*/ */
function loadPluginDescriptor(descriptor: BasicDescriptor) { function loadPluginDescriptor(descriptor: BasicDescriptor): Plugin {
if (descriptor.value instanceof Plugin) { if (descriptor.value instanceof Plugin) {
if (descriptor.options) { if (descriptor.options) {
throw new Error( throw new Error(
@ -328,7 +335,7 @@ function loadPluginDescriptor(descriptor: BasicDescriptor) {
} }
const instantiatePlugin = makeWeakCache( const instantiatePlugin = makeWeakCache(
({ value: pluginObj, descriptor }, cache) => { ({ value: pluginObj, descriptor }: LoadedDescriptor, cache): Plugin => {
Object.keys(pluginObj).forEach(key => { Object.keys(pluginObj).forEach(key => {
if (!ALLOWED_PLUGIN_KEYS.has(key)) { if (!ALLOWED_PLUGIN_KEYS.has(key)) {
throw new Error( throw new Error(
@ -391,7 +398,8 @@ const loadPresetDescriptor = (descriptor: BasicDescriptor): MergeOptions => {
return instantiatePreset(loadDescriptor(descriptor)); return instantiatePreset(loadDescriptor(descriptor));
}; };
const instantiatePreset = makeWeakCache(({ value, descriptor }) => { const instantiatePreset = makeWeakCache(
({ value, descriptor }: LoadedDescriptor): MergeOptions => {
return { return {
type: "preset", type: "preset",
options: value, options: value,
@ -399,7 +407,8 @@ const instantiatePreset = makeWeakCache(({ value, descriptor }) => {
loc: descriptor.loc, loc: descriptor.loc,
dirname: descriptor.dirname, dirname: descriptor.dirname,
}; };
}); },
);
/** /**
* Validate and return the options object for the config. * Validate and return the options object for the config.