Skip to content

Commit 3bf776c

Browse files
util: allow single-line format when break length is infinite
Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
1 parent c7ddda9 commit 3bf776c

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/internal/util/inspect.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,14 @@ function isBelowBreakLength(ctx, output, start, base) {
26242624
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
26252625
// function. Check the performance overhead and make it an opt-in in case it's
26262626
// significant.
2627+
// allow the single-line format if the length limit is infinite and no items have newlines
2628+
if (ctx.breakLength === Infinity) {
2629+
if (base !== '' && StringPrototypeIncludes(base, '\n')) return false;
2630+
for (let i = 0; i < output.length; i++) {
2631+
if (typeof output[i] === 'string' && StringPrototypeIncludes(output[i], '\n')) return false;
2632+
}
2633+
return true;
2634+
}
26272635
let totalLength = output.length + start;
26282636
if (totalLength + output.length > ctx.breakLength)
26292637
return false;

test/parallel/test-util-inspect.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,3 +4058,15 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
40584058
assert.match(inspect(DOMException.prototype), /^\[object DOMException\] \{/);
40594059
delete Error[Symbol.hasInstance];
40604060
}
4061+
4062+
{
4063+
// Ensure breakLength: Infinity formats deeply nested objects on a single line
4064+
// while properly preserving multi-line outer formatting if inner items have newlines.
4065+
const nestedObj = { a: { b: { c: [1, 2, 'long string '.repeat(50)] } } };
4066+
const singleLine = util.inspect(nestedObj, { breakLength: Infinity });
4067+
assert.ok(!singleLine.includes('\n'));
4068+
4069+
const multilineObj = { first: 'hello\nworld', second: 'foo' };
4070+
const multiLine = util.inspect(multilineObj, { breakLength: Infinity });
4071+
assert.ok(multiLine.startsWith('{\n'));
4072+
}

0 commit comments

Comments
 (0)