From 923c02dded80ba8ce74861c1a39392a6ad8e1ffa Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 13 Jun 2019 18:08:24 +0200 Subject: [PATCH] util: hide toStringTags if already included in the constructor This makes sure that `Symbol.toStringTag` entries won't be taken into account when inspecting objects with `util.inspect()` in case the tag is already included in the found constructor name. --- lib/internal/util/inspect.js | 2 +- test/parallel/test-util-inspect.js | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index e9dc8d83eac9d8..5c0a73e75b23f3 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -386,7 +386,7 @@ function getPrefix(constructor, tag, fallback) { return `[${fallback}: null prototype] `; } - if (tag !== '' && constructor !== tag) { + if (tag !== '' && !constructor.includes(tag)) { return `${constructor} [${tag}] `; } return `${constructor} `; diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 03cbebe164316c..a8d5ab67039f04 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1227,7 +1227,9 @@ if (typeof Symbol !== 'undefined') { class ArraySubclass extends Array {} class SetSubclass extends Set {} class MapSubclass extends Map {} + class MSubclass extends Map {} class PromiseSubclass extends Promise {} + class PSubclass extends Promise {} const x = new ObjectSubclass(); x.foo = 42; @@ -1236,11 +1238,15 @@ if (typeof Symbol !== 'undefined') { assert.strictEqual(util.inspect(new ArraySubclass(1, 2, 3)), 'ArraySubclass [ 1, 2, 3 ]'); assert.strictEqual(util.inspect(new SetSubclass([1, 2, 3])), - 'SetSubclass [Set] { 1, 2, 3 }'); + 'SetSubclass { 1, 2, 3 }'); assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])), - "MapSubclass [Map] { 'foo' => 42 }"); + "MapSubclass { 'foo' => 42 }"); + assert.strictEqual(util.inspect(new MSubclass([['foo', 42]])), + "MSubclass [Map] { 'foo' => 42 }"); assert.strictEqual(util.inspect(new PromiseSubclass(() => {})), - 'PromiseSubclass [Promise] { }'); + 'PromiseSubclass { }'); + assert.strictEqual(util.inspect(new PSubclass(() => {})), + 'PSubclass [Promise] { }'); assert.strictEqual( util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }), '{ a: { b: [ArraySubclass] } }'