From 5ed8a1c017f3b9c7df503c41542b387a484b255a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 4 Dec 2021 06:05:05 +0100 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/41083 Reviewed-By: Anto Aravinth Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 7 +++++-- test/parallel/test-util-inspect.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 563ab00262a356..ceaf8e133788ca 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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]}`; + } } } } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 65736cd62a8111..34cc5cc56af48b 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -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: {