From 5bfd13b81e38b60a7b9f346fbfcb216192cf0974 Mon Sep 17 00:00:00 2001 From: Shahar Or Date: Tue, 22 Nov 2016 00:14:36 +0200 Subject: [PATCH] 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 Reviewed-By: Ben Noordhuis --- lib/util.js | 7 +++++-- test/parallel/test-util-inspect.js | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/util.js b/lib/util.js index 31761f0dd35545..9e61bd83e05541 100644 --- a/lib/util.js +++ b/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() diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 033d15ff057850..873918f942284b 100644 --- a/test/parallel/test-util-inspect.js +++ b/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