From 903f353497385d16ecbd9bb42f85ee05f28d5cb2 Mon Sep 17 00:00:00 2001 From: Shahar Or Date: Tue, 22 Nov 2016 00:14:36 +0200 Subject: [PATCH 1/5] Symbol key props visible in inspection 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 (which should not have been closed so fast) --- lib/util.js | 5 +++-- test/parallel/test-util-inspect.js | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/util.js b/lib/util.js index 37008b2d176062..5f9c27a19bde40 100644 --- a/lib/util.js +++ b/lib/util.js @@ -371,10 +371,11 @@ function formatValue(ctx, value, recurseTimes) { // Look up the keys of the object. var keys = Object.keys(value); var visibleKeys = arrayToHash(keys); + var symbolKeys = Object.getOwnPropertySymbols(value); + keys = keys.concat(symbolKeys); 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 7fd682954207d3..f9b8c33063510c 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -588,8 +588,9 @@ assert.doesNotThrow(function() { '{ a: 123, inspect: [Function: inspect] }'); const subject = { a: 123, [util.inspect.custom]() { return this; } }; + const UTC = 'util.inspect.custom'; assert.strictEqual(util.inspect(subject), - '{ a: 123 }'); + `{ a: 123,\n [Symbol(${UTC})]: [Function: [${UTC}]] }`); } // util.inspect with "colors" option should produce as many lines as without it @@ -659,7 +660,7 @@ 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 }' @@ -668,9 +669,8 @@ if (typeof Symbol !== 'undefined') { 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 From 192d79a76646da9fb004646aa99baa9ed867d311 Mon Sep 17 00:00:00 2001 From: Shahar Or Date: Tue, 22 Nov 2016 20:54:02 +0200 Subject: [PATCH 2/5] test/parallel/test-util-inspect: const UTC -> const UIC --- test/parallel/test-util-inspect.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index f9b8c33063510c..b99c756feff64f 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -588,9 +588,9 @@ assert.doesNotThrow(function() { '{ a: 123, inspect: [Function: inspect] }'); const subject = { a: 123, [util.inspect.custom]() { return this; } }; - const UTC = 'util.inspect.custom'; + const UIC = 'util.inspect.custom'; assert.strictEqual(util.inspect(subject), - `{ a: 123,\n [Symbol(${UTC})]: [Function: [${UTC}]] }`); + `{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`); } // util.inspect with "colors" option should produce as many lines as without it From 3ace6a901ea89156888f3c5b44063d37ef291f69 Mon Sep 17 00:00:00 2001 From: Shahar Or Date: Thu, 24 Nov 2016 02:32:38 +0200 Subject: [PATCH 3/5] Excl. non-enumerable property symbols from inspection by default Including test --- lib/util.js | 4 +++- test/parallel/test-util-inspect.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 5f9c27a19bde40..53e4dc748b6143 100644 --- a/lib/util.js +++ b/lib/util.js @@ -372,7 +372,9 @@ function formatValue(ctx, value, recurseTimes) { var keys = Object.keys(value); var visibleKeys = arrayToHash(keys); var symbolKeys = Object.getOwnPropertySymbols(value); - keys = keys.concat(symbolKeys); + var enumSymbolKeys = symbolKeys + .filter((key) => value.propertyIsEnumerable(key)); + keys = keys.concat(enumSymbolKeys); if (ctx.showHidden) { keys = Object.getOwnPropertyNames(value).concat(symbolKeys); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index b99c756feff64f..e86a10c43f63db 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -666,6 +666,16 @@ if (typeof Symbol !== 'undefined') { '{ [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; From 8e73c9501c0a4e88fbfd933ddc6ef1ebc0b50ad3 Mon Sep 17 00:00:00 2001 From: Shahar Or Date: Thu, 24 Nov 2016 23:39:18 +0200 Subject: [PATCH 4/5] lib/util: use const for symbolKeys & enumSymbolKeys --- lib/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 53e4dc748b6143..c7be8f3a5212af 100644 --- a/lib/util.js +++ b/lib/util.js @@ -371,8 +371,8 @@ function formatValue(ctx, value, recurseTimes) { // Look up the keys of the object. var keys = Object.keys(value); var visibleKeys = arrayToHash(keys); - var symbolKeys = Object.getOwnPropertySymbols(value); - var enumSymbolKeys = symbolKeys + const symbolKeys = Object.getOwnPropertySymbols(value); + const enumSymbolKeys = symbolKeys .filter((key) => value.propertyIsEnumerable(key)); keys = keys.concat(enumSymbolKeys); From 3cbd8472c2d31e340fdbe9af2aa2e3054ffdfc19 Mon Sep 17 00:00:00 2001 From: Shahar Or Date: Thu, 24 Nov 2016 23:40:18 +0200 Subject: [PATCH 5/5] lib/util: fix indentation of enumSymbolKeys's filter call --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index c7be8f3a5212af..d6a3c6dbc67b2e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -373,7 +373,7 @@ function formatValue(ctx, value, recurseTimes) { var visibleKeys = arrayToHash(keys); const symbolKeys = Object.getOwnPropertySymbols(value); const enumSymbolKeys = symbolKeys - .filter((key) => value.propertyIsEnumerable(key)); + .filter((key) => value.propertyIsEnumerable(key)); keys = keys.concat(enumSymbolKeys); if (ctx.showHidden) {