From 6e780d3d80769d67aa3af7813052aab2ca618960 Mon Sep 17 00:00:00 2001 From: Andy Mockler Date: Fri, 31 Aug 2018 10:00:09 -0400 Subject: [PATCH] Prevent plugin overwriting --- src/state.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/state.js b/src/state.js index e3054ee07..fb2bbc325 100644 --- a/src/state.js +++ b/src/state.js @@ -3,7 +3,7 @@ import {types as tt} from "./tokentype" import {lineBreak} from "./whitespace" import {getOptions} from "./options" -// Registered plugins +// Global registered plugins for loose parser export const plugins = {} function keywordRegexp(words) { @@ -12,6 +12,7 @@ function keywordRegexp(words) { export class Parser { constructor(options, input, startPos) { + this.plugins = {} this.options = options = getOptions(options) this.sourceFile = options.sourceFile this.keywords = keywordRegexp(keywords[options.ecmaVersion >= 6 ? 6 : 5]) @@ -104,9 +105,16 @@ export class Parser { loadPlugins(pluginConfigs) { for (let name in pluginConfigs) { - let plugin = plugins[name] - if (!plugin) throw new Error("Plugin '" + name + "' not found") - plugin(this, pluginConfigs[name]) + let plugin = this.plugins[name] + let loosePlugin = plugins[name] + + if (plugin) { + plugin(this, pluginConfigs[name]) + } else if (loosePlugin) { + loosePlugin(this, pluginConfigs[name]) + } else { + throw new Error("Plugin '" + name + "' not found") + } } }