Skip to content

Commit 7603a51

Browse files
Chenyu Yangmarco-ippolito
authored andcommitted
util: fix %s format behavior with Symbol.toPrimitive
This commit ensures `console.log("%s", obj)` correctly invokes `obj[Symbol.toPrimitive]` for string conversion, fixing unexpected object display issue. PR-URL: #50992Fixes: #50909 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zeyu "Alex" Yang <[email protected]>
1 parent 800b6f6 commit 7603a51

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎lib/internal/util/inspect.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const{
8787
SymbolPrototypeToString,
8888
SymbolPrototypeValueOf,
8989
SymbolIterator,
90+
SymbolToPrimitive,
9091
SymbolToStringTag,
9192
TypedArrayPrototypeGetLength,
9293
TypedArrayPrototypeGetSymbolToStringTag,
@@ -2103,6 +2104,11 @@ function hasBuiltInToString(value){
21032104
value=proxyTarget;
21042105
}
21052106

2107+
// Check if value has a custom Symbol.toPrimitive transformation.
2108+
if(typeofvalue[SymbolToPrimitive]==='function'){
2109+
returnfalse;
2110+
}
2111+
21062112
// Count objects that have no `toString` function as built-in.
21072113
if(typeofvalue.toString!=='function'){
21082114
returntrue;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ assert.strictEqual(util.format('%s', -Infinity), '-Infinity');
269269
);
270270
}
271271

272+
// Symbol.toPrimitive handling for string format specifier
273+
{
274+
constobjectWithToPrimitive={
275+
[Symbol.toPrimitive](hint){
276+
switch(hint){
277+
case'number':
278+
return42;
279+
case'string':
280+
return'string representation';
281+
case'default':
282+
default:
283+
return'default context';
284+
}
285+
}
286+
};
287+
288+
assert.strictEqual(util.format('%s',+objectWithToPrimitive),'42');
289+
assert.strictEqual(util.format('%s',objectWithToPrimitive),'string representation');
290+
assert.strictEqual(util.format('%s',objectWithToPrimitive+''),'default context');
291+
}
292+
272293
// JSON format specifier
273294
assert.strictEqual(util.format('%j'),'%j');
274295
assert.strictEqual(util.format('%j',42),'42');

0 commit comments

Comments
(0)