Skip to content

Commit 5546d95

Browse files
committed
util: improve prototype inspection using inspect() and showHidden
The fast path for the prototype inspection had a bug that caused some prototype properties to be skipped that should in fact be inspected.
1 parent 3abe3f2 commit 5546d95

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lib/internal/util/inspect.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
470470
typeof descriptor.value === 'function' &&
471471
descriptor.value.name !== '') {
472472
if (protoProps !== undefined &&
473-
!builtInObjects.has(descriptor.value.name)) {
474-
const isProto = firstProto !== undefined;
473+
(firstProto !== obj ||
474+
!builtInObjects.has(descriptor.value.name))) {
475475
addPrototypeProperties(
476-
ctx, tmp, obj, recurseTimes, isProto, protoProps);
476+
ctx, tmp, firstProto || tmp, recurseTimes, protoProps);
477477
}
478478
return descriptor.value.name;
479479
}
@@ -511,12 +511,12 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
511511
// This function has the side effect of adding prototype properties to the
512512
// `output` argument (which is an array). This is intended to highlight user
513513
// defined prototype properties.
514-
function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
514+
function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
515515
let depth = 0;
516516
let keys;
517517
let keySet;
518518
do {
519-
if (!isProto) {
519+
if (depth !== 0 || main === obj) {
520520
obj = ObjectGetPrototypeOf(obj);
521521
// Stop as soon as a null prototype is encountered.
522522
if (obj === null) {
@@ -529,8 +529,6 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
529529
builtInObjects.has(descriptor.value.name)) {
530530
return;
531531
}
532-
} else {
533-
isProto = false;
534532
}
535533

536534
if (depth === 0) {

test/parallel/test-util-inspect.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,8 @@ util.inspect(process);
16961696
' 1,',
16971697
' 2,',
16981698
' [length]: 2',
1699-
' ]',
1699+
' ],',
1700+
" [Symbol(Symbol.toStringTag)]: 'Set Iterator'",
17001701
' } => <ref *1> [Map Iterator] {',
17011702
' Uint8Array(0) [',
17021703
' [BYTES_PER_ELEMENT]: 1,',
@@ -1708,7 +1709,8 @@ util.inspect(process);
17081709
' foo: true',
17091710
' }',
17101711
' ],',
1711-
' [Circular *1]',
1712+
' [Circular *1],',
1713+
" [Symbol(Symbol.toStringTag)]: 'Map Iterator'",
17121714
' }',
17131715
'}'
17141716
].join('\n');
@@ -1735,15 +1737,19 @@ util.inspect(process);
17351737
' [byteOffset]: 0,',
17361738
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
17371739
' ],',
1738-
' [Set Iterator] { [ 1, 2, [length]: 2 ] } => <ref *1> [Map Iterator] {',
1740+
' [Set Iterator] {',
1741+
' [ 1, 2, [length]: 2 ],',
1742+
" [Symbol(Symbol.toStringTag)]: 'Set Iterator'",
1743+
' } => <ref *1> [Map Iterator] {',
17391744
' Uint8Array(0) [',
17401745
' [BYTES_PER_ELEMENT]: 1,',
17411746
' [length]: 0,',
17421747
' [byteLength]: 0,',
17431748
' [byteOffset]: 0,',
17441749
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
17451750
' ],',
1746-
' [Circular *1]',
1751+
' [Circular *1],',
1752+
" [Symbol(Symbol.toStringTag)]: 'Map Iterator'",
17471753
' }',
17481754
'}'
17491755
].join('\n');
@@ -1773,7 +1779,9 @@ util.inspect(process);
17731779
' [Set Iterator] {',
17741780
' [ 1,',
17751781
' 2,',
1776-
' [length]: 2 ] } => <ref *1> [Map Iterator] {',
1782+
' [length]: 2 ],',
1783+
' [Symbol(Symbol.toStringTag)]:',
1784+
" 'Set Iterator' } => <ref *1> [Map Iterator] {",
17771785
' Uint8Array(0) [',
17781786
' [BYTES_PER_ELEMENT]: 1,',
17791787
' [length]: 0,',
@@ -1782,7 +1790,9 @@ util.inspect(process);
17821790
' [buffer]: ArrayBuffer {',
17831791
' byteLength: 0,',
17841792
' foo: true } ],',
1785-
' [Circular *1] } }'
1793+
' [Circular *1],',
1794+
' [Symbol(Symbol.toStringTag)]:',
1795+
" 'Map Iterator' } }"
17861796
].join('\n');
17871797

17881798
assert.strict.equal(out, expected);
@@ -2681,4 +2691,11 @@ assert.strictEqual(
26812691
' \x1B[2m[def]: \x1B[36m[Getter/Setter]\x1B[39m\x1B[22m\n' +
26822692
'}'
26832693
);
2694+
2695+
const obj = Object.create({ abc: true, def: 5, toString() {} });
2696+
assert.strictEqual(
2697+
inspect(obj, { showHidden: true, colors: true }),
2698+
'{ \x1B[2mabc: \x1B[33mtrue\x1B[39m\x1B[22m, ' +
2699+
'\x1B[2mdef: \x1B[33m5\x1B[39m\x1B[22m }'
2700+
);
26842701
}

0 commit comments

Comments
 (0)