From cb0026edfe1e76705c103198fabe5021636b1b53 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 3 Apr 2015 01:53:25 +1100 Subject: [PATCH] fix plugin api --- CHANGELOG.md | 1 + src/babel/transformation/file/index.js | 22 ++++++++-------------- src/babel/transformation/transformer.js | 7 ++++++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b312b3940..7df7b3cde1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog. * Add `jsxPragma` option. * Automatically generate CLI options based on internal API options. * Add support for `.babelrc` on absolute paths. + * Plugin API! * **Internal** * Export `options` in browser API. * Rewritten parser. diff --git a/src/babel/transformation/file/index.js b/src/babel/transformation/file/index.js index f8bc9b411d..c5c5452fec 100644 --- a/src/babel/transformation/file/index.js +++ b/src/babel/transformation/file/index.js @@ -6,7 +6,6 @@ import isFunction from "lodash/lang/isFunction"; import isAbsolute from "path-is-absolute"; import resolveRc from "../../tools/resolve-rc"; import sourceMap from "source-map"; -import Transformer from "../transformer"; import transform from "./../index"; import generate from "../../generation"; import defaults from "lodash/object/defaults"; @@ -177,7 +176,7 @@ export default class File { buildTransformers() { var file = this; - var transformers = {}; + var transformers = this.transformers = {}; var secondaryStack = []; var stack = []; @@ -202,14 +201,13 @@ export default class File { // init plugins! var beforePlugins = []; var afterPlugins = []; - for (var i = 0; i < file.opts.plugins; i++) { - this.addPlugin(file.opts.plugins[i]); + for (var i = 0; i < file.opts.plugins.length; i++) { + this.addPlugin(file.opts.plugins[i], beforePlugins, afterPlugins); } stack = beforePlugins.concat(stack, afterPlugins); // register this.transformerStack = stack.concat(secondaryStack); - this.transformers = transformers; } getModuleFormatter(type: string) { @@ -258,21 +256,17 @@ export default class File { throw new TypeError(`Plugin ${JSON.stringify(name)} has an illegal position of ${JSON.stringify(position)}`); } - // validate Transformer instance - if (!(plugin instanceof Transformer)) { - if (plugin && plugin.constructor.name === "Transformer") { - throw new TypeError(`Plugin ${JSON.stringify(name)} exported a Transformer instance but it resolved to a different version of Babel`); - } else { - throw new TypeError(`Plugin ${JSON.stringify(name)} didn't export default a Transformer instance`); - } - } - // validate transformer key var key = plugin.key; if (this.transformers[key]) { throw new ReferenceError(`The key for plugin ${JSON.stringify(name)} of ${key} collides with an existing plugin`); } + // validate Transformer instance + if (!plugin.buildPass || plugin.constructor.name !== "Transformer") { + throw new TypeError(`Plugin ${JSON.stringify(name)} didn't export default a Transformer instance`); + } + // build! var pass = this.transformers[key] = plugin.buildPass(this); if (pass.canTransform()) { diff --git a/src/babel/transformation/transformer.js b/src/babel/transformation/transformer.js index 88ecae6029..a40b804bb7 100644 --- a/src/babel/transformation/transformer.js +++ b/src/babel/transformation/transformer.js @@ -9,7 +9,7 @@ import each from "lodash/collection/each"; /** * This is the class responsible for normalising a transformers handlers - * as well as constructing a `TransformerPass` that is repsonsible for + * as well as constructing a `TransformerPass` that is responsible for * actually running the transformer over the provided `File`. */ @@ -69,6 +69,11 @@ export default class Transformer { } buildPass(file: File): TransformerPass { + // validate Transformer instance + if (!(file instanceof File)) { + throw new TypeError(`Transformer ${this.key} is resolving to a different Babel version to what is doing the actual transformation...`); + } + return new TransformerPass(file, this); } }