From b7bebca1959ad020f64b9ea7166764d73d513149 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Mon, 11 May 2026 22:11:25 +0200 Subject: [PATCH] util: canonicalize namespaced tags in inspect() Signed-off-by: Renegade334 --- lib/internal/util/inspect.js | 17 +++++------- test/parallel/test-util-inspect.js | 42 ++++++++++++------------------ 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c611eb97fc0755..c08d48242a0eeb 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1031,20 +1031,15 @@ function getPrefix(constructor, tag, fallback, size = '') { return `[${fallback}${size}: null prototype] `; } - let result = `${constructor}${size} `; if (tag !== '') { - const position = constructor.indexOf(tag); - if (position === -1) { - result += `[${tag}] `; - } else { - const endPos = position + tag.length; - if (endPos !== constructor.length && - constructor[endPos] === constructor[endPos].toLowerCase()) { - result += `[${tag}] `; - } + const dot = StringPrototypeLastIndexOf(tag, '.'); + if (constructor === (dot >= 1 ? StringPrototypeSlice(tag, dot + 1) : tag)) { + return `${tag}${size} `; } + return `${constructor}${size} [${tag}] `; } - return result; + + return `${constructor}${size} `; } // Look up the keys of the object. diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index e60320d0591233..f2f9e2146d301a 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1475,11 +1475,11 @@ if (typeof Symbol !== 'undefined') { assert.strictEqual(util.inspect(new ArraySubclass(1, 2, 3)), 'ArraySubclass(3) [ 1, 2, 3 ]'); assert.strictEqual(util.inspect(new SetSubclass([1, 2, 3])), - 'SetSubclass(3) { 1, 2, 3 }'); + 'SetSubclass(3) [Set] { 1, 2, 3 }'); assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])), - "MapSubclass(1) { 'foo' => 42 }"); + "MapSubclass(1) [Map] { 'foo' => 42 }"); assert.strictEqual(util.inspect(new PromiseSubclass(() => {})), - 'PromiseSubclass { }'); + 'PromiseSubclass [Promise] { }'); assert.strictEqual(util.inspect(new SymbolNameClass()), 'Symbol(name) {}'); assert.strictEqual( @@ -1490,29 +1490,6 @@ if (typeof Symbol !== 'undefined') { util.inspect(Object.setPrototypeOf(x, null)), '[ObjectSubclass: null prototype] { foo: 42 }' ); - - class MiddleErrorPart extends Error {} - assert(util.inspect(new MiddleErrorPart('foo')).includes('MiddleErrorPart: foo')); - - class MapClass extends Map {} - assert.strictEqual(util.inspect(new MapClass([['key', 'value']])), - "MapClass(1) { 'key' => 'value' }"); - - class AbcMap extends Map {} - assert.strictEqual(util.inspect(new AbcMap([['key', 'value']])), - "AbcMap(1) { 'key' => 'value' }"); - - class SetAbc extends Set {} - assert.strictEqual(util.inspect(new SetAbc([1, 2, 3])), - 'SetAbc(3) { 1, 2, 3 }'); - - class FooSet extends Set {} - assert.strictEqual(util.inspect(new FooSet([1, 2, 3])), - 'FooSet(3) { 1, 2, 3 }'); - - class Settings extends Set {} - assert.strictEqual(util.inspect(new Settings([1, 2, 3])), - 'Settings(3) [Set] { 1, 2, 3 }'); } // Empty and circular before depth. @@ -4033,3 +4010,16 @@ ${error.stack.split('\n').slice(1).join('\n')}`, assert.match(inspect(DOMException.prototype), /^\[object DOMException\] \{/); delete Error[Symbol.hasInstance]; } + +{ + class Class { + get [Symbol.toStringTag]() { + return 'Namespaced.Class'; + } + } + + class DerivedClass extends Class {} + + assert.strictEqual(inspect(new Class()), 'Namespaced.Class {}'); + assert.strictEqual(inspect(new DerivedClass()), 'DerivedClass [Namespaced.Class] {}'); +}