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: improve util inspect output #26984

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
16 changes: 9 additions & 7 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,26 +848,27 @@ function groupArrayElements(ctx, output) {
let totalLength = 0;
let maxLength = 0;
let i = 0;
const separatorSpace = 2; // Add 1 for the space and 1 for the separator.
const dataLen = new Array(output.length);
// Calculate the total length of all output entries and the individual max
// entries length of all output entries. We have to remove colors first,
// otherwise the length would not be calculated properly.
for (; i < output.length; i++) {
const len = ctx.colors ? removeColors(output[i]).length : output[i].length;
dataLen[i] = len;
totalLength += len;
totalLength += len + separatorSpace;
if (maxLength < len)
maxLength = len;
}
// Add two to `maxLength` as we add a single whitespace character plus a comma
// in-between two entries.
const actualMax = maxLength + 2;
const actualMax = maxLength + separatorSpace;
// Check if at least three entries fit next to each other and prevent grouping
// of arrays that contains entries of very different length (i.e., if a single
// entry is longer than 1/5 of all other entries combined). Otherwise the
// space in-between small entries would be enormous.
if (actualMax * 3 + ctx.indentationLvl < ctx.breakLength &&
(totalLength / maxLength > 5 || maxLength <= 6)) {
(totalLength / actualMax > 5 || maxLength <= 6)) {

const approxCharHeights = 2.5;
const bias = 1;
Expand Down Expand Up @@ -1314,7 +1315,7 @@ function formatProperty(ctx, value, recurseTimes, key, type) {
return `${name}:${extra}${str}`;
}

function isBelowBreakLength(ctx, output, start) {
function isBelowBreakLength(ctx, output, start, base) {
// Each entry is separated by at least a comma. Thus, we start with a total
// length of at least `output.length`. In addition, some cases have a
// whitespace in-between each other that is added to the total as well.
Expand All @@ -1331,7 +1332,8 @@ function isBelowBreakLength(ctx, output, start) {
return false;
}
}
return true;
// Do not line up properties on the same line if `base` contains line breaks.
return base === '' || !base.includes('\n');
}

function reduceToSingleString(ctx, output, base, braces, combine = false) {
Expand All @@ -1342,7 +1344,7 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
// that may reduce `breakLength`.
const start = output.length + ctx.indentationLvl +
braces[0].length + base.length + 10;
if (isBelowBreakLength(ctx, output, start)) {
if (isBelowBreakLength(ctx, output, start, base)) {
return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')} ` +
braces[1];
}
Expand All @@ -1354,7 +1356,7 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
}
// Line up all entries on a single line in case the entries do not exceed
// `breakLength`.
if (isBelowBreakLength(ctx, output, 0)) {
if (isBelowBreakLength(ctx, output, 0, base)) {
return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` +
braces[1];
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-repl-pretty-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const tests = [
{
command: '(() => { const err = Error(\'Whoops!\'); ' +
'err.foo = \'bar\'; throw err; })()',
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: \'bar\' }\n',
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: \'bar\' }\n',
},
{
command: '(() => { const err = Error(\'Whoops!\'); ' +
'err.foo = \'bar\'; throw err; })()',
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: ' +
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: ' +
"\u001b[32m'bar'\u001b[39m }\n",
useColors: true
},
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ const errorTests = [
/^ at .*/,
/^ at .*/,
/^ at .*/,
/^ at .*/
/^ at .*/,
" code: 'MODULE_NOT_FOUND',",
" requireStack: [ '<repl>' ] }"
]
},
// REPL should handle quotes within regexp literal in multiline mode
Expand Down
26 changes: 25 additions & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
);
assert.strictEqual(
util.inspect(err2, { compact: true }),
'{ [Error: foo\nbar] bar: true }'
'{ [Error: foo\nbar]\n bar: true }'
);
assert.strictEqual(
util.inspect(err, { compact: true, breakLength: 5 }),
Expand Down Expand Up @@ -2099,6 +2099,30 @@ assert.strictEqual(

assert.strictEqual(out, expected);

obj = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 123456789
];

out = util.inspect(obj, { compact: 3 });

expected = [
'[',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 123456789',
']'
].join('\n');

assert.strictEqual(out, expected);

// Verify that array grouping and line consolidation does not happen together.
obj = {
a: {
Expand Down