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>
This commit is contained in:
parent
474e9d64b5
commit
5bfd13b81e
@ -362,10 +362,13 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
// Look up the keys of the object.
|
// Look up the keys of the object.
|
||||||
var keys = Object.keys(value);
|
var keys = Object.keys(value);
|
||||||
var visibleKeys = arrayToHash(keys);
|
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) {
|
if (ctx.showHidden) {
|
||||||
keys = Object.getOwnPropertyNames(value);
|
keys = Object.getOwnPropertyNames(value).concat(symbolKeys);
|
||||||
keys = keys.concat(Object.getOwnPropertySymbols(value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This could be a boxed primitive (new String(), etc.), check valueOf()
|
// This could be a boxed primitive (new String(), etc.), check valueOf()
|
||||||
|
@ -658,7 +658,9 @@ assert.doesNotThrow(() => {
|
|||||||
'{ a: 123, inspect: [Function: inspect] }');
|
'{ a: 123, inspect: [Function: inspect] }');
|
||||||
|
|
||||||
const subject = { a: 123, [util.inspect.custom]() { return this; } };
|
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
|
// 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;
|
subject[Symbol('symbol')] = 42;
|
||||||
|
|
||||||
assert.strictEqual(util.inspect(subject), '{}');
|
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
util.inspect(subject, options),
|
util.inspect(subject, options),
|
||||||
'{ [Symbol(symbol)]: 42 }'
|
'{ [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 = [1, 2, 3];
|
||||||
subject[Symbol('symbol')] = 42;
|
subject[Symbol('symbol')] = 42;
|
||||||
|
|
||||||
assert.strictEqual(util.inspect(subject), '[ 1, 2, 3 ]');
|
assert.strictEqual(util.inspect(subject),
|
||||||
assert.strictEqual(util.inspect(subject, options),
|
'[ 1, 2, 3, [Symbol(symbol)]: 42 ]');
|
||||||
'[ 1, 2, 3, [length]: 3, [Symbol(symbol)]: 42 ]');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// test Set
|
// test Set
|
||||||
|
Loading…
x
Reference in New Issue
Block a user