Skip to content

Commit 212f6c6

Browse files
Renegade334aduh95
authored andcommitted
util: canonicalize namespaced tags in inspect()
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk> PR-URL: #63257 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent b6d7ed3 commit 212f6c6

2 files changed

Lines changed: 22 additions & 37 deletions

File tree

‎lib/internal/util/inspect.js‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -778,20 +778,15 @@ function getPrefix(constructor, tag, fallback, size = '') {
778778
return `[${fallback}${size}: null prototype] `;
779779
}
780780

781-
let result = `${constructor}${size} `;
782781
if (tag !== '') {
783-
const position = constructor.indexOf(tag);
784-
if (position === -1) {
785-
result += `[${tag}] `;
786-
} else {
787-
const endPos = position + tag.length;
788-
if (endPos !== constructor.length &&
789-
constructor[endPos] === constructor[endPos].toLowerCase()) {
790-
result += `[${tag}] `;
791-
}
782+
const dot = StringPrototypeLastIndexOf(tag, '.');
783+
if (constructor === (dot >= 1 ? StringPrototypeSlice(tag, dot + 1) : tag)) {
784+
return `${tag}${size} `;
792785
}
786+
return `${constructor}${size} [${tag}] `;
793787
}
794-
return result;
788+
789+
return `${constructor}${size} `;
795790
}
796791

797792
// Look up the keys of the object.

‎test/parallel/test-util-inspect.js‎

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,11 +1475,11 @@ if (typeof Symbol !== 'undefined') {
14751475
assert.strictEqual(util.inspect(new ArraySubclass(1, 2, 3)),
14761476
'ArraySubclass(3) [ 1, 2, 3 ]');
14771477
assert.strictEqual(util.inspect(new SetSubclass([1, 2, 3])),
1478-
'SetSubclass(3) { 1, 2, 3 }');
1478+
'SetSubclass(3) [Set] { 1, 2, 3 }');
14791479
assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])),
1480-
"MapSubclass(1) { 'foo' => 42 }");
1480+
"MapSubclass(1) [Map] { 'foo' => 42 }");
14811481
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
1482-
'PromiseSubclass { <pending> }');
1482+
'PromiseSubclass [Promise] { <pending> }');
14831483
assert.strictEqual(util.inspect(new SymbolNameClass()),
14841484
'Symbol(name) {}');
14851485
assert.strictEqual(
@@ -1490,29 +1490,6 @@ if (typeof Symbol !== 'undefined') {
14901490
util.inspect(Object.setPrototypeOf(x, null)),
14911491
'[ObjectSubclass: null prototype] { foo: 42 }'
14921492
);
1493-
1494-
class MiddleErrorPart extends Error {}
1495-
assert(util.inspect(new MiddleErrorPart('foo')).includes('MiddleErrorPart: foo'));
1496-
1497-
class MapClass extends Map {}
1498-
assert.strictEqual(util.inspect(new MapClass([['key', 'value']])),
1499-
"MapClass(1) { 'key' => 'value' }");
1500-
1501-
class AbcMap extends Map {}
1502-
assert.strictEqual(util.inspect(new AbcMap([['key', 'value']])),
1503-
"AbcMap(1) { 'key' => 'value' }");
1504-
1505-
class SetAbc extends Set {}
1506-
assert.strictEqual(util.inspect(new SetAbc([1, 2, 3])),
1507-
'SetAbc(3) { 1, 2, 3 }');
1508-
1509-
class FooSet extends Set {}
1510-
assert.strictEqual(util.inspect(new FooSet([1, 2, 3])),
1511-
'FooSet(3) { 1, 2, 3 }');
1512-
1513-
class Settings extends Set {}
1514-
assert.strictEqual(util.inspect(new Settings([1, 2, 3])),
1515-
'Settings(3) [Set] { 1, 2, 3 }');
15161493
}
15171494

15181495
// Empty and circular before depth.
@@ -4070,3 +4047,16 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
40704047
const expected = "{ a: 'short string', b: [ 1, 2 ], c: { d: true } }";
40714048
assert.strictEqual(util.inspect(obj, { breakLength: Infinity }), expected);
40724049
}
4050+
4051+
{
4052+
class Class {
4053+
get [Symbol.toStringTag]() {
4054+
return 'Namespaced.Class';
4055+
}
4056+
}
4057+
4058+
class DerivedClass extends Class {}
4059+
4060+
assert.strictEqual(inspect(new Class()), 'Namespaced.Class {}');
4061+
assert.strictEqual(inspect(new DerivedClass()), 'DerivedClass [Namespaced.Class] {}');
4062+
}

0 commit comments

Comments
 (0)