generator: add semicolon helper method, add optional printJoin iterator

This commit is contained in:
Sebastian McKenzie 2014-10-31 21:24:54 +11:00
parent e8628ea1a7
commit 0fbf0e2a77

View File

@ -57,6 +57,10 @@ CodeGenerator.prototype.newline = function () {
this.buf += "\n";
};
CodeGenerator.prototype.semicolon = function () {
this.buf += ";";
};
CodeGenerator.prototype.push = function (str) {
if (this._indent) {
// we have an indent level and we aren't pushing a newline
@ -164,33 +168,28 @@ CodeGenerator.prototype.printLeadingComments = function (node) {
});
};
CodeGenerator.prototype.printJoin = function (print, nodes, sep, indent) {
sep = sep || "\n";
CodeGenerator.prototype.printJoin = function (print, nodes, sep, opts) {
opts = opts || {};
sep = sep || "\n";
var self = this;
var len = nodes.length;
if (indent) self.indent();
if (opts.indent) self.indent();
_.each(nodes, function (node, i) {
print(node);
if (opts.iterator) {
opts.iterator(node, i);
} else {
print(node);
}
if (i < len - 1) {
self.push(sep);
}
});
if (indent) self.dedent();
};
CodeGenerator.prototype.removeEmptyExpressions = function (nodes) {
return nodes.filter(function (node) {
if (node.type === "EmptyStatement") {
return false;
} else {
return true;
}
});
if (opts.indent) self.dedent();
};
CodeGenerator.generators = {