make comment retainment for multiple nodes more predictable - fixes #2146

This commit is contained in:
Sebastian McKenzie 2015-08-02 21:37:12 +01:00
parent dea349c4d4
commit 89983cead9
2 changed files with 23 additions and 6 deletions

View File

@ -56,7 +56,8 @@ export function replaceWithMultiple(nodes: Array<Object>) {
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();

View File

@ -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.
*/