Skip to content

Commit e243bf2

Browse files
martinheideggerljharb
authored andcommitted
[Fix] use util.inspect for a custom inspection symbol method
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.
1 parent 1db93bc commit e243bf2

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ function addNumericSeparator(num, str) {
6565
return $replace.call(str, sepRegex, '$&_');
6666
}
6767

68-
var inspectCustom = require('./util.inspect').custom;
69-
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;
68+
var utilInspect = require('./util.inspect');
69+
var inspectCustom = utilInspect.custom;
70+
var inspectSymbol = isSymbol(inspectCustom) ? inspectCustom : null;
7071

7172
module.exports = function inspect_(obj, options, depth, seen) {
7273
var opts = options || {};
@@ -193,8 +194,8 @@ module.exports = function inspect_(obj, options, depth, seen) {
193194
return '{ [' + String(obj) + '] ' + $join.call(parts, ', ') + ' }';
194195
}
195196
if (typeof obj === 'object' && customInspect) {
196-
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
197-
return obj[inspectSymbol]();
197+
if (inspectSymbol && typeof obj[inspectSymbol] === 'function' && utilInspect) {
198+
return utilInspect(obj, { depth: maxDepth - depth });
198199
} else if (customInspect !== 'symbol' && typeof obj.inspect === 'function') {
199200
return obj.inspect();
200201
}

test/inspect.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,40 @@ test('maxStringLength', function (t) {
100100

101101
t.end();
102102
});
103+
104+
test('inspect options', { skip: !utilInspect.custom }, function (t) {
105+
var obj = {};
106+
obj[utilInspect.custom] = function () {
107+
return JSON.stringify(arguments);
108+
};
109+
t.equal(
110+
inspect(obj),
111+
utilInspect(obj, { depth: 5 }),
112+
'custom symbols will use node\'s inspect'
113+
);
114+
t.equal(
115+
inspect(obj, { depth: 2 }),
116+
utilInspect(obj, { depth: 2 }),
117+
'a reduced depth will be passed to node\'s inspect'
118+
);
119+
t.equal(
120+
inspect({ d1: obj }, { depth: 3 }),
121+
'{ d1: ' + utilInspect(obj, { depth: 2 }) + ' }',
122+
'deep objects will receive a reduced depth'
123+
);
124+
t.equal(
125+
inspect({ d1: obj }, { depth: 1 }),
126+
'{ d1: [Object] }',
127+
'unlike nodejs inspect, customInspect will not be used once the depth is exceeded.'
128+
);
129+
t.end();
130+
});
131+
132+
test('inspect URL', { skip: typeof URL === 'undefined' }, function (t) {
133+
t.match(
134+
inspect(new URL('https://nodejs.org')),
135+
/nodejs\.org/, // Different environments stringify it differently
136+
'url can be inspected'
137+
);
138+
t.end();
139+
});

0 commit comments

Comments
 (0)