From b1c8f15c5f169e021f7c46eb7b219de95fe97603 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 17 Aug 2017 02:33:11 -0300 Subject: [PATCH] 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 Reviewed-By: Colin Ihrig Reviewed-By: Timothy Gu Reviewed-By: Yuta Hiroto Reviewed-By: Refael Ackermann --- lib/util.js | 8 +++----- test/parallel/test-util-inspect.js | 4 ++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/util.js b/lib/util.js index 07fbe15f38cb70..590a3b49034345 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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; } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 70f7c4d43139ea..02e35ec9e85562 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -994,6 +994,10 @@ if (typeof Symbol !== 'undefined') { 'MapSubclass { \'foo\' => 42 }'); assert.strictEqual(util.inspect(new PromiseSubclass(() => {})), 'PromiseSubclass { }'); + assert.strictEqual( + util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }), + '{ a: { b: [ArraySubclass] } }' + ); } // Empty and circular before depth