Skip to content

Commit

Permalink
util: use proper circular reference checking
Browse files Browse the repository at this point in the history
Circular references are conceptually nothing that should be checked
for objects (or Sets or Maps) only, but applies to recursive structures
in general, so move the `seen` checks into a position where it is part
of the recursion itself.

Fixes: #14758
PR-URL: #14790
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: Alexey Orlenko <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed Sep 12, 2017
1 parent dcd7817 commit c3c6cb1
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,13 @@ function formatValue(ctx, value, recurseTimes) {
}
}

ctx.seen.push(value);

var output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
// TODO(addaleax): Make `seen` a Set to avoid linear-time lookup.
if (ctx.seen.includes(value)) {
return ctx.stylize('[Circular]', 'special');
}

ctx.seen.push(value);
const output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
ctx.seen.pop();

return reduceToSingleString(output, base, braces, ctx.breakLength);
Expand Down Expand Up @@ -835,21 +838,17 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
}
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
if (recurseTimes === null) {
str = formatValue(ctx, desc.value, null);
if (recurseTimes === null) {
str = formatValue(ctx, desc.value, null);
} else {
str = formatValue(ctx, desc.value, recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (array) {
str = str.replace(/\n/g, '\n ');
} else {
str = formatValue(ctx, desc.value, recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (array) {
str = str.replace(/\n/g, '\n ');
} else {
str = str.replace(/^|\n/g, '\n ');
}
str = str.replace(/^|\n/g, '\n ');
}
} else {
str = ctx.stylize('[Circular]', 'special');
}
}
if (name === undefined) {
Expand Down

0 comments on commit c3c6cb1

Please sign in to comment.