Skip to content

Commit

Permalink
inspect: Correct %s format behavior with Symbol.toPrimitive
Browse files Browse the repository at this point in the history
Ensure console.log("%s", obj) correctly invokes obj[Symbol.toPrimitive]
for string conversion, fixing unexpected object display issue.

Fixes: nodejs#50909
  • Loading branch information
Chenyu Yang committed Mar 18, 2024
1 parent 454d080 commit baf1b8c
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const {
const assert = require('internal/assert');

const { BuiltinModule } = require('internal/bootstrap/realm');
const { SymbolToPrimitive } = primordials;
const {
validateObject,
validateString,
Expand Down Expand Up @@ -2204,6 +2205,10 @@ function formatWithOptionsInternal(inspectOptions, args) {
tempArg === null ||
!hasBuiltInToString(tempArg)) {
tempStr = String(tempArg);
} else if (hasBuiltInToString(tempArg) &&
getProxyDetails(tempArg, false) === undefined &&
tempArg[SymbolToPrimitive] !== undefined) {
tempStr = tempArg[SymbolToPrimitive]('string');
} else {
tempStr = inspect(tempArg, {
...inspectOptions,
Expand Down

0 comments on commit baf1b8c

Please sign in to comment.