Commit 6eb6ebd
fix(util): quote top-level strings in util.format %o/%O (Node parity) (#5106)
`util.format("%o", "hello")` / `console.log("%o", str)` printed the
string unquoted (`hello`) where Node renders it through `util.inspect`
(`'hello'`). The `%o`/`%O` arms called `format_jsvalue(val, 0)`, which
mirrors the bare `console.log(str)` behavior of leaving a *top-level*
string unquoted — but `%o`/`%O` should inspect the value, and inspect
quotes strings at every depth (nested strings already quoted correctly,
e.g. `%o` of `{a:"x"}` → `{ a: 'x' }`).
Route the `%o`/`%O` argument through a new `inspect_format_arg` helper
that mirrors `js_util_inspect`'s top-level handling: a primitive string
becomes `'<escaped>'`; everything else goes through `format_jsvalue`.
Verified vs Node v26.3.0: `%o`/`%O` of a string now yield `'hello'`;
numbers/objects/arrays/null/bool unchanged; `%s` still coerces without
quotes. Existing formatting/inspect unit tests pass (12).
(Out of scope: Node's `%o` also sets `showHidden`, so an array prints
`[ 1, 'two', [length]: 2 ]`; matching the `[length]` internal-slot
rendering is a separate showHidden-array feature and left as-is.)
Co-authored-by: Ralph Küpper <ralph2@skelpo.com>1 parent 088afba commit 6eb6ebd
1 file changed
Lines changed: 18 additions & 2 deletions
Lines changed: 18 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
115 | 131 | | |
116 | 132 | | |
117 | 133 | | |
| |||
324 | 340 | | |
325 | 341 | | |
326 | 342 | | |
327 | | - | |
| 343 | + | |
328 | 344 | | |
329 | 345 | | |
330 | 346 | | |
331 | 347 | | |
332 | | - | |
| 348 | + | |
333 | 349 | | |
334 | 350 | | |
335 | 351 | | |
| |||
0 commit comments