Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Excl. non-enumerable property symbols from inspection by default
Including test
  • Loading branch information
mightyiam committed Nov 24, 2016
commit 3ace6a901ea89156888f3c5b44063d37ef291f69
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ function formatValue(ctx, value, recurseTimes) {
var keys = Object.keys(value);
var visibleKeys = arrayToHash(keys);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand what goes on with keys vs. visibleKeys, yet, this seems to work.

var symbolKeys = Object.getOwnPropertySymbols(value);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven’t run this but right now this looks like this will include even *non-*enumerable Symbol properties?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. That is how Object.getOwnPropertySymbols works. It is like the conversation between it and Object.getOwnPropertyNames is:

  • Object.getOwnPropertyNames: Hey, Symbol. You may join. But don't expect me to list you, even if you're enumerable...
  • Symbol: 😐
  • Object.getOwnPropertySymbols: Gee, did you see that guy? Tell you what, Symbol. I'll list all of you, regardless!
  • Symbol: 😟
  • Object.getOwnPropertyNames: 😦
  • Object.getOwnPropertySymbols: 😊
  • Symbol: 😕

I wouldn't mind changing it to exclude non-enumerable symbol-keyed props. Should I?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well… it seems a bit weird to list only enumerable string properties but all Symbol properties by default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just don't know the reasons for not listing non-enumerable properties. For the sake of explaining my experience with the matter, I'll testify that I:

  1. never made a property non-enumerable before examining this issue
  2. never had to examine the non-enumerable properties of an object. At least not that I know of.

So, it may help that one with experience with the topic of enumerability and non-enumerability will share his thoughts. Also, the thoughts of one who knows the intention of symbols in this regard and why this is the behavior of Object.getOwnPropertySymbols.

keys = keys.concat(symbolKeys);
var enumSymbolKeys = symbolKeys
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This one can be const

.filter((key) => value.propertyIsEnumerable(key));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we generally indent statement continuations by 4 spaces, not 2

keys = keys.concat(enumSymbolKeys);

if (ctx.showHidden) {
keys = Object.getOwnPropertyNames(value).concat(symbolKeys);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same result. The change is to not get the symbols twice.

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down