Skip to content

Commit 37526a5

Browse files
committed
util: change inspect's default depth from 2 to 4
When dealing with somewhat deep objects, the default depath of 2 is often a hindrance when using the console to log said objects. This changes the default depth to a more reasonable 4. PR-URL: #23062
1 parent a5b92a7 commit 37526a5

4 files changed

Lines changed: 32 additions & 24 deletions

File tree

doc/api/util.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ stream.write('With ES6');
360360
<!-- YAML
361361
added: v0.3.0
362362
changes:
363+
- version: REPLACEME
364+
pr-url: https://github.com/nodejs/node/pull/23062
365+
description: The `depth` option now defaults to `4` instead of `2`.
363366
- version: REPLACEME
364367
pr-url: https://github.com/nodejs/node/pull/22788
365368
description: The `sorted` option is supported now.
@@ -401,7 +404,7 @@ changes:
401404
* `depth` {number} Specifies the number of times to recurse while formatting
402405
the `object`. This is useful for inspecting large complicated objects. To
403406
make it recurse up to the maximum call stack size pass `Infinity` or `null`.
404-
**Default:** `2`.
407+
**Default:** `4`.
405408
* `colors` {boolean} If `true`, the output will be styled with ANSI color
406409
codes. Colors are customizable, see [Customizing `util.inspect` colors][].
407410
**Default:** `false`.

lib/internal/util/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);
9494

9595
const inspectDefaultOptions = Object.seal({
9696
showHidden: false,
97-
depth: 2,
97+
depth: 4,
9898
colors: false,
9999
customInspect: true,
100100
showProxy: false,

test/parallel/test-util-inspect-proxy.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ const expected1 = 'Proxy [ {}, {} ]';
5050
const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
5151
const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
5252
const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
53-
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], {} ],' +
53+
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ],' +
5454
' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ]' +
55-
', Proxy [ Proxy [Array], {} ] ] ]';
56-
const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], Proxy [Array]' +
57-
' ],\n Proxy [ Proxy [Array], Proxy [Array] ] ],\n' +
58-
' Proxy [ Proxy [ Proxy [Array], Proxy [Array] ],\n' +
59-
' Proxy [ Proxy [Array], Proxy [Array] ] ] ]';
55+
', Proxy [ Proxy [ {}, {} ], {} ] ] ]';
56+
const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ],' +
57+
' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ], Proxy' +
58+
' [ Proxy [ {}, {} ], {} ] ] ],\n Proxy [ Proxy [ Proxy' +
59+
' [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ],\n Proxy' +
60+
' [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ] ] ]';
6061
assert.strictEqual(
6162
util.inspect(proxy1, { showProxy: true, depth: null }),
6263
expected1);

test/parallel/test-util-inspect.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ assert.strictEqual(util.inspect({ a: function*() {} }),
7070
assert.strictEqual(util.inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
7171
assert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }');
7272
assert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }');
73-
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
74-
'{ a: { b: { c: [Object] } } }');
73+
assert.strictEqual(util.inspect(
74+
{ 'a': { 'b': { 'c': { 'd': { 'e': { 'f': {} } } } } } }),
75+
'{ a: { b: { c: { d: { e: [Object] } } } } }'
76+
);
7577
assert.strictEqual(
7678
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
7779
'{ a: { b: { c: { d: 2 } } } }');
@@ -111,10 +113,12 @@ assert.strictEqual(util.inspect((new JSStream())._externalStream),
111113
}
112114

113115
assert(/Object/.test(
114-
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true)
116+
util.inspect({ a: { a: { a: { a: { a: { a: {} } } } } } },
117+
undefined, undefined, true)
115118
));
116119
assert(!/Object/.test(
117-
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, null, true)
120+
util.inspect({ a: { a: { a: { a: { a: { a: {} } } } } } },
121+
undefined, null, true)
118122
));
119123

120124
for (const showHidden of [true, false]) {
@@ -1048,16 +1052,16 @@ if (typeof Symbol !== 'undefined') {
10481052

10491053
// Empty and circular before depth.
10501054
{
1051-
const arr = [[[[]]]];
1052-
assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]');
1053-
arr[0][0][0][0] = [];
1054-
assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]');
1055-
arr[0][0][0] = {};
1056-
assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]');
1057-
arr[0][0][0] = { a: 2 };
1058-
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
1059-
arr[0][0][0] = arr;
1060-
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
1055+
const arr = [[[[[[]]]]]];
1056+
assert.strictEqual(util.inspect(arr), '[ [ [ [ [ [] ] ] ] ] ]');
1057+
arr[0][0][0][0][0][0] = [];
1058+
assert.strictEqual(util.inspect(arr), '[ [ [ [ [ [Array] ] ] ] ] ]');
1059+
arr[0][0][0][0][0] = {};
1060+
assert.strictEqual(util.inspect(arr), '[ [ [ [ [ {} ] ] ] ] ]');
1061+
arr[0][0][0][0][0] = { a: 2 };
1062+
assert.strictEqual(util.inspect(arr), '[ [ [ [ [ [Object] ] ] ] ] ]');
1063+
arr[0][0][0][0][0] = arr;
1064+
assert.strictEqual(util.inspect(arr), '[ [ [ [ [ [Circular] ] ] ] ] ]');
10611065
}
10621066

10631067
// Corner cases.
@@ -1145,7 +1149,7 @@ if (typeof Symbol !== 'undefined') {
11451149
// util.inspect.defaultOptions tests.
11461150
{
11471151
const arr = new Array(101).fill();
1148-
const obj = { a: { a: { a: { a: 1 } } } };
1152+
const obj = { a: { a: { a: { a: { a: { a: 1 } } } } } };
11491153

11501154
const oldOptions = Object.assign({}, util.inspect.defaultOptions);
11511155

@@ -1559,7 +1563,7 @@ util.inspect(process);
15591563
head = head.next = {};
15601564
assert.strictEqual(
15611565
util.inspect(list),
1562-
'{ next: { next: { next: [Object] } } }'
1566+
'{ next: { next: { next: { next: { next: [Object] } } } } }'
15631567
);
15641568
const longList = util.inspect(list, { depth: Infinity });
15651569
const match = longList.match(/next/g);

0 commit comments

Comments
 (0)