Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: #27668
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
BridgeAR committed May 16, 2019
commit 1bef82d3f0eeca518079f7adec8005cf54217e2a
18 changes: 13 additions & 5 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}{`;
}
}
}
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 @@ -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.
Expand Down