Skip to content
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

Merged
merged 1 commit into from
May 26, 2022

Conversation

martinheidegger
Copy link
Contributor

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:

node:internal/url:689
    if (opts.showHidden) {
             ^

TypeError: Cannot read properties of undefined (reading 'showHidden')
    at [nodejs.util.inspect.custom] (node:internal/url:689:14)
    at inspect_ (/inspect-js/object-inspect/index.js:197:38)
    at Test.<anonymous> (/inspect-js/object-inspect/test/inspect.js:110:9)
    at Test.bound [as _cb] (/inspect-js/object-inspect/node_modules/tape/lib/test.js:99:32)
    at Test.run (/inspect-js/object-inspect/node_modules/tape/lib/test.js:117:31)
    at Test.bound [as run] (/inspect-js/object-inspect/node_modules/tape/lib/test.js:99:32)
    at Immediate.next (/inspect-js/object-inspect/node_modules/tape/lib/results.js:88:19)
    at process.processImmediate (node:internal/timers:471:21)

This PR fixes this by also passing inspect options to inspect calls.

@codecov
Copy link

codecov bot commented May 25, 2022

Codecov Report

Merging #40 (ab0b3e0) into main (1db93bc) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head ab0b3e0 differs from pull request most recent head e243bf2. Consider uploading reports for the commit e243bf2 to get more accurate results

@@            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              
Impacted Files Coverage Δ
index.js 96.18% <100.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1db93bc...e243bf2. Read the comment docs.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch.

index.js Outdated Show resolved Hide resolved
test/inspect.js Outdated Show resolved Hide resolved
test/inspect.js Outdated Show resolved Hide resolved
test/inspect.js Outdated Show resolved Hide resolved
test/inspect.js Outdated Show resolved Hide resolved
test/inspect.js Outdated Show resolved Hide resolved
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,
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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,
Copy link
Member

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.

Suggested change
seen: seen,

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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.
@martinheidegger
Copy link
Contributor Author

Since util.inspect is not really compatible between node.js versions, I thought it is a fools errand to try to make it match somehow 🤷 . In ab0b3e0 I tried to fix it by simply using node's inspect function for custom inspects. It is not a perfect solution as the inspect calls will receive a wrong "opts.depth" property, but I think its a solution that can be lived with. If you are okay with this, I am happy to squash the commits.

Copy link
Member

@ljharb ljharb left a 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

@ljharb ljharb merged commit e243bf2 into inspect-js:main May 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants