Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: do not reduce to a single line if not appropriate using inspect #41083

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
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