Refactor: Stop mutating AST during getSortedChildNodes (#9797)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-11-30 18:10:30 +08:00
1 parent c0fbd75288
commit fdaaf2298a
1 file changed
+5 -8
+5 -8
View File
@@ -26,8 +26,7 @@ const {
addTrailingComment,
} = require("../common/util");
const childNodesCacheKey = Symbol("child-nodes");
const childNodesCache = new WeakMap();
function getSortedChildNodes(node, options, resultArray) {
if (!node) {
return;
@@ -51,8 +50,8 @@ function getSortedChildNodes(node, options, resultArray) {
resultArray.splice(i + 1, 0, node);
return;
}
} else if (node[childNodesCacheKey]) {
return node[childNodesCacheKey];
} else if (childNodesCache.has(node)) {
return childNodesCache.get(node);
}
const childNodes =
@@ -75,10 +74,8 @@ function getSortedChildNodes(node, options, resultArray) {
}
if (!resultArray) {
Object.defineProperty(node, childNodesCacheKey, {
value: (resultArray = []),
enumerable: false,
});
resultArray = [];
childNodesCache.set(node, resultArray);
}
childNodes.forEach((childNode) => {