Skip to content
Open
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
13 changes: 11 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,14 @@ function formatError(err, constructor, tag, ctx, keys) {
return stack;
}

function isUnlimitedDepth(ctx) {
return ctx.depth === null || ctx.depth === Infinity;
}

function groupArrayElements(ctx, output, value) {
if (!NumberIsFinite(ctx.breakLength) && isUnlimitedDepth(ctx)) {
return output;
}
let totalLength = 0;
let maxLength = 0;
let i = 0;
Expand Down Expand Up @@ -2640,8 +2647,10 @@ function reduceToSingleString(
// Consolidate all entries of the local most inner depth up to
// `ctx.compact`, as long as the properties are smaller than
// `ctx.breakLength`.
if (ctx.currentDepth - recurseTimes < ctx.compact &&
entries === output.length) {
const allowInline =
(!NumberIsFinite(ctx.breakLength) && isUnlimitedDepth(ctx)) ||
ctx.currentDepth - recurseTimes < ctx.compact;
if (allowInline && entries === output.length) {
// Line up all entries on a single line in case the entries do not
// exceed `breakLength`. Add 10 as constant to start next to all other
// factors that may reduce `breakLength`.
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,25 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
}), "[ 'foo', <1 empty item>, ... 99 more items ]");
}

// `breakLength: Infinity` should force single-line output even if `depth` is
// explicitly set to `Infinity`.
{
const tree = {};
for (let i = 0; i < 5; i++) {
const branch = tree[`field${i}`] = { leaves: {} };
for (let j = 0; j < 5; j++) {
branch.leaves[`leaf${j}`] = new Array(10).fill(`value-${i}-${j}`);
}
}

const options = { breakLength: Infinity, compact: 3 };
const singleLine = util.inspect(tree, options);
assert.strictEqual(singleLine.includes('\n'), false);

const deepOptions = { ...options, depth: Infinity };
assert.strictEqual(util.inspect(tree, deepOptions).includes('\n'), false);
}

// Test for Array constructor in different context.
{
const map = new Map();
Expand Down
Loading