From 1d40cac52f614ff584cc7ef08cab0513adb4a3c7 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 4 Nov 2014 18:34:21 +1100 Subject: [PATCH] let ExpressionStatements handle child comments --- lib/6to5/generator.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/6to5/generator.js b/lib/6to5/generator.js index 4fd56c4ef5..83d669bd4c 100644 --- a/lib/6to5/generator.js +++ b/lib/6to5/generator.js @@ -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])) {