Skip to content

Commit 50dba4d

Browse files
BridgeARdanielleadams
authored andcommitted
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]> PR-URL: #41083 Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9d55d1c commit 50dba4d

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
@@ -1931,8 +1931,11 @@ function reduceToSingleString(
19311931
const start = output.length + ctx.indentationLvl +
19321932
braces[0].length + base.length + 10;
19331933
if (isBelowBreakLength(ctx, output, start, base)) {
1934-
return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')}` +
1935-
` ${braces[1]}`;
1934+
const joinedOutput = join(output, ', ');
1935+
if (!joinedOutput.includes('\n')) {
1936+
return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
1937+
` ${braces[1]}`;
1938+
}
19361939
}
19371940
}
19381941
}

test/parallel/test-util-inspect.js

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

26322632
assert.strictEqual(out, expected);
26332633

2634+
// Array grouping should prevent lining up outer elements on a single line.
2635+
obj = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]];
2636+
2637+
out = util.inspect(obj, { compact: 3 });
2638+
2639+
expected = [
2640+
'[',
2641+
' [',
2642+
' [',
2643+
' 1, 2, 3, 4, 5,',
2644+
' 6, 7, 8, 9',
2645+
' ]',
2646+
' ]',
2647+
']',
2648+
].join('\n');
2649+
2650+
assert.strictEqual(out, expected);
2651+
26342652
// Verify that array grouping and line consolidation does not happen together.
26352653
obj = {
26362654
a: {

0 commit comments

Comments
 (0)