Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbol key props visible in inspection by default #9726

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,13 @@ function formatValue(ctx, value, recurseTimes) {
// Look up the keys of the object.
var keys = Object.keys(value);
var visibleKeys = arrayToHash(keys);
Copy link
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.

const symbolKeys = Object.getOwnPropertySymbols(value);
const enumSymbolKeys = symbolKeys
.filter((key) => value.propertyIsEnumerable(key));
Copy link
Member

@TimothyGu TimothyGu Feb 7, 2017

Choose a reason for hiding this comment

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

This would break if value does not inherit from Object.prototype. Object.getOwnPropertyDescriptor(value, key).enumerable should work.

keys = keys.concat(enumSymbolKeys);

if (ctx.showHidden) {
keys = Object.getOwnPropertyNames(value);
keys = keys.concat(Object.getOwnPropertySymbols(value));
keys = Object.getOwnPropertyNames(value).concat(symbolKeys);
Copy link
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.

}

// This could be a boxed primitive (new String(), etc.), check valueOf()
Expand Down
20 changes: 15 additions & 5 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,9 @@ assert.doesNotThrow(function() {
'{ a: 123, inspect: [Function: inspect] }');

const subject = { a: 123, [util.inspect.custom]() { return this; } };
const UIC = 'util.inspect.custom';
assert.strictEqual(util.inspect(subject),
'{ a: 123 }');
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
}

// util.inspect with "colors" option should produce as many lines as without it
Expand Down Expand Up @@ -659,18 +660,27 @@ if (typeof Symbol !== 'undefined') {

subject[Symbol('symbol')] = 42;

assert.strictEqual(util.inspect(subject), '{}');
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This demonstrates the change.

Copy link
Contributor Author

@mightyiam mightyiam Nov 21, 2016

Choose a reason for hiding this comment

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

Just the way I like it 😉

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 }');
Copy link
Contributor Author

@mightyiam mightyiam Nov 21, 2016

Choose a reason for hiding this comment

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

This assertion is removed because we already have an assertion above that symbol key properties are visible with showHidden.

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 ]');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So in essence, this assertion is not instead of the one in 672, but instead of the one in 671. I guess it shows that symbol key properties can coexist peacefully with other properties in inspection. Or something about the order of properties? In any case, I'm guessing that the essence is preserved.

}

// test Set
Expand Down