perf: Make plugins a map instead of object

Fix plugin options
This commit is contained in:
Daniel Tschinder 2019-01-15 12:54:57 -08:00
parent 2dc1c91955
commit 59c4bbb4ab
2 changed files with 7 additions and 8 deletions

View File

@ -26,10 +26,11 @@ export default class BaseParser {
}
hasPlugin(name: string): boolean {
return Object.hasOwnProperty.call(this.plugins, name);
return this.plugins.has(name);
}
getPluginOption(plugin: string, name: string) {
if (this.hasPlugin(plugin)) return this.plugins[plugin][name];
// $FlowIssue
if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
}
}

View File

@ -6,9 +6,7 @@ import type { PluginList } from "../plugin-utils";
import { getOptions } from "../options";
import StatementParser from "./statement";
export type PluginsMap = {
[key: string]: { [option: string]: any },
};
export type PluginsMap = Map<string, { [string]: any }>;
export default class Parser extends StatementParser {
// Forward-declaration so typescript plugin can override jsx plugin
@ -35,10 +33,10 @@ export default class Parser extends StatementParser {
}
function pluginsMap(plugins: PluginList): PluginsMap {
const pluginMap: PluginsMap = (Object.create(null): Object);
const pluginMap: PluginsMap = new Map();
for (const plugin of plugins) {
const [name, options = {}] = Array.isArray(plugin) ? plugin : [plugin, {}];
if (!pluginMap[name]) pluginMap[name] = options || {};
const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
if (!pluginMap.has(name)) pluginMap.set(name, options || {});
}
return pluginMap;
}