Skip to content

Commit

Permalink
util: display Symbol keys in inspect by default
Browse files Browse the repository at this point in the history
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 Mar 16, 2017
1 parent 474e9d6 commit 5bfd13b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 16 additions & 5 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5bfd13b

Please sign in to comment.