Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: inspect all prototypes #24974

Closed
wants to merge 1 commit into from
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
13 changes: 7 additions & 6 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function getEmptyFormatArray() {
return [];
}

function getConstructorName(obj) {
function getConstructorName(obj, ctx) {
let firstProto;
while (obj) {
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
Expand All @@ -341,10 +341,11 @@ function getConstructorName(obj) {
if (firstProto === null) {
return null;
}
// TODO(BridgeAR): Improve prototype inspection.
// We could use inspect on the prototype itself to improve the output.

return '';
return `<${inspect(firstProto, {
...ctx,
customInspect: false
})}>`;
}

function getPrefix(constructor, tag, fallback) {
Expand Down Expand Up @@ -503,7 +504,7 @@ function formatValue(ctx, value, recurseTimes) {
}

if (ctx.stop !== undefined) {
const name = getConstructorName(value) || value[Symbol.toStringTag];
const name = getConstructorName(value, ctx) || value[Symbol.toStringTag];
return ctx.stylize(`[${name || 'Object'}]`, 'special');
}

Expand Down Expand Up @@ -547,7 +548,7 @@ function formatValue(ctx, value, recurseTimes) {
function formatRaw(ctx, value, recurseTimes) {
let keys;

const constructor = getConstructorName(value);
const constructor = getConstructorName(value, ctx);
let tag = value[Symbol.toStringTag];
if (typeof tag !== 'string')
tag = '';
Expand Down
21 changes: 18 additions & 3 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1738,19 +1738,34 @@ assert.strictEqual(
);
}

// Manipulate the prototype to one that we can not handle.
// Manipulate the prototype in weird ways.
{
let obj = { a: true };
let value = (function() { return function() {}; })();
Object.setPrototypeOf(value, null);
Object.setPrototypeOf(obj, value);
assert.strictEqual(util.inspect(obj), '{ a: true }');
assert.strictEqual(util.inspect(obj), '<[Function]> { a: true }');
assert.strictEqual(
util.inspect(obj, { colors: true }),
'<\u001b[36m[Function]\u001b[39m> { a: \u001b[33mtrue\u001b[39m }'
);

obj = { a: true };
value = [];
Object.setPrototypeOf(value, null);
Object.setPrototypeOf(obj, value);
assert.strictEqual(util.inspect(obj), '{ a: true }');
assert.strictEqual(
util.inspect(obj),
'<[Array: null prototype] []> { a: true }'
);

function StorageObject() {}
StorageObject.prototype = Object.create(null);
assert.strictEqual(
util.inspect(new StorageObject()),
'<[Object: null prototype] {}> {}'
);

}

// Check that the fallback always works.
Expand Down