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 <ruben@bridgewater.de>

PR-URL: https://github.com/nodejs/node/pull/41083
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater authored and Danielle Adams committed 2022-01-31 23:54:08 -05:00
1 parent 3185772b66
commit 2c7f17c72c
2 files changed
+23 -2

No files matched your search

+5 -2
View File
@@ -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]}`;
}
}
}
}
+18
View File
@@ -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: {