Skip to content

Commit

Permalink
util: do not reduce to a single line if not appropriate using inspect
Browse files Browse the repository at this point in the history
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]>

PR-URL: #41083
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed Jan 14, 2022
1 parent e3a0a9c commit 5ed8a1c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1931,8 +1931,11 @@ function reduceToSingleString(
const start = output.length + ctx.indentationLvl +
braces[0].length + base.length + 10;
if (isBelowBreakLength(ctx, output, start, base)) {
return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')}` +
` ${braces[1]}`;
const joinedOutput = join(output, ', ');
if (!joinedOutput.includes('\n')) {
return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
` ${braces[1]}`;
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,24 @@ assert.strictEqual(

assert.strictEqual(out, expected);

// Array grouping should prevent lining up outer elements on a single line.
obj = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]];

out = util.inspect(obj, { compact: 3 });

expected = [
'[',
' [',
' [',
' 1, 2, 3, 4, 5,',
' 6, 7, 8, 9',
' ]',
' ]',
']',
].join('\n');

assert.strictEqual(out, expected);

// Verify that array grouping and line consolidation does not happen together.
obj = {
a: {
Expand Down

0 comments on commit 5ed8a1c

Please sign in to comment.