-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
fix: passing options to custom inspect #40
Conversation
Codecov Report
@@ Coverage Diff @@
## main #40 +/- ##
==========================================
+ Coverage 96.18% 96.19% +0.01%
==========================================
Files 2 2
Lines 341 342 +1
Branches 147 147
==========================================
+ Hits 328 329 +1
Misses 13 13
Continue to review full report at Codecov.
|
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.
Thanks, good catch.
221a1bf
to
ea15e31
Compare
index.js
Outdated
@@ -194,7 +194,16 @@ module.exports = function inspect_(obj, options, depth, seen) { | |||
} | |||
if (typeof obj === 'object' && customInspect) { | |||
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') { | |||
return obj[inspectSymbol](); | |||
return obj[inspectSymbol](maxDepth - depth, { | |||
breakLength: 60, |
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.
hardcoding these defaults seems problematic - i do get that node probably passes them explicitly. is there a way we could look up the values so that if node changes them, ours change too?
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.
🤔 They could be retrieved by doing a dummy inspect call.
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.
util.inspect.defaultOptions
might be helpful here
index.js
Outdated
customInspect: true, | ||
depth: maxDepth, | ||
maxArrayLength: 100, | ||
seen: seen, |
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.
passing this is a very bad idea.
seen: seen, |
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.
I am passing this because it is also passed by the nodejs inspect
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.
i'm pretty sure node's cache is different than ours, but either way, i'd rather not pass it here even if that causes breakage for a custom inspect function.
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.
Would it be better to pass an empty array? There is likely some code somewhere expect this to be an array?
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.
when i do var o = { [util.inspect.custom]() { console.log(arguments); } }; util.inspect(o);
in the repl, this is what i see as the first argument:
{
stylize: [Function: stylizeNoColor],
showHidden: false,
depth: 2,
colors: false,
customInspect: true,
showProxy: false,
maxArrayLength: 100,
maxStringLength: 10000,
breakLength: 80,
compact: 3,
sorted: false,
getters: false,
numericSeparator: false
}
which doesn't contain a "seen" property.
I also note that there's a third argument, which is the original inspect function. should we pass that?
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.
The third argument is a fancy new feature which, yeah, probably is a good idea to have.
seen
has been passed in older versions of node, I couldn't locate the commit where this changed.
Node internal objects (like URL) expect options to be passed in to inspect call. Without these options inspecting some native objects will result in unexpected errors.
f4cdc97
to
e1dd809
Compare
Since |
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.
simple approach, i like it
Node internal objects (like URL) expect options to be passed in to inspect call. Without these options inspecting some native objects will result in unexpected errors. I noticed this problem when inspecting a URL object:
This PR fixes this by also passing inspect options to inspect calls.