From 72c1f48b4de12369f6788cc83f3a60792fecc3a7 Mon Sep 17 00:00:00 2001 From: scagood <2230835+scagood@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:27:10 +0000 Subject: [PATCH] fix: Correctly handle recursive objects on a per module basis --- lib/util/enumerate-property-names.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/util/enumerate-property-names.js b/lib/util/enumerate-property-names.js index de802c46..54fcc455 100644 --- a/lib/util/enumerate-property-names.js +++ b/lib/util/enumerate-property-names.js @@ -15,15 +15,16 @@ const unprefixNodeColon = require("./unprefix-node-colon") * Enumerate property names of a given object recursively. * @param {TraceMap} traceMap The map for APIs to enumerate. * @param {string[]} [path] The path to the current map. - * @param {WeakSet} [recursionSet] A WeakSet used to block recursion (eg Module, Module.Module, Module.Module.Module) + * @param {{ [key: string]: WeakSet }} [recursion] An object to block recursion (per module) * @returns {IterableIterator} The property names of the map. */ -function* enumeratePropertyNames( - traceMap, - path = [], - recursionSet = new WeakSet() -) { - if (recursionSet.has(traceMap)) { +function* enumeratePropertyNames(traceMap, path = [], recursion = {}) { + const recursionSet = + typeof path[0] === "string" + ? (recursion[path[0]] ??= new WeakSet()) + : undefined + + if (recursionSet?.has(traceMap)) { return } @@ -48,11 +49,8 @@ function* enumeratePropertyNames( yield childName } - yield* enumeratePropertyNames( - childValue, - childPath, - recursionSet.add(traceMap) - ) + recursionSet?.add(traceMap) + yield* enumeratePropertyNames(childValue, childPath, recursion) } }