Skip to content

Commit 4aa78fe

Browse files
committed
util: do not reduce to a single line if not appropriate using inspect
This makes sure entries are not lined up on a single line if the content contains any new line. That would otherwise cause confusing output. Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent ea65e1f commit 4aa78fe

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/internal/util/inspect.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1913,8 +1913,11 @@ function reduceToSingleString(
19131913
const start = output.length + ctx.indentationLvl +
19141914
braces[0].length + base.length + 10;
19151915
if (isBelowBreakLength(ctx, output, start, base)) {
1916-
return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')}` +
1917-
` ${braces[1]}`;
1916+
const joinedOutput = join(output, ', ');
1917+
if (!joinedOutput.includes('\n')) {
1918+
return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
1919+
` ${braces[1]}`;
1920+
}
19181921
}
19191922
}
19201923
}

test/parallel/test-util-inspect.js

+18
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,24 @@ assert.strictEqual(
25722572

25732573
assert.strictEqual(out, expected);
25742574

2575+
// Array grouping should prevent lining up outer elements on a single line.
2576+
obj = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]];
2577+
2578+
out = util.inspect(obj, { compact: 3 });
2579+
2580+
expected = [
2581+
'[',
2582+
' [',
2583+
' [',
2584+
' 1, 2, 3, 4, 5,',
2585+
' 6, 7, 8, 9',
2586+
' ]',
2587+
' ]',
2588+
']',
2589+
].join('\n');
2590+
2591+
assert.strictEqual(out, expected);
2592+
25752593
// Verify that array grouping and line consolidation does not happen together.
25762594
obj = {
25772595
a: {

0 commit comments

Comments
 (0)