Skip to content

Commit fc0febe

Browse files
BridgeARMylesBorins
authored andcommitted
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. Backport-PR-URL: #31431 PR-URL: #31113 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 6f5ced6 commit fc0febe

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lib/internal/util/inspect.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
467467
typeof descriptor.value === 'function' &&
468468
descriptor.value.name !== '') {
469469
if (protoProps !== undefined &&
470-
!builtInObjects.has(descriptor.value.name)) {
471-
const isProto = firstProto !== undefined;
470+
(firstProto !== obj ||
471+
!builtInObjects.has(descriptor.value.name))) {
472472
addPrototypeProperties(
473-
ctx, tmp, obj, recurseTimes, isProto, protoProps);
473+
ctx, tmp, firstProto || tmp, recurseTimes, protoProps);
474474
}
475475
return descriptor.value.name;
476476
}
@@ -508,12 +508,12 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
508508
// This function has the side effect of adding prototype properties to the
509509
// `output` argument (which is an array). This is intended to highlight user
510510
// defined prototype properties.
511-
function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
511+
function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
512512
let depth = 0;
513513
let keys;
514514
let keySet;
515515
do {
516-
if (!isProto) {
516+
if (depth !== 0 || main === obj) {
517517
obj = ObjectGetPrototypeOf(obj);
518518
// Stop as soon as a null prototype is encountered.
519519
if (obj === null) {
@@ -526,8 +526,6 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
526526
builtInObjects.has(descriptor.value.name)) {
527527
return;
528528
}
529-
} else {
530-
isProto = false;
531529
}
532530

533531
if (depth === 0) {

test/parallel/test-util-inspect.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,8 @@ util.inspect(process);
16871687
' 1,',
16881688
' 2,',
16891689
' [length]: 2',
1690-
' ]',
1690+
' ],',
1691+
" [Symbol(Symbol.toStringTag)]: 'Set Iterator'",
16911692
' } => [Map Iterator] {',
16921693
' Uint8Array(0) [',
16931694
' [BYTES_PER_ELEMENT]: 1,',
@@ -1699,7 +1700,8 @@ util.inspect(process);
16991700
' foo: true',
17001701
' }',
17011702
' ],',
1702-
' [Circular]',
1703+
' [Circular],',
1704+
" [Symbol(Symbol.toStringTag)]: 'Map Iterator'",
17031705
' },',
17041706
' [size]: 2',
17051707
'}'
@@ -1727,15 +1729,19 @@ util.inspect(process);
17271729
' [byteOffset]: 0,',
17281730
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
17291731
' ],',
1730-
' [Set Iterator] { [ 1, 2, [length]: 2 ] } => [Map Iterator] {',
1732+
' [Set Iterator] {',
1733+
' [ 1, 2, [length]: 2 ],',
1734+
" [Symbol(Symbol.toStringTag)]: 'Set Iterator'",
1735+
' } => [Map Iterator] {',
17311736
' Uint8Array(0) [',
17321737
' [BYTES_PER_ELEMENT]: 1,',
17331738
' [length]: 0,',
17341739
' [byteLength]: 0,',
17351740
' [byteOffset]: 0,',
17361741
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
17371742
' ],',
1738-
' [Circular]',
1743+
' [Circular],',
1744+
" [Symbol(Symbol.toStringTag)]: 'Map Iterator'",
17391745
' },',
17401746
' [size]: 2',
17411747
'}'
@@ -1767,7 +1773,9 @@ util.inspect(process);
17671773
' [Set Iterator] {',
17681774
' [ 1,',
17691775
' 2,',
1770-
' [length]: 2 ] } => [Map Iterator] {',
1776+
' [length]: 2 ],',
1777+
' [Symbol(Symbol.toStringTag)]:',
1778+
" 'Set Iterator' } => [Map Iterator] {",
17711779
' Uint8Array(0) [',
17721780
' [BYTES_PER_ELEMENT]: 1,',
17731781
' [length]: 0,',
@@ -1776,7 +1784,9 @@ util.inspect(process);
17761784
' [buffer]: ArrayBuffer {',
17771785
' byteLength: 0,',
17781786
' foo: true } ],',
1779-
' [Circular] },',
1787+
' [Circular],',
1788+
' [Symbol(Symbol.toStringTag)]:',
1789+
" 'Map Iterator' },",
17801790
' [size]: 2 }'
17811791
].join('\n');
17821792

@@ -2678,4 +2688,11 @@ assert.strictEqual(
26782688
' \x1B[2m[def]: \x1B[36m[Getter/Setter]\x1B[39m\x1B[22m\n' +
26792689
'}'
26802690
);
2691+
2692+
const obj = Object.create({ abc: true, def: 5, toString() {} });
2693+
assert.strictEqual(
2694+
inspect(obj, { showHidden: true, colors: true }),
2695+
'{ \x1B[2mabc: \x1B[33mtrue\x1B[39m\x1B[22m, ' +
2696+
'\x1B[2mdef: \x1B[33m5\x1B[39m\x1B[22m }'
2697+
);
26812698
}

0 commit comments

Comments
 (0)