add support for optional transformers

This commit is contained in:
Sebastian McKenzie 2015-01-01 22:35:02 +11:00
parent 64b7d6fa93
commit 20a0280a52
2 changed files with 41 additions and 5 deletions

View File

@ -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("-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("-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("-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("-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("-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); 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(" " + title + ":");
console.log(); console.log();
var hasOptional = true;
_.each(_.keys(obj).sort(), function (key) { _.each(_.keys(obj).sort(), function (key) {
if (key[0] === "_") return; if (key[0] === "_") return;
if (obj[key].optional) {
hasOptional = true;
key = "[" + key + "]";
}
console.log(" - " + key); console.log(" - " + key);
}); });
@ -96,6 +105,7 @@ exports.opts = {
blacklist: commander.blacklist, blacklist: commander.blacklist,
whitelist: commander.whitelist, whitelist: commander.whitelist,
sourceMap: commander.sourceMaps || commander.sourceMapsInline, sourceMap: commander.sourceMaps || commander.sourceMapsInline,
optional: commander.optional,
comments: !commander.removeComments, comments: !commander.removeComments,
runtime: commander.runtime, runtime: commander.runtime,
modules: commander.modules modules: commander.modules

View File

@ -10,9 +10,10 @@ var t = require("./types");
var _ = require("lodash"); var _ = require("lodash");
function File(opts) { function File(opts) {
this.opts = File.normaliseOptions(opts); this.opts = File.normaliseOptions(opts);
this.uids = {}; this.transformers = this.getTransformers();
this.ast = {}; this.uids = {};
this.ast = {};
} }
File.declarations = [ File.declarations = [
@ -39,6 +40,7 @@ File.normaliseOptions = function (opts) {
blacklist: [], blacklist: [],
whitelist: [], whitelist: [],
sourceMap: false, sourceMap: false,
optional: [],
comments: true, comments: true,
filename: "unknown", filename: "unknown",
modules: "common", modules: "common",
@ -80,10 +82,24 @@ File.normaliseOptions = function (opts) {
transform._ensureTransformerNames("blacklist", opts.blacklist); transform._ensureTransformerNames("blacklist", opts.blacklist);
transform._ensureTransformerNames("whitelist", opts.whitelist); transform._ensureTransformerNames("whitelist", opts.whitelist);
transform._ensureTransformerNames("optional", opts.optional);
return opts; 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) { File.prototype.toArray = function (node, i) {
if (t.isArrayExpression(node)) { if (t.isArrayExpression(node)) {
return node; return node;
@ -182,15 +198,25 @@ File.prototype.parse = function (code) {
}; };
File.prototype.transform = function (ast) { File.prototype.transform = function (ast) {
var self = this;
this.ast = ast; this.ast = ast;
this.scope = new Scope(ast.program); this.scope = new Scope(ast.program);
this.moduleFormatter = this.getModuleFormatter(this.opts.modules); 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); transformer.transform(self);
}); });
astRun("exit");
}; };
File.prototype.generate = function () { File.prototype.generate = function () {