diff --git a/CHANGELOG.md b/CHANGELOG.md index 105d0e734b..77dee85f80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.12.16 + + * Fix missing comments not being retained from `MethodDefinition` in classes. + # 1.12.15 * Update `acorn-6to5`. diff --git a/lib/6to5/transformation/transformers/classes.js b/lib/6to5/transformation/transformers/classes.js index 1e0aa0dc2a..f1837b803d 100644 --- a/lib/6to5/transformation/transformers/classes.js +++ b/lib/6to5/transformation/transformers/classes.js @@ -254,6 +254,7 @@ Class.prototype.pushConstructor = function (method) { this.hasConstructor = true; t.inherits(construct, fn); + t.inheritsComments(construct, method); construct.defaults = fn.defaults; construct.params = fn.params; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 28d3fc17ad..f144a395ef 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -70,6 +70,9 @@ t.isReferenced = function (node, parent) { // we're a property key so we aren't referenced if (t.isProperty(parent) && parent.key === node) return false; + // we're a variable declarator id so we aren't referenced + if (t.isVariableDeclarator(parent) && parent.id === node) return false; + var isMemberExpression = t.isMemberExpression(parent); // we're in a member expression and we're the computed property so we're referenced @@ -161,7 +164,7 @@ t.toBlock = function (node, parent) { t.getIds = function (node, map, ignoreTypes) { ignoreTypes = ignoreTypes || []; - var search = [node]; + var search = [].concat(node); var ids = {}; while (search.length) { @@ -185,14 +188,6 @@ t.getIds = function (node, map, ignoreTypes) { return ids; }; -t.isLet = function (node) { - return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let); -}; - -t.isVar = function (node) { - return t.isVariableDeclaration(node, { kind: "var" }) && !node._let; -}; - t.getIds.nodes = { AssignmentExpression: "left", ImportSpecifier: "id", @@ -214,13 +209,32 @@ t.getIds.arrays = { ObjectPattern: "properties" }; +t.isLet = function (node) { + return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let); +}; + +t.isVar = function (node) { + return t.isVariableDeclaration(node, { kind: "var" }) && !node._let; +}; + +t.removeComments = function (child) { + delete child.leadingComments; + delete child.trailingComments; + return child; +}; + +t.inheritsComments = function (child, parent) { + child.leadingComments = _.compact([].concat(child.leadingComments, parent.leadingComments)); + child.trailingComments = _.compact([].concat(child.trailingComments, parent.trailingComments)); + return child; +}; + t.inherits = function (child, parent) { - child.loc = parent.loc; - child.end = parent.end; - child.range = parent.range; - child.start = parent.start; - child.leadingComments = parent.leadingComments; - child.trailingComments = parent.trailingComments; + child.loc = parent.loc; + child.end = parent.end; + child.range = parent.range; + child.start = parent.start; + t.inheritsComments(child, parent); return child; }; diff --git a/lib/6to5/util.js b/lib/6to5/util.js index d9175afeb1..2745955309 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -106,8 +106,13 @@ exports.buildDefineProperties = function (mutatorMap) { if (key[0] === "_") return; node = _.clone(node); + var inheritNode = node; if (t.isMethodDefinition(node)) node = node.value; - mapNode.properties.push(t.property("init", t.identifier(key), node)); + + var prop = t.property("init", t.identifier(key), node); + t.inheritsComments(prop, inheritNode); + t.removeComments(inheritNode); + mapNode.properties.push(prop); }); objExpr.properties.push(propNode);