Skip to content

Commit

Permalink
util: use constructor name
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
BridgeAR committed Sep 19, 2017
1 parent 4bcb1c3 commit b1c8f15
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
return ctx.stylize('[Circular]', 'special');

if (recurseTimes != null) {
if (recurseTimes < 0) {
if (Array.isArray(value))
return ctx.stylize('[Array]', 'special');
return ctx.stylize('[Object]', 'special');
}
if (recurseTimes < 0)
return ctx.stylize(`[${constructor ? constructor.name : 'Object'}]`,
'special');
recurseTimes -= 1;
}

Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,10 @@ if (typeof Symbol !== 'undefined') {
'MapSubclass { \'foo\' => 42 }');
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
'PromiseSubclass { <pending> }');
assert.strictEqual(
util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }),
'{ a: { b: [ArraySubclass] } }'
);
}

// Empty and circular before depth
Expand Down

0 comments on commit b1c8f15

Please sign in to comment.