diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b8dcd7b44..ff51e5e681 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Gaps between patch versions are faulty/broken releases. +## 2.3.2 + + * Add parens on expressions with trailing comments. + ## 2.3.1 * Add `undefinedToVoid` optional transformer. diff --git a/lib/6to5/generation/generator.js b/lib/6to5/generation/generator.js index 1b830b6a30..d2da9ac9c4 100644 --- a/lib/6to5/generation/generator.js +++ b/lib/6to5/generation/generator.js @@ -148,6 +148,11 @@ CodeGenerator.prototype.print = function (node, parent, opts) { }; if (this[node.type]) { + // only compute if this node needs parens if our parent has been changed + // since acorn would've wrapped us in a ParanthesizedExpression + var needsParens = n.needsParens(node, parent); + if (needsParens) this.push("("); + this.printLeadingComments(node, parent); newline(true); @@ -155,11 +160,6 @@ CodeGenerator.prototype.print = function (node, parent, opts) { if (opts.before) opts.before(); this.map.mark(node, "start"); - // only compute if this node needs parens if our parent has been changed - // since acorn would've wrapped us in a ParanthesizedExpression - var needsParens = n.needsParens(node, parent); - if (needsParens) this.push("("); - this[node.type](node, this.buildPrint(node), parent); if (needsParens) this.push(")"); diff --git a/lib/6to5/generation/node/index.js b/lib/6to5/generation/node/index.js index b1ab6b7599..8b364bfcdb 100644 --- a/lib/6to5/generation/node/index.js +++ b/lib/6to5/generation/node/index.js @@ -60,6 +60,10 @@ Node.prototype.needsParens = function () { if (!parent) return false; + if (t.isExpression(node) && node.leadingComments && node.leadingComments.length) { + return true; + } + if (t.isNewExpression(parent) && parent.callee === node) { return t.isCallExpression(node) || _.some(node, function (val) { return t.isCallExpression(val); diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 2978115f63..16ae144200 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -297,19 +297,26 @@ t.isVar = function (node) { return t.isVariableDeclaration(node, { kind: "var" }) && !node._let; }; +// + +t.COMMENT_KEYS = ["leadingComments", "trailingComments"]; + t.removeComments = function (child) { - delete child.leadingComments; - delete child.trailingComments; + _.each(t.COMMENT_KEYS, function (key) { + delete child[key]; + }); return child; }; t.inheritsComments = function (child, parent) { - _.each(["leadingComments", "trailingComments"], function (key) { + _.each(t.COMMENT_KEYS, function (key) { child[key] = _.uniq(_.compact([].concat(child[key], parent[key]))); }); return child; }; +// + t.inherits = function (child, parent) { child.loc = parent.loc; child.end = parent.end;