[babylon] Refactor mixin plugins handling & validation (#7999)

This commit is contained in:
Nicolò Ribaudo
2018-05-23 21:28:05 +02:00
committed by Henry Zhu
parent b33823e7f8
commit ffe04d9195
5 changed files with 84 additions and 73 deletions

View File

@@ -0,0 +1,50 @@
// @flow
import type Parser from "./parser";
export type Plugin = string | [string, Object];
export type PluginList = $ReadOnlyArray<Plugin>;
export type MixinPlugin = (superClass: Class<Parser>) => Class<Parser>;
export function hasPlugin(plugins: PluginList, name: string): boolean {
return plugins.some(plugin => {
if (Array.isArray(plugin)) {
return plugin[0] === name;
} else {
return plugin === name;
}
});
}
export function validatePlugins(plugins: PluginList) {
if (
hasPlugin(plugins, "decorators") &&
hasPlugin(plugins, "decorators-legacy")
) {
throw new Error(
"Cannot use the decorators and decorators-legacy plugin together",
);
}
if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
throw new Error("Cannot combine flow and typescript plugins.");
}
}
// These plugins are defined using a mixin which extends the parser class.
import estree from "./plugins/estree";
import flow from "./plugins/flow";
import jsx from "./plugins/jsx";
import typescript from "./plugins/typescript";
// NOTE: estree must load first; flow and typescript must load last.
export const mixinPluginNames = ["estree", "jsx", "flow", "typescript"];
export const mixinPlugins: { [name: string]: MixinPlugin } = {
estree,
jsx,
flow,
typescript,
};