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]>
  • Loading branch information
BridgeAR committed Dec 4, 2021
1 parent ea65e1f commit 4aa78fe
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 @@ -1913,8 +1913,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 @@ -2572,6 +2572,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 4aa78fe

Please sign in to comment.