let ExpressionStatements handle child comments

This commit is contained in:
Sebastian McKenzie
2014-11-04 18:34:21 +11:00
parent fb0325a4d8
commit 1d40cac52f

View File

@@ -277,7 +277,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
opts = opts || {};
if (this[node.type]) {
this.printLeadingComments(node);
this.printLeadingComments(node, parent);
if (opts.before) opts.before();
this.mark(node, "start");
@@ -291,7 +291,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
this.mark(node, "end");
if (opts.after) opts.after();
this.printTrailingComments(node);
this.printTrailingComments(node, parent);
} else {
throw new ReferenceError("unknown node " + node.type + " " + JSON.stringify(node));
}
@@ -300,19 +300,37 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
CodeGenerator.prototype.generateComment = function (comment) {
var val = comment.value;
if (comment.type === "Line") {
val = "//" + val + "\n";
val = "//" + val;
} else {
val = "/*" + val + "*/\n";
this.newline(true);
val = val.replace(/\n(\s+)/g, "\n ");
val = "/*" + val + "*/";
}
return val;
};
CodeGenerator.prototype.printTrailingComments = function (node) {
this._printComments(node.trailingComments);
CodeGenerator.prototype.printTrailingComments = function (node, parent) {
this._printComments(this.getComments("trailingComments", node, parent));
};
CodeGenerator.prototype.printLeadingComments = function (node) {
this._printComments(node.leadingComments);
CodeGenerator.prototype.printLeadingComments = function (node, parent) {
this._printComments(this.getComments("leadingComments", node, parent));
};
CodeGenerator.prototype.getComments = function (key, node, parent) {
if (t.isExpressionStatement(parent)) {
return [];
}
if (t.isExpressionStatement(node)) {
return [].concat(this._getComments(key, node), this._getComments(key, node.argument));
}
return this._getComments(key, node);
};
CodeGenerator.prototype._getComments = function (key, node) {
return (node && node[key]) || [];
};
CodeGenerator.prototype._printComments = function (comments) {
@@ -327,6 +345,7 @@ CodeGenerator.prototype._printComments = function (comments) {
}
self.push(self.generateComment(comment));
self.newline();
// whitespace after
if (self.hasWhitespaceBetween(comment, comments[i + 1])) {