From 6260629f18d1235e02bbcb63babcb3c73bb8706a Mon Sep 17 00:00:00 2001 From: Marcel Jackwerth Date: Wed, 3 Jan 2018 13:58:53 +0100 Subject: [PATCH] [Plugins] Allow custom getCommentChildNodes (#3639) * [Plugins] Allow plugins to specify their own getChildNodes for comment printing * Rename getChildNodes -> getCommentChildNodes --- src/main/comments.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/comments.js b/src/main/comments.js index ad2fe68d..1c4787e5 100644 --- a/src/main/comments.js +++ b/src/main/comments.js @@ -45,13 +45,22 @@ function getSortedChildNodes(node, text, options, resultArray) { return node[childNodesCacheKey]; } - let names; - if (node && typeof node === "object") { - names = Object.keys(node).filter( - n => - n !== "enclosingNode" && n !== "precedingNode" && n !== "followingNode" - ); - } else { + let childNodes; + + if (printer.getCommentChildNodes) { + childNodes = printer.getCommentChildNodes(node); + } else if (node && typeof node === "object") { + childNodes = Object.keys(node) + .filter( + n => + n !== "enclosingNode" && + n !== "precedingNode" && + n !== "followingNode" + ) + .map(n => node[n]); + } + + if (!childNodes) { return; } @@ -62,9 +71,9 @@ function getSortedChildNodes(node, text, options, resultArray) { }); } - for (let i = 0, nameCount = names.length; i < nameCount; ++i) { - getSortedChildNodes(node[names[i]], text, options, resultArray); - } + childNodes.forEach(childNode => { + getSortedChildNodes(childNode, text, options, resultArray); + }); return resultArray; }