From 753e900eba5db0316fa8e2175a7759275775e2c6 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Thu, 6 Apr 2017 18:38:38 -0700 Subject: [PATCH] Track options on the plugin instance to avoid array pair usage. --- packages/babel-core/src/config/index.js | 2 +- .../babel-core/src/config/option-manager.js | 24 +++++++++++-------- packages/babel-core/src/config/plugin.js | 5 +++- .../src/transformation/file/index.js | 8 +++---- packages/babel-core/test/api.js | 5 ++-- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/babel-core/src/config/index.js b/packages/babel-core/src/config/index.js index f00fd0e924bd..f3978060e0ef 100644 --- a/packages/babel-core/src/config/index.js +++ b/packages/babel-core/src/config/index.js @@ -5,7 +5,7 @@ import manageOptions from "./option-manager"; export type ResolvedConfig = { options: Object, - passes: Array>, + passes: Array>, }; /** diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index d1d37579b591..2e504fe16173 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -79,7 +79,7 @@ const ALLOWED_PLUGIN_KEYS = new Set([ export default function manageOptions(opts: {}): { options: Object, - passes: Array>, + passes: Array>, } | null { return new OptionManager().init(opts); } @@ -91,7 +91,7 @@ class OptionManager { } options: Object; - passes: Array>; + passes: Array>; /** * This is called when we want to merge the input `opts` into the @@ -102,7 +102,7 @@ class OptionManager { * - `dirname` is used to resolve plugins relative to it. */ - mergeOptions(config: MergeOptions, pass?: Array<[Plugin, ?{}]>) { + mergeOptions(config: MergeOptions, pass?: Array) { const result = loadConfig(config); const plugins = result.plugins.map(descriptor => @@ -315,12 +315,16 @@ const loadDescriptor = makeWeakCache((descriptor, cache) => { */ function loadPluginDescriptor(descriptor: BasicDescriptor) { if (descriptor.value instanceof Plugin) { - return [descriptor.value, descriptor.options]; - } + if (descriptor.options) { + throw new Error( + "Passed options to an existing Plugin instance will not work.", + ); + } - const result = instantiatePlugin(loadDescriptor(descriptor)); + return descriptor.value; + } - return [result, descriptor.options]; + return instantiatePlugin(loadDescriptor(descriptor)); } const instantiatePlugin = makeWeakCache( @@ -360,8 +364,8 @@ const instantiatePlugin = makeWeakCache( }; // If the inherited plugin changes, reinstantiate this plugin. - inherits = cache.invalidate( - () => loadPluginDescriptor(inheritsDescriptor)[0], + inherits = cache.invalidate(() => + loadPluginDescriptor(inheritsDescriptor), ); plugin.pre = chain(inherits.pre, plugin.pre); @@ -376,7 +380,7 @@ const instantiatePlugin = makeWeakCache( ]); } - return new Plugin(plugin, descriptor.alias); + return new Plugin(plugin, descriptor.options, descriptor.alias); }, ); diff --git a/packages/babel-core/src/config/plugin.js b/packages/babel-core/src/config/plugin.js index 84b3701061bc..48c821e207da 100644 --- a/packages/babel-core/src/config/plugin.js +++ b/packages/babel-core/src/config/plugin.js @@ -1,7 +1,7 @@ // @flow export default class Plugin { - constructor(plugin: {}, key?: string) { + constructor(plugin: {}, options: ?{}, key?: string) { if (plugin.name != null && typeof plugin.name !== "string") { throw new Error("Plugin .name must be a string, null, or undefined"); } @@ -29,6 +29,7 @@ export default class Plugin { this.post = plugin.post; this.pre = plugin.pre; this.visitor = plugin.visitor; + this.options = options || undefined; } key: ?string; @@ -36,4 +37,6 @@ export default class Plugin { post: ?Function; pre: ?Function; visitor: ?{}; + + options: {} | void; } diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index b827c4b0bcac..574278dbb250 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -58,7 +58,7 @@ export default class File { }; for (const pluginPairs of passes) { - for (const [plugin] of pluginPairs) { + for (const plugin of pluginPairs) { if (plugin.manipulateOptions) { plugin.manipulateOptions(this.opts, this.parserOpts, this); } @@ -77,7 +77,7 @@ export default class File { this.hub = new Hub(this); } - pluginPasses: Array>; + pluginPasses: Array>; parserOpts: BabelParserOptions; opts: Object; declarations: Object; @@ -321,8 +321,8 @@ export default class File { const passes = []; const visitors = []; - for (const [plugin, pluginOpts] of pluginPairs.concat(INTERNAL_PLUGINS)) { - const pass = new PluginPass(this, plugin.key, pluginOpts); + for (const plugin of pluginPairs.concat(INTERNAL_PLUGINS)) { + const pass = new PluginPass(this, plugin.key, plugin.options); passPairs.push([plugin, pass]); passes.push(pass); diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index b9c0f76d4dfe..2bcbd9007d3f 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -154,9 +154,8 @@ describe("api", function() { plugins: [__dirname + "/../../babel-plugin-syntax-jsx"], }).then(function(result) { assert.ok( - result.options.plugins[0][0].manipulateOptions - .toString() - .indexOf("jsx") >= 0, + result.options.plugins[0].manipulateOptions.toString().indexOf("jsx") >= + 0, ); }); });