Skip to content

Commit c3d0976

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 8861722 commit c3d0976

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
@@ -1029,20 +1029,15 @@ function getPrefix(constructor, tag, fallback, size = '') {
10291029
return `[${fallback}${size}: null prototype] `;
10301030
}
10311031

1032-
let result = `${constructor}${size} `;
10331032
if (tag !== '') {
1034-
const position = constructor.indexOf(tag);
1035-
if (position === -1) {
1036-
result += `[${tag}] `;
1037-
} else {
1038-
const endPos = position + tag.length;
1039-
if (endPos !== constructor.length &&
1040-
constructor[endPos] === constructor[endPos].toLowerCase()) {
1041-
result += `[${tag}] `;
1042-
}
1033+
const dot = StringPrototypeLastIndexOf(tag, '.');
1034+
if (constructor === (dot >= 1 ? StringPrototypeSlice(tag, dot + 1) : tag)) {
1035+
return `${tag}${size} `;
10431036
}
1037+
return `${constructor}${size} [${tag}] `;
10441038
}
1045-
return result;
1039+
1040+
return `${constructor}${size} `;
10461041
}
10471042

10481043
// 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)