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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Ruben Bridgewater 2017-08-17 02:33:11 -03:00
parent 4bcb1c3f75
commit b1c8f15c5f
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 7 additions and 5 deletions

View File

@ -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;
}

View File

@ -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