Skip to content

Commit 6984ca1

Browse files
BridgeARtargos
authored andcommitted
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 <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 7cc21d8 commit 6984ca1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/internal/util/inspect.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -727,12 +727,20 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
727727
braces = getIteratorBraces('Set', tag);
728728
formatter = formatIterator;
729729
// Handle other regular objects again.
730-
} else if (keys.length === 0) {
731-
if (isExternal(value))
732-
return ctx.stylize('[External]', 'special');
733-
return `${getPrefix(constructor, tag, 'Object')}{}`;
734730
} else {
735-
braces[0] = `${getPrefix(constructor, tag, 'Object')}{`;
731+
let fallback = '';
732+
if (constructor === null) {
733+
fallback = internalGetConstructorName(value);
734+
if (fallback === tag) {
735+
fallback = 'Object';
736+
}
737+
}
738+
if (keys.length === 0) {
739+
if (isExternal(value))
740+
return ctx.stylize('[External]', 'special');
741+
return `${getPrefix(constructor, tag, fallback)}{}`;
742+
}
743+
braces[0] = `${getPrefix(constructor, tag, fallback)}{`;
736744
}
737745
}
738746
}

test/parallel/test-util-inspect.js

+4
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,10 @@ if (typeof Symbol !== 'undefined') {
11561156
util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }),
11571157
'{ a: { b: [ArraySubclass] } }'
11581158
);
1159+
assert.strictEqual(
1160+
util.inspect(Object.setPrototypeOf(x, null)),
1161+
'[ObjectSubclass: null prototype] { foo: 42 }'
1162+
);
11591163
}
11601164

11611165
// Empty and circular before depth.

0 commit comments

Comments
 (0)