Skip to content

Commit 10b4a81

Browse files
BridgeARtargos
authored andcommitted
util: if present, fallback to toString using the %s formatter
This makes sure that `util.format` uses `String` to stringify an object in case the object has an own property named `toString` with type `function`. That way objects that do not have such function are still inspected using `util.inspect` and the old behavior is preserved as well. PR-URL: #27621 Refs: jestjs/jest#8443 Reviewed-By: Roman Reiss <[email protected]>
1 parent 378f44c commit 10b4a81

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

doc/api/util.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ specifiers. Each specifier is replaced with the converted value from the
221221
corresponding argument. Supported specifiers are:
222222

223223
* `%s` - `String` will be used to convert all values except `BigInt`, `Object`
224-
and `-0`. `BigInt` values will be represented with an `n` and Objects are
225-
inspected using `util.inspect()` with options
226-
`{ depth: 0, colors: false, compact: 3 }`.
224+
and `-0`. `BigInt` values will be represented with an `n` and Objects that
225+
have no user defined `toString` function are inspected using `util.inspect()`
226+
with options `{ depth: 0, colors: false, compact: 3 }`.
227227
* `%d` - `Number` will be used to convert all values except `BigInt` and
228228
`Symbol`.
229229
* `%i` - `parseInt(value, 10)` is used for all values except `BigInt` and

lib/internal/util/inspect.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ const { NativeModule } = require('internal/bootstrap/loaders');
9393

9494
let hexSlice;
9595

96+
const builtInObjects = new Set(
97+
Object.getOwnPropertyNames(global).filter((e) => /^([A-Z][a-z]+)+$/.test(e))
98+
);
99+
96100
const inspectDefaultOptions = Object.seal({
97101
showHidden: false,
98102
depth: 2,
@@ -1539,16 +1543,33 @@ function formatWithOptions(inspectOptions, ...args) {
15391543
switch (nextChar) {
15401544
case 115: // 's'
15411545
const tempArg = args[++a];
1542-
if (typeof tempArg !== 'string' &&
1543-
typeof tempArg !== 'function') {
1544-
tempStr = inspect(tempArg, {
1545-
...inspectOptions,
1546-
compact: 3,
1547-
colors: false,
1548-
depth: 0
1549-
});
1546+
if (typeof tempArg === 'number') {
1547+
tempStr = formatNumber(stylizeNoColor, tempArg);
1548+
// eslint-disable-next-line valid-typeof
1549+
} else if (typeof tempArg === 'bigint') {
1550+
tempStr = `${tempArg}n`;
15501551
} else {
1551-
tempStr = String(tempArg);
1552+
let constr;
1553+
if (typeof tempArg !== 'object' ||
1554+
tempArg === null ||
1555+
typeof tempArg.toString === 'function' &&
1556+
// A direct own property.
1557+
(hasOwnProperty(tempArg, 'toString') ||
1558+
// A direct own property on the constructor prototype in
1559+
// case the constructor is not an built-in object.
1560+
(constr = tempArg.constructor) &&
1561+
!builtInObjects.has(constr.name) &&
1562+
constr.prototype &&
1563+
hasOwnProperty(constr.prototype, 'toString'))) {
1564+
tempStr = String(tempArg);
1565+
} else {
1566+
tempStr = inspect(tempArg, {
1567+
...inspectOptions,
1568+
compact: 3,
1569+
colors: false,
1570+
depth: 0
1571+
});
1572+
}
15521573
}
15531574
break;
15541575
case 106: // 'j'

test/parallel/test-util-format.js

+22
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,30 @@ assert.strictEqual(util.format('%s', 42n), '42n');
138138
assert.strictEqual(util.format('%s', Symbol('foo')), 'Symbol(foo)');
139139
assert.strictEqual(util.format('%s', true), 'true');
140140
assert.strictEqual(util.format('%s', { a: [1, 2, 3] }), '{ a: [Array] }');
141+
assert.strictEqual(util.format('%s', { toString() { return 'Foo'; } }), 'Foo');
142+
assert.strictEqual(util.format('%s', { toString: 5 }), '{ toString: 5 }');
141143
assert.strictEqual(util.format('%s', () => 5), '() => 5');
142144

145+
// String format specifier including `toString` properties on the prototype.
146+
{
147+
class Foo { toString() { return 'Bar'; } }
148+
assert.strictEqual(util.format('%s', new Foo()), 'Bar');
149+
assert.strictEqual(
150+
util.format('%s', Object.setPrototypeOf(new Foo(), null)),
151+
'[Foo: null prototype] {}'
152+
);
153+
global.Foo = Foo;
154+
assert.strictEqual(util.format('%s', new Foo()), 'Bar');
155+
delete global.Foo;
156+
class Bar { abc = true; }
157+
assert.strictEqual(util.format('%s', new Bar()), 'Bar { abc: true }');
158+
class Foobar extends Array { aaa = true; }
159+
assert.strictEqual(
160+
util.format('%s', new Foobar(5)),
161+
'Foobar [ <5 empty items>, aaa: true ]'
162+
);
163+
}
164+
143165
// JSON format specifier
144166
assert.strictEqual(util.format('%j'), '%j');
145167
assert.strictEqual(util.format('%j', 42), '42');

0 commit comments

Comments
 (0)