Browse Source

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: https://github.com/nodejs/node/issues/9709
PR-URL: https://github.com/nodejs/node/pull/9726
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
v6
Shahar Or 8 years ago
committed by Anna Henningsen
parent
commit
5bfd13b81e
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 7
      lib/util.js
  2. 21
      test/parallel/test-util-inspect.js

7
lib/util.js

@ -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()

21
test/parallel/test-util-inspect.js

@ -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

Loading…
Cancel
Save