From 89983cead94901e9fdfcde2446499aba2ed17a9e Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 2 Aug 2015 21:37:12 +0100 Subject: [PATCH] make comment retainment for multiple nodes more predictable - fixes #2146 --- .../babel/src/traversal/path/replacement.js | 3 ++- packages/babel/src/types/index.js | 26 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/babel/src/traversal/path/replacement.js b/packages/babel/src/traversal/path/replacement.js index 16bda31e7a..abaf3d5778 100644 --- a/packages/babel/src/traversal/path/replacement.js +++ b/packages/babel/src/traversal/path/replacement.js @@ -56,7 +56,8 @@ export function replaceWithMultiple(nodes: Array) { this.resync(); nodes = this._verifyNodeList(nodes); - t.inheritsComments(nodes[0], this.node); + t.inheritLeadingComments(nodes[0], this.node); + t.inheritTrailingComments(nodes[nodes.length - 1], this.node); this.node = this.container[this.key] = null; this.insertAfter(nodes); if (!this.node) this.dangerouslyRemove(); diff --git a/packages/babel/src/types/index.js b/packages/babel/src/types/index.js index 8975faea2f..2f9bd85aeb 100644 --- a/packages/babel/src/types/index.js +++ b/packages/babel/src/types/index.js @@ -318,14 +318,30 @@ export function removeComments(node: Object): Object { */ export function inheritsComments(child: Object, parent: Object): Object { - if (child && parent) { - for (var key of (COMMENT_KEYS: Array)) { - child[key] = uniq(compact([].concat(child[key], parent[key]))); - } - } + inheritTrailingComments(child, parent); + inheritLeadingComments(child, parent); + inheritInnerComments(child, parent); return child; } +export function inheritTrailingComments(child, parent) { + _inheritComments("trailingComments", child, parent); +} + +export function inheritLeadingComments(child, parent) { + _inheritComments("leadingComments", child, parent); +} + +export function inheritInnerComments(child, parent) { + _inheritComments("innerComments", child, parent); +} + +function _inheritComments(key, child, parent) { + if (child && parent) { + child[key] = uniq(compact([].concat(child[key], parent[key]))); + } +} + /** * Inherit all contextual properties from `parent` node to `child` node. */