Skip to content

Commit

Permalink
util: improve Symbol.toStringTag handling
Browse files Browse the repository at this point in the history
Only special handle `Symbol.toStringTag` if the property is not
enumerable or not the own property of the inspected object.

PR-URL: #27342
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
BridgeAR authored and targos committed Apr 27, 2019
1 parent 4dfe54a commit 973d705
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,15 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {

const constructor = getConstructorName(value, ctx);
let tag = value[Symbol.toStringTag];
if (typeof tag !== 'string')
// Only list the tag in case it's non-enumerable / not an own property.
// Otherwise we'd print this twice.
if (typeof tag !== 'string' ||
tag !== '' &&
(ctx.showHidden ? hasOwnProperty : propertyIsEnumerable)(
value, Symbol.toStringTag
)) {
tag = '';
}
let base = '';
let formatter = getEmptyFormatArray;
let braces;
Expand Down
16 changes: 14 additions & 2 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1267,8 +1267,20 @@ util.inspect(process);

{
// @@toStringTag
assert.strictEqual(util.inspect({ [Symbol.toStringTag]: 'a' }),
"Object [a] { [Symbol(Symbol.toStringTag)]: 'a' }");
const obj = { [Symbol.toStringTag]: 'a' };
assert.strictEqual(
util.inspect(obj),
"{ [Symbol(Symbol.toStringTag)]: 'a' }"
);
Object.defineProperty(obj, Symbol.toStringTag, {
value: 'a',
enumerable: false
});
assert.strictEqual(util.inspect(obj), 'Object [a] {}');
assert.strictEqual(
util.inspect(obj, { showHidden: true }),
"{ [Symbol(Symbol.toStringTag)]: 'a' }"
);

class Foo {
constructor() {
Expand Down

0 comments on commit 973d705

Please sign in to comment.