Skip to content

Commit

Permalink
fix: Correctly handle recursive objects on a per module basis
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood committed Dec 9, 2024
1 parent 8f23d37 commit 72c1f48
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/util/enumerate-property-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<TraceMap>} [recursionSet] A WeakSet used to block recursion (eg Module, Module.Module, Module.Module.Module)
* @param {{ [key: string]: WeakSet<TraceMap> }} [recursion] An object to block recursion (per module)
* @returns {IterableIterator<string>} 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
}

Expand All @@ -48,11 +49,8 @@ function* enumeratePropertyNames(
yield childName
}

yield* enumeratePropertyNames(
childValue,
childPath,
recursionSet.add(traceMap)
)
recursionSet?.add(traceMap)
yield* enumeratePropertyNames(childValue, childPath, recursion)
}
}

Expand Down

0 comments on commit 72c1f48

Please sign in to comment.