Permalink
Browse files

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 <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information...
mightyiam authored and addaleax committed Nov 21, 2016
1 parent 474e9d6 commit 5bfd13b81e38b60a7b9f346fbfcb216192cf0974
Showing with 21 additions and 7 deletions.
  1. +5 −2 lib/util.js
  2. +16 −5 test/parallel/test-util-inspect.js
View
@@ -362,10 +362,13 @@ function formatValue(ctx, value, recurseTimes) {
// Look up the keys of the object.
var keys = Object.keys(value);
var visibleKeys = arrayToHash(keys);
const symbolKeys = Object.getOwnPropertySymbols(value);
const enumSymbolKeys = symbolKeys
.filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
keys = keys.concat(enumSymbolKeys);
if (ctx.showHidden) {
keys = Object.getOwnPropertyNames(value);
keys = keys.concat(Object.getOwnPropertySymbols(value));
keys = Object.getOwnPropertyNames(value).concat(symbolKeys);
}
// This could be a boxed primitive (new String(), etc.), check valueOf()
@@ -658,7 +658,9 @@ assert.doesNotThrow(() => {
'{ a: 123, inspect: [Function: inspect] }');
const subject = { a: 123, [util.inspect.custom]() { return this; } };
assert.strictEqual(util.inspect(subject), '{ a: 123 }');
const UIC = 'util.inspect.custom';
assert.strictEqual(util.inspect(subject),
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
}
// util.inspect with "colors" option should produce as many lines as without it
@@ -725,18 +727,27 @@ if (typeof Symbol !== 'undefined') {
subject[Symbol('symbol')] = 42;
assert.strictEqual(util.inspect(subject), '{}');
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
assert.strictEqual(
util.inspect(subject, options),
'{ [Symbol(symbol)]: 42 }'
);
Object.defineProperty(
subject,
Symbol(),
{enumerable: false, value: 'non-enum'});
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
assert.strictEqual(
util.inspect(subject, options),
'{ [Symbol(symbol)]: 42, [Symbol()]: \'non-enum\' }'
);
subject = [1, 2, 3];
subject[Symbol('symbol')] = 42;
assert.strictEqual(util.inspect(subject), '[ 1, 2, 3 ]');
assert.strictEqual(util.inspect(subject, options),
'[ 1, 2, 3, [length]: 3, [Symbol(symbol)]: 42 ]');
assert.strictEqual(util.inspect(subject),
'[ 1, 2, 3, [Symbol(symbol)]: 42 ]');
}
// test Set

0 comments on commit 5bfd13b

Please sign in to comment.