Skip to content

Commit 5bfd13b

Browse files
mightyiamaddaleax
authored andcommitted
util: display Symbol keys in inspect by default
I use symbol key properties. And I find it awful that they do not show up in inspection. I can alter `util.inspect.defaultOptions.showHidden` each time I debug. Does that sound like fun to you? Isn't fun a core principle life? The way I see it, it is not about the spec or about what is enumerable/hidden, etc. When inspecting, it is about ease of access to the information. That's how I see it. Does anyone have any other thoughts? Fixes: #9709 PR-URL: #9726 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 474e9d6 commit 5bfd13b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/util.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ function formatValue(ctx, value, recurseTimes) {
362362
// Look up the keys of the object.
363363
var keys = Object.keys(value);
364364
var visibleKeys = arrayToHash(keys);
365+
const symbolKeys = Object.getOwnPropertySymbols(value);
366+
const enumSymbolKeys = symbolKeys
367+
.filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
368+
keys = keys.concat(enumSymbolKeys);
365369

366370
if (ctx.showHidden) {
367-
keys = Object.getOwnPropertyNames(value);
368-
keys = keys.concat(Object.getOwnPropertySymbols(value));
371+
keys = Object.getOwnPropertyNames(value).concat(symbolKeys);
369372
}
370373

371374
// This could be a boxed primitive (new String(), etc.), check valueOf()

test/parallel/test-util-inspect.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,9 @@ assert.doesNotThrow(() => {
658658
'{ a: 123, inspect: [Function: inspect] }');
659659

660660
const subject = { a: 123, [util.inspect.custom]() { return this; } };
661-
assert.strictEqual(util.inspect(subject), '{ a: 123 }');
661+
const UIC = 'util.inspect.custom';
662+
assert.strictEqual(util.inspect(subject),
663+
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
662664
}
663665

664666
// util.inspect with "colors" option should produce as many lines as without it
@@ -725,18 +727,27 @@ if (typeof Symbol !== 'undefined') {
725727

726728
subject[Symbol('symbol')] = 42;
727729

728-
assert.strictEqual(util.inspect(subject), '{}');
730+
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
729731
assert.strictEqual(
730732
util.inspect(subject, options),
731733
'{ [Symbol(symbol)]: 42 }'
732734
);
733735

736+
Object.defineProperty(
737+
subject,
738+
Symbol(),
739+
{enumerable: false, value: 'non-enum'});
740+
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
741+
assert.strictEqual(
742+
util.inspect(subject, options),
743+
'{ [Symbol(symbol)]: 42, [Symbol()]: \'non-enum\' }'
744+
);
745+
734746
subject = [1, 2, 3];
735747
subject[Symbol('symbol')] = 42;
736748

737-
assert.strictEqual(util.inspect(subject), '[ 1, 2, 3 ]');
738-
assert.strictEqual(util.inspect(subject, options),
739-
'[ 1, 2, 3, [length]: 3, [Symbol(symbol)]: 42 ]');
749+
assert.strictEqual(util.inspect(subject),
750+
'[ 1, 2, 3, [Symbol(symbol)]: 42 ]');
740751
}
741752

742753
// test Set

0 commit comments

Comments
 (0)