Skip to content

Commit 68116c3

Browse files
committed
util: honor breakLength Infinity with unlimited depth
Preserve single-line formatting for util.inspect() when breakLength is Infinity and depth is unlimited, while keeping explicit compact settings and customized defaultOptions.compact behavior unchanged. Add regression coverage for depth: Infinity, depth: null, explicit compact: 3, explicit compact: 1, and custom defaultOptions.compact. Fixes: #60475
1 parent 53bcd11 commit 68116c3

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/internal/util/inspect.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2660,7 +2660,10 @@ function reduceToSingleString(
26602660
// Consolidate all entries of the local most inner depth up to
26612661
// `ctx.compact`, as long as the properties are smaller than
26622662
// `ctx.breakLength`.
2663-
if (ctx.currentDepth - recurseTimes < ctx.compact &&
2663+
if (((ctx.breakLength === Infinity &&
2664+
(ctx.depth === Infinity || ctx.depth === null) &&
2665+
ctx.compact === 3) ||
2666+
ctx.currentDepth - recurseTimes < ctx.compact) &&
26642667
entries === output.length) {
26652668
// Line up all entries on a single line in case the entries do not
26662669
// exceed `breakLength`. Add 10 as constant to start next to all other

test/parallel/test-util-inspect.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,36 @@ if (typeof Symbol !== 'undefined') {
16151615
assert.strictEqual(twoLines, "{\n foo: 'abc',\n bar: 'xyz'\n}");
16161616
}
16171617

1618+
{
1619+
const obj = {
1620+
a: {
1621+
b: {
1622+
c: {
1623+
d: 1,
1624+
},
1625+
},
1626+
},
1627+
};
1628+
1629+
assert.strictEqual(
1630+
util.inspect(obj, { breakLength: Infinity, depth: Infinity }),
1631+
'{ a: { b: { c: { d: 1 } } } }',
1632+
);
1633+
assert.strictEqual(
1634+
util.inspect(obj, { breakLength: Infinity, depth: null }),
1635+
'{ a: { b: { c: { d: 1 } } } }',
1636+
);
1637+
assert.strictEqual(
1638+
util.inspect(obj, { breakLength: Infinity, depth: Infinity, compact: 3 }),
1639+
'{ a: { b: { c: { d: 1 } } } }',
1640+
);
1641+
1642+
assert.strictEqual(
1643+
util.inspect(obj, { breakLength: Infinity, depth: Infinity, compact: 1 }),
1644+
'{\n a: {\n b: {\n c: { d: 1 }\n }\n }\n}',
1645+
);
1646+
}
1647+
16181648
// util.inspect.defaultOptions tests.
16191649
{
16201650
const arr = new Array(101).fill();
@@ -1631,6 +1661,12 @@ if (typeof Symbol !== 'undefined') {
16311661
assert.doesNotMatch(util.inspect(obj), /Object/);
16321662
util.inspect.defaultOptions.depth = oldOptions.depth;
16331663
assert.match(util.inspect(obj), /Object/);
1664+
util.inspect.defaultOptions.compact = 1;
1665+
assert.strictEqual(
1666+
util.inspect(obj, { breakLength: Infinity, depth: Infinity }),
1667+
'{\n a: {\n a: {\n a: { a: 1 }\n }\n }\n}',
1668+
);
1669+
util.inspect.defaultOptions.compact = oldOptions.compact;
16341670
assert.strictEqual(
16351671
JSON.stringify(util.inspect.defaultOptions),
16361672
JSON.stringify(oldOptions)

0 commit comments

Comments
 (0)