Skip to content

Commit

Permalink
fix: passing options to custom inspect
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinheidegger committed May 26, 2022
1 parent 1db93bc commit ea15e31
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
colors: false,
customInspect: true,
depth: maxDepth,
maxArrayLength: 100,
seen: seen,
showHidden: false,
showProxy: false
});
} else if (customInspect !== 'symbol' && typeof obj.inspect === 'function') {
return obj.inspect();
}
Expand Down
38 changes: 37 additions & 1 deletion test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var test = require('tape');
var hasSymbols = require('has-symbols/shams')();
var utilInspect = require('../util.inspect');
var repeat = require('string.prototype.repeat');

var inspect = require('..');

test('inspect', function (t) {
Expand Down Expand Up @@ -100,3 +99,40 @@ test('maxStringLength', function (t) {

t.end();
});

test('inspect Options', function (t) {
var obj = {};
obj[utilInspect.custom] = function (depth, opts) {
return String(depth) + ':' + JSON.stringify(opts);
};
t.equal(
inspect(obj),
'5:{"breakLength":60,"colors":false,"customInspect":true,"depth":5,"maxArrayLength":100,"seen":[],"showHidden":false,"showProxy":false}',
'direct inspect will fallback to default maxdepth = 5'
);
t.equal(
inspect(obj, { depth: 2 }),
'2:{"breakLength":60,"colors":false,"customInspect":true,"depth":2,"maxArrayLength":100,"seen":[],"showHidden":false,"showProxy":false}',
'custom depth will be passed to the inspect call'
);
t.equal(
inspect({ d1: obj }, { depth: 3 }),
'{ d1: 2:{"breakLength":60,"colors":false,"customInspect":true,"depth":3,"maxArrayLength":100,"seen":[{"d1":{}}],"showHidden":false,"showProxy":false} }',
'the passed-in depth is reduced if the object is deeper in the hierarchy'
);
t.equal(
inspect({ d1: obj }, { depth: 1 }),
'{ d1: [Object] }',
'unlike nodejs inspect, customInspect will not be used once the depth is exceeded.'
);
t.end();
});

test('inspect URL', { skip: typeof URL === 'undefined' }, function (t) {
t.equal(
inspect(new URL('https://nodejs.org')),
'URL {\n href: \'https://nodejs.org/\',\n origin: \'https://nodejs.org\',\n protocol: \'https:\',\n username: \'\',\n password: \'\',\n host: \'nodejs.org\',\n hostname: \'nodejs.org\',\n port: \'\',\n pathname: \'/\',\n search: \'\',\n searchParams: URLSearchParams {},\n hash: \'\'\n}',
'url can be inspected'
);
t.end();
});

0 comments on commit ea15e31

Please sign in to comment.