Skip to content

Commit 5ed8a1c

Browse files
BridgeARtargos
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 <ruben@bridgewater.de> PR-URL: #41083 Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e3a0a9c commit 5ed8a1c

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎lib/internal/util/inspect.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,8 +1931,11 @@ function reduceToSingleString(
19311931
conststart=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+
constjoinedOutput=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‎

Lines changed: 18 additions & 0 deletions
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)