util: improve Symbol.toStringTag
handling
Only special handle `Symbol.toStringTag` if the property is not enumerable or not the own property of the inspected object. PR-URL: https://github.com/nodejs/node/pull/27342 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
80c0b89bbb
commit
2f1add18a4
@ -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;
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user