Skip to content

Commit b1c8f15

Browse files
committed
util: use constructor name
When reaching the depth limit util.inspect always prints [Array] or [Object] no matter if it is a subclass or not. This fixes it by showing the actual constructor name instead. PR-URL: #14886 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 4bcb1c3 commit b1c8f15

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/util.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
594594
return ctx.stylize('[Circular]', 'special');
595595

596596
if (recurseTimes != null) {
597-
if (recurseTimes < 0) {
598-
if (Array.isArray(value))
599-
return ctx.stylize('[Array]', 'special');
600-
return ctx.stylize('[Object]', 'special');
601-
}
597+
if (recurseTimes < 0)
598+
return ctx.stylize(`[${constructor ? constructor.name : 'Object'}]`,
599+
'special');
602600
recurseTimes -= 1;
603601
}
604602

test/parallel/test-util-inspect.js

+4
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,10 @@ if (typeof Symbol !== 'undefined') {
994994
'MapSubclass { \'foo\' => 42 }');
995995
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
996996
'PromiseSubclass { <pending> }');
997+
assert.strictEqual(
998+
util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }),
999+
'{ a: { b: [ArraySubclass] } }'
1000+
);
9971001
}
9981002

9991003
// Empty and circular before depth

0 commit comments

Comments
 (0)