-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
util: fix circular ref for Set and Map #14775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -738,7 +738,9 @@ function formatSet(ctx, value, recurseTimes, visibleKeys, keys) { | |
| var output = []; | ||
| value.forEach(function(v) { | ||
| var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1; | ||
| var str = formatValue(ctx, v, nextRecurseTimes); | ||
| var str = ctx.seen.includes(v) ? | ||
| ctx.stylize('[Circular]', 'special') : | ||
| formatValue(ctx, v, nextRecurseTimes); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t know if you tried that, but wouldn’t it be better to do this type of check/early return in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If not: Can we DRY this up a bit, maybe add a function for it or something?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @not-an-aardvark @BridgeAR @aqrln This is what I was having in mind: diff in the folddiff --git a/lib/util.js b/lib/util.js
index a9f267bd416d..9b8e8395ffac 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -216,7 +216,7 @@ function debuglog(set) {
function inspect(obj, opts) {
// default options
var ctx = {
- seen: [],
+ seen: new Set(),
stylize: stylizeNoColor
};
// legacy...
@@ -613,11 +613,16 @@ function formatValue(ctx, value, recurseTimes) {
}
}
- ctx.seen.push(value);
+ var output;
+ if (ctx.seen.has(value)) {
+ output = ctx.stylize('[Circular]', 'special');
+ } else {
+ ctx.seen.push(value);
- var output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
+ output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
- ctx.seen.pop();
+ ctx.seen.pop();
+ }
return reduceToSingleString(output, base, braces, ctx.breakLength);
}
@@ -822,21 +827,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);
+ str = str.replace(/^|\n/g, '\n ');
}
- if (str.indexOf('\n') > -1) {
- if (array) {
- str = str.replace(/\n/g, '\n ');
- } else {
- str = str.replace(/^|\n/g, '\n ');
- }
- }
- } else {
- str = ctx.stylize('[Circular]', 'special');
}
}
if (name === undefined) {WDYT?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am aware of that (ref #14492 (comment)) but I would rather keep the concerns separated.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @addaleax That would be a semver-major change as the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One non-breaking workaround would be to continue to make
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ll just open a PR and we can discuss from there. |
||
| output.push(str); | ||
| }); | ||
| for (var n = 0; n < keys.length; n++) { | ||
|
|
@@ -753,9 +755,13 @@ function formatMap(ctx, value, recurseTimes, visibleKeys, keys) { | |
| var output = []; | ||
| value.forEach(function(v, k) { | ||
| var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1; | ||
| var str = formatValue(ctx, k, nextRecurseTimes); | ||
| var str = ctx.seen.includes(k) ? | ||
| ctx.stylize('[Circular]', 'special') : | ||
| formatValue(ctx, k, nextRecurseTimes); | ||
| str += ' => '; | ||
| str += formatValue(ctx, v, nextRecurseTimes); | ||
| str += ctx.seen.includes(v) ? | ||
| ctx.stylize('[Circular]', 'special') : | ||
| formatValue(ctx, v, nextRecurseTimes); | ||
| output.push(str); | ||
| }); | ||
| for (var n = 0; n < keys.length; n++) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't directly related to this PR, but why is
ctx.seenan array? Would it be better to use aSet/WeakSetto avoid linear-timeincludeschecks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #14775 (comment)