add support for escodegen-style format options

This commit is contained in:
Sebastian McKenzie 2014-11-11 14:30:06 +11:00
parent 25a5caa0fc
commit 4722c0ce56
3 changed files with 42 additions and 28 deletions

View File

@ -1,3 +1,4 @@
# 1.11.13
* Update regenerator-6to5
* Add support for most escodegen formatting options

View File

@ -16,29 +16,38 @@ var _ = require("lodash");
function CodeGenerator(ast, opts, code) {
opts = opts || {};
this.style = {
semicolons: true,
comments: true,
compact: false,
indent: {
char: " ",
width: 2
}
};
this.comments = ast.comments || [];
this.tokens = ast.tokens || [];
this.opts = opts;
this.ast = ast;
this.buf = "";
this._indent = 0;
this.format = CodeGenerator.normaliseOptions(opts);
this._indent = this.format.indent.base;
this.whitespace = new Whitespace(this.tokens, this.comments);
this.position = new Position;
this.map = new SourceMap(this.position, opts, code);
}
CodeGenerator.normaliseOptions = function (opts) {
opts = opts.format || {};
opts = _.merge({
parentheses: true,
semicolons: true,
comments: true,
compact: false,
indent: {
adjustMultilineComment: true,
style: " ",
base: 0
}
}, opts);
return opts;
};
CodeGenerator.generators = {
arrayComprehensions: require("./generators/array-comprehensions"),
templateLiterals: require("./generators/template-literals"),
@ -58,7 +67,7 @@ _.each(CodeGenerator.generators, function (generator) {
CodeGenerator.prototype.newline = function (i, removeLast) {
if (!this.buf) return;
if (this.style.compact) return;
if (this.format.compact) return;
if (this.endsWith("{\n")) return;
if (_.isBoolean(i)) {
@ -89,7 +98,7 @@ CodeGenerator.prototype.removeLast = function (cha) {
};
CodeGenerator.prototype.semicolon = function () {
if (this.style.semicolons) this.push(";");
if (this.format.semicolons) this.push(";");
};
CodeGenerator.prototype.ensureSemicolon = function () {
@ -145,15 +154,15 @@ CodeGenerator.prototype.isLast = function (cha, trimRight) {
};
CodeGenerator.prototype.getIndent = function () {
if (this.style.compact) {
if (this.format.compact) {
return "";
} else {
return util.repeat(this.indentSize(), this.style.indent.char);
return util.repeat(this._indent, this.format.indent.style);
}
};
CodeGenerator.prototype.indentSize = function () {
return this._indent * this.style.indent.width;
return this.getIndent().length;
};
CodeGenerator.prototype.indent = function () {
@ -353,8 +362,8 @@ CodeGenerator.prototype._getComments = function (key, node) {
};
CodeGenerator.prototype._printComments = function (comments) {
if (this.style.compact) return;
if (!this.style.comments) return;
if (this.format.compact) return;
if (!this.format.comments) return;
if (!comments || !comments.length) return;
var self = this;
@ -373,14 +382,16 @@ CodeGenerator.prototype._printComments = function (comments) {
//
var offset = comment.loc.start.column;
if (offset) {
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
}
if (comment.type === "Block" && self.format.indent.adjustMultilineComment) {
var offset = comment.loc.start.column;
if (offset) {
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
}
var indent = Math.max(self.indentSize(), column);
val = val.replace(/\n/g, "\n" + util.repeat(indent));
var indent = Math.max(self.indentSize(), column);
val = val.replace(/\n/g, "\n" + util.repeat(indent));
}
if (column === 0) {
val = self.getIndent() + val;

View File

@ -44,9 +44,11 @@ exports.ConditionalExpression = function (node, print) {
exports.NewExpression = function (node, print) {
this.push("new ");
print(node.callee);
this.push("(");
print.join(node.arguments, { separator: ", " });
this.push(")");
if (node.arguments.length || this.format.parentheses) {
this.push("(");
print.join(node.arguments, { separator: ", " });
this.push(")");
}
};
exports.SequenceExpression = function (node, print) {