From 6984ca1c2ffe72b415cb12f0f0bf0f03b60860f7 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 12 May 2019 02:39:41 +0200 Subject: [PATCH] util: reconstruct constructor in more cases This makes sure the constructor is reconstructed in cases where we otherwise would not be able to detect the actual constructor anymore. That way some `util.inspect` output is improved. PR-URL: https://github.com/nodejs/node/pull/27668 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Anto Aravinth --- lib/internal/util/inspect.js | 18 +++++++++++++----- test/parallel/test-util-inspect.js | 4 ++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index e51ea04c4a0208..ebb43204d052fa 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -727,12 +727,20 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { braces = getIteratorBraces('Set', tag); formatter = formatIterator; // Handle other regular objects again. - } else if (keys.length === 0) { - if (isExternal(value)) - return ctx.stylize('[External]', 'special'); - return `${getPrefix(constructor, tag, 'Object')}{}`; } else { - braces[0] = `${getPrefix(constructor, tag, 'Object')}{`; + let fallback = ''; + if (constructor === null) { + fallback = internalGetConstructorName(value); + if (fallback === tag) { + fallback = 'Object'; + } + } + if (keys.length === 0) { + if (isExternal(value)) + return ctx.stylize('[External]', 'special'); + return `${getPrefix(constructor, tag, fallback)}{}`; + } + braces[0] = `${getPrefix(constructor, tag, fallback)}{`; } } } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 4ecb69997863b0..8a23621a18811d 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1156,6 +1156,10 @@ if (typeof Symbol !== 'undefined') { util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }), '{ a: { b: [ArraySubclass] } }' ); + assert.strictEqual( + util.inspect(Object.setPrototypeOf(x, null)), + '[ObjectSubclass: null prototype] { foo: 42 }' + ); } // Empty and circular before depth.