Skip to content

Commit 17a9e47

Browse files
committed
util: fix breakLength: Infinity for deep objects
When `util.inspect` is called with `breakLength: Infinity`, it should always format the output as a single line. However, the existing logic for compact arrays (`groupArrayElements`) and deep objects ignored this when the depth was large or the array was long. This commit updates `groupArrayElements` to return early if `breakLength === Infinity`, preventing column grouping. It also updates `reduceToSingleString` to bypass the depth checks when `breakLength === Infinity`, ensuring all deep objects are consolidated. Fixes: #60475 Signed-off-by: Rohith Pariki <rohithpariki@gmail.com>
1 parent 617e082 commit 17a9e47

2 files changed

Lines changed: 31 additions & 27 deletions

File tree

‎lib/internal/util/inspect.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,9 @@ function formatError(err, constructor, tag, ctx, keys) {
20522052
}
20532053

20542054
function groupArrayElements(ctx, output, value) {
2055+
if (ctx.breakLength === Infinity) {
2056+
return output;
2057+
}
20552058
let totalLength = 0;
20562059
let maxLength = 0;
20572060
let i = 0;
@@ -2681,7 +2684,8 @@ function reduceToSingleString(
26812684
// Consolidate all entries of the local most inner depth up to
26822685
// `ctx.compact`, as long as the properties are smaller than
26832686
// `ctx.breakLength`.
2684-
if (ctx.currentDepth - recurseTimes < ctx.compact &&
2687+
if ((ctx.breakLength === Infinity ||
2688+
ctx.currentDepth - recurseTimes < ctx.compact) &&
26852689
entries === output.length) {
26862690
// Line up all entries on a single line in case the entries do not
26872691
// exceed `breakLength`. Add 10 as constant to start next to all other

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,18 +3053,19 @@ assert.strictEqual(
30533053

30543054
out = util.inspect(obj, { compact: 1, breakLength: Infinity, colors: true });
30553055

3056+
const inlineArray = [
3057+
"\u001b[32m'foobar'\u001b[39m", "\u001b[32m'baz'\u001b[39m",
3058+
"\u001b[32m'foobar'\u001b[39m", "\u001b[32m'baz'\u001b[39m",
3059+
"\u001b[32m'foobar'\u001b[39m", "\u001b[32m'baz'\u001b[39m",
3060+
"\u001b[32m'foobar'\u001b[39m", "\u001b[32m'baz'\u001b[39m",
3061+
"\u001b[32m'foobar'\u001b[39m",
3062+
];
30563063
expected = [
30573064
'{',
30583065
' a: {',
30593066
' b: { x: \u001b[33m5\u001b[39m, c: \u001b[36m[Object]\u001b[39m }',
30603067
' },',
3061-
' b: [',
3062-
" \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,",
3063-
" \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,",
3064-
" \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,",
3065-
" \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,",
3066-
" \u001b[32m'foobar'\u001b[39m",
3067-
' ]',
3068+
` b: [ ${inlineArray.join(', ')} ]`,
30683069
'}',
30693070
].join('\n');
30703071

@@ -3073,25 +3074,8 @@ assert.strictEqual(
30733074
obj = Array.from({ length: 60 }).map((e, i) => i);
30743075
out = util.inspect(obj, { compact: 1, breakLength: Infinity, colors: true });
30753076

3076-
expected = [
3077-
'[',
3078-
' \u001b[33m0\u001b[39m, \u001b[33m1\u001b[39m, \u001b[33m2\u001b[39m, \u001b[33m3\u001b[39m,',
3079-
' \u001b[33m4\u001b[39m, \u001b[33m5\u001b[39m, \u001b[33m6\u001b[39m, \u001b[33m7\u001b[39m,',
3080-
' \u001b[33m8\u001b[39m, \u001b[33m9\u001b[39m, \u001b[33m10\u001b[39m, \u001b[33m11\u001b[39m,',
3081-
' \u001b[33m12\u001b[39m, \u001b[33m13\u001b[39m, \u001b[33m14\u001b[39m, \u001b[33m15\u001b[39m,',
3082-
' \u001b[33m16\u001b[39m, \u001b[33m17\u001b[39m, \u001b[33m18\u001b[39m, \u001b[33m19\u001b[39m,',
3083-
' \u001b[33m20\u001b[39m, \u001b[33m21\u001b[39m, \u001b[33m22\u001b[39m, \u001b[33m23\u001b[39m,',
3084-
' \u001b[33m24\u001b[39m, \u001b[33m25\u001b[39m, \u001b[33m26\u001b[39m, \u001b[33m27\u001b[39m,',
3085-
' \u001b[33m28\u001b[39m, \u001b[33m29\u001b[39m, \u001b[33m30\u001b[39m, \u001b[33m31\u001b[39m,',
3086-
' \u001b[33m32\u001b[39m, \u001b[33m33\u001b[39m, \u001b[33m34\u001b[39m, \u001b[33m35\u001b[39m,',
3087-
' \u001b[33m36\u001b[39m, \u001b[33m37\u001b[39m, \u001b[33m38\u001b[39m, \u001b[33m39\u001b[39m,',
3088-
' \u001b[33m40\u001b[39m, \u001b[33m41\u001b[39m, \u001b[33m42\u001b[39m, \u001b[33m43\u001b[39m,',
3089-
' \u001b[33m44\u001b[39m, \u001b[33m45\u001b[39m, \u001b[33m46\u001b[39m, \u001b[33m47\u001b[39m,',
3090-
' \u001b[33m48\u001b[39m, \u001b[33m49\u001b[39m, \u001b[33m50\u001b[39m, \u001b[33m51\u001b[39m,',
3091-
' \u001b[33m52\u001b[39m, \u001b[33m53\u001b[39m, \u001b[33m54\u001b[39m, \u001b[33m55\u001b[39m,',
3092-
' \u001b[33m56\u001b[39m, \u001b[33m57\u001b[39m, \u001b[33m58\u001b[39m, \u001b[33m59\u001b[39m',
3093-
']',
3094-
].join('\n');
3077+
const inlineNumbers = Array.from({ length: 60 }, (_, i) => `\u001b[33m${i}\u001b[39m`);
3078+
expected = `[ ${inlineNumbers.join(', ')} ]`;
30953079

30963080
assert.strictEqual(out, expected);
30973081

@@ -4095,6 +4079,22 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
40954079
assert.strictEqual(util.inspect(obj, { breakLength: Infinity }), expected);
40964080
}
40974081

4082+
{
4083+
// Refs: https://github.com/nodejs/node/issues/60475
4084+
// `breakLength: Infinity` should format deeply nested objects on a single line
4085+
// regardless of depth limit or array size.
4086+
const obj = { a: { b: { c: { d: { e: 1 } } } } };
4087+
const expected = '{ a: { b: { c: { d: { e: 1 } } } } }';
4088+
assert.strictEqual(util.inspect(obj, { breakLength: Infinity, depth: Infinity }), expected);
4089+
assert.strictEqual(util.inspect(obj, { breakLength: Infinity, depth: null }), expected);
4090+
assert.strictEqual(util.inspect(obj, { breakLength: Infinity, depth: 10 }), expected);
4091+
4092+
const arr = Array.from({ length: 60 }, (_, i) => i);
4093+
const arrExpected = `[ ${arr.join(', ')} ]`;
4094+
assert.strictEqual(util.inspect(arr, { breakLength: Infinity, depth: Infinity }), arrExpected);
4095+
assert.strictEqual(util.inspect(arr, { breakLength: Infinity, depth: 2, compact: 3 }), arrExpected);
4096+
}
4097+
40984098
{
40994099
class Class {
41004100
get [Symbol.toStringTag]() {

0 commit comments

Comments
 (0)