diff --git a/bin/6to5/index.js b/bin/6to5/index.js index 77263e50ee..84cfa2db0b 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -17,6 +17,7 @@ commander.option("-p, --playground", "Enable playground support"); commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common"); commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list); commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list); +commander.option("-i, --optional [list]", "List of optional transformers to enable", util.list); commander.option("-o, --out-file [out]", "Compile all input files into a single file"); commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory"); commander.option("-c, --remove-comments", "Remove comments from the compiled code", false); @@ -28,8 +29,16 @@ commander.on("--help", function(){ console.log(" " + title + ":"); console.log(); + var hasOptional = true; + _.each(_.keys(obj).sort(), function (key) { if (key[0] === "_") return; + + if (obj[key].optional) { + hasOptional = true; + key = "[" + key + "]"; + } + console.log(" - " + key); }); @@ -96,6 +105,7 @@ exports.opts = { blacklist: commander.blacklist, whitelist: commander.whitelist, sourceMap: commander.sourceMaps || commander.sourceMapsInline, + optional: commander.optional, comments: !commander.removeComments, runtime: commander.runtime, modules: commander.modules diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 3f055a7456..53550d43b3 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -10,9 +10,10 @@ var t = require("./types"); var _ = require("lodash"); function File(opts) { - this.opts = File.normaliseOptions(opts); - this.uids = {}; - this.ast = {}; + this.opts = File.normaliseOptions(opts); + this.transformers = this.getTransformers(); + this.uids = {}; + this.ast = {}; } File.declarations = [ @@ -39,6 +40,7 @@ File.normaliseOptions = function (opts) { blacklist: [], whitelist: [], sourceMap: false, + optional: [], comments: true, filename: "unknown", modules: "common", @@ -80,10 +82,24 @@ File.normaliseOptions = function (opts) { transform._ensureTransformerNames("blacklist", opts.blacklist); transform._ensureTransformerNames("whitelist", opts.whitelist); + transform._ensureTransformerNames("optional", opts.optional); return opts; }; +File.prototype.getTransformers = function () { + var file = this; + var transformers = []; + + _.each(transform.transformers, function (transformer) { + if (transformer.canRun(file)) { + transformers.push(transformer); + } + }); + + return transformers; +}; + File.prototype.toArray = function (node, i) { if (t.isArrayExpression(node)) { return node; @@ -182,15 +198,25 @@ File.prototype.parse = function (code) { }; File.prototype.transform = function (ast) { + var self = this; + this.ast = ast; this.scope = new Scope(ast.program); this.moduleFormatter = this.getModuleFormatter(this.opts.modules); - var self = this; + var astRun = function (key) { + _.each(self.transformers, function (transformer) { + transformer.astRun(self, key); + }); + }; - _.each(transform.transformers, function (transformer) { + astRun("enter"); + + _.each(this.transformers, function (transformer) { transformer.transform(self); }); + + astRun("exit"); }; File.prototype.generate = function () {