Skip to content

Commit

Permalink
Track options on the plugin instance to avoid array pair usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Oct 1, 2017
1 parent 8f90a1f commit 753e900
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/index.js
Expand Up @@ -5,7 +5,7 @@ import manageOptions from "./option-manager";

export type ResolvedConfig = {
options: Object,
passes: Array<Array<[Plugin, ?{}]>>,
passes: Array<Array<Plugin>>,
};

/**
Expand Down
24 changes: 14 additions & 10 deletions packages/babel-core/src/config/option-manager.js
Expand Up @@ -79,7 +79,7 @@ const ALLOWED_PLUGIN_KEYS = new Set([

export default function manageOptions(opts: {}): {
options: Object,
passes: Array<Array<[Plugin, ?{}]>>,
passes: Array<Array<Plugin>>,
} | null {
return new OptionManager().init(opts);
}
Expand All @@ -91,7 +91,7 @@ class OptionManager {
}

options: Object;
passes: Array<Array<[Plugin, ?{}]>>;
passes: Array<Array<Plugin>>;

/**
* This is called when we want to merge the input `opts` into the
Expand All @@ -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<Plugin>) {
const result = loadConfig(config);

const plugins = result.plugins.map(descriptor =>
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -376,7 +380,7 @@ const instantiatePlugin = makeWeakCache(
]);
}

return new Plugin(plugin, descriptor.alias);
return new Plugin(plugin, descriptor.options, descriptor.alias);
},
);

Expand Down
5 changes: 4 additions & 1 deletion 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");
}
Expand Down Expand Up @@ -29,11 +29,14 @@ export default class Plugin {
this.post = plugin.post;
this.pre = plugin.pre;
this.visitor = plugin.visitor;
this.options = options || undefined;
}

key: ?string;
manipulateOptions: ?Function;
post: ?Function;
pre: ?Function;
visitor: ?{};

options: {} | void;
}
8 changes: 4 additions & 4 deletions packages/babel-core/src/transformation/file/index.js
Expand Up @@ -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);
}
Expand All @@ -77,7 +77,7 @@ export default class File {
this.hub = new Hub(this);
}

pluginPasses: Array<Array<[Plugin, Object]>>;
pluginPasses: Array<Array<Plugin>>;
parserOpts: BabelParserOptions;
opts: Object;
declarations: Object;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-core/test/api.js
Expand Up @@ -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,
);
});
});
Expand Down

0 comments on commit 753e900

Please sign in to comment.