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: improve prototype inspection using inspect() and showHidden #31113

Closed
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
12 changes: 5 additions & 7 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
typeof descriptor.value === 'function' &&
descriptor.value.name !== '') {
if (protoProps !== undefined &&
!builtInObjects.has(descriptor.value.name)) {
const isProto = firstProto !== undefined;
(firstProto !== obj ||
!builtInObjects.has(descriptor.value.name))) {
addPrototypeProperties(
ctx, tmp, obj, recurseTimes, isProto, protoProps);
ctx, tmp, firstProto || tmp, recurseTimes, protoProps);
}
return descriptor.value.name;
}
Expand Down Expand Up @@ -511,12 +511,12 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
// This function has the side effect of adding prototype properties to the
// `output` argument (which is an array). This is intended to highlight user
// defined prototype properties.
function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
let depth = 0;
let keys;
let keySet;
do {
if (!isProto) {
if (depth !== 0 || main === obj) {
obj = ObjectGetPrototypeOf(obj);
// Stop as soon as a null prototype is encountered.
if (obj === null) {
Expand All @@ -529,8 +529,6 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
builtInObjects.has(descriptor.value.name)) {
return;
}
} else {
isProto = false;
}

if (depth === 0) {
Expand Down
29 changes: 23 additions & 6 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,8 @@ util.inspect(process);
' 1,',
' 2,',
' [length]: 2',
' ]',
' ],',
" [Symbol(Symbol.toStringTag)]: 'Set Iterator'",
' } => <ref *1> [Map Iterator] {',
' Uint8Array(0) [',
' [BYTES_PER_ELEMENT]: 1,',
Expand All @@ -1708,7 +1709,8 @@ util.inspect(process);
' foo: true',
' }',
' ],',
' [Circular *1]',
' [Circular *1],',
" [Symbol(Symbol.toStringTag)]: 'Map Iterator'",
' }',
'}'
].join('\n');
Expand All @@ -1735,15 +1737,19 @@ util.inspect(process);
' [byteOffset]: 0,',
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
' ],',
' [Set Iterator] { [ 1, 2, [length]: 2 ] } => <ref *1> [Map Iterator] {',
' [Set Iterator] {',
' [ 1, 2, [length]: 2 ],',
" [Symbol(Symbol.toStringTag)]: 'Set Iterator'",
' } => <ref *1> [Map Iterator] {',
' Uint8Array(0) [',
' [BYTES_PER_ELEMENT]: 1,',
' [length]: 0,',
' [byteLength]: 0,',
' [byteOffset]: 0,',
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
' ],',
' [Circular *1]',
' [Circular *1],',
" [Symbol(Symbol.toStringTag)]: 'Map Iterator'",
' }',
'}'
].join('\n');
Expand Down Expand Up @@ -1773,7 +1779,9 @@ util.inspect(process);
' [Set Iterator] {',
' [ 1,',
' 2,',
' [length]: 2 ] } => <ref *1> [Map Iterator] {',
' [length]: 2 ],',
' [Symbol(Symbol.toStringTag)]:',
" 'Set Iterator' } => <ref *1> [Map Iterator] {",
' Uint8Array(0) [',
' [BYTES_PER_ELEMENT]: 1,',
' [length]: 0,',
Expand All @@ -1782,7 +1790,9 @@ util.inspect(process);
' [buffer]: ArrayBuffer {',
' byteLength: 0,',
' foo: true } ],',
' [Circular *1] } }'
' [Circular *1],',
' [Symbol(Symbol.toStringTag)]:',
" 'Map Iterator' } }"
].join('\n');

assert.strict.equal(out, expected);
Expand Down Expand Up @@ -2681,4 +2691,11 @@ assert.strictEqual(
' \x1B[2m[def]: \x1B[36m[Getter/Setter]\x1B[39m\x1B[22m\n' +
'}'
);

const obj = Object.create({ abc: true, def: 5, toString() {} });
assert.strictEqual(
inspect(obj, { showHidden: true, colors: true }),
'{ \x1B[2mabc: \x1B[33mtrue\x1B[39m\x1B[22m, ' +
'\x1B[2mdef: \x1B[33m5\x1B[39m\x1B[22m }'
);
}