Skip to content

Commit ac7450a

Browse files
committedOct 2, 2018
util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is changed to inspect objects to a maximum depth of 20 in case util.inspect is called with it's defaults. The default is kept at 2 when using console.log() and similar in the repl. PR-URL: #17907 Refs: #12693 PR-URL: #22846 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent 83d0404 commit ac7450a

File tree

6 files changed

+62
-27
lines changed

6 files changed

+62
-27
lines changed
 

‎doc/api/util.md

+19-5
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/22846
365+
description: The `depth` default changed to `20`.
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:** `20`.
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`.
@@ -458,12 +461,23 @@ util.inspect(new Bar()); // 'Bar {}'
458461
util.inspect(baz); // '[foo] {}'
459462
```
460463

461-
The following example inspects all properties of the `util` object:
464+
The following example limits the inspected output of the `paths` property:
462465

463466
```js
464467
const util = require('util');
465468

466-
console.log(util.inspect(util, { showHidden: true, depth: null }));
469+
console.log(util.inspect(module, { depth: 0 }));
470+
// Instead of showing all entries in `paths` `[Array]` is used to limit the
471+
// output for readability:
472+
473+
// Module {
474+
// id: '<repl>',
475+
// exports: {},
476+
// parent: undefined,
477+
// filename: null,
478+
// loaded: false,
479+
// children: [],
480+
// paths: [Array] }
467481
```
468482

469483
The following example highlights the difference with the `compact` option:
@@ -479,7 +493,7 @@ const o = {
479493
'foo']], 4],
480494
b: new Map([['za', 1], ['zb', 'test']])
481495
};
482-
console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
496+
console.log(util.inspect(o, { compact: true, breakLength: 80 }));
483497

484498
// This will print
485499

@@ -493,7 +507,7 @@ console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
493507
// b: Map { 'za' => 1, 'zb' => 'test' } }
494508

495509
// Setting `compact` to false changes the output to be more reader friendly.
496-
console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
510+
console.log(util.inspect(o, { compact: false, breakLength: 80 }));
497511

498512
// {
499513
// a: [

‎lib/internal/util/inspect.js

+1-1
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: 20,
9898
colors: false,
9999
customInspect: true,
100100
showProxy: false,

‎lib/repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ function hasOwnProperty(obj, prop) {
128128
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
129129
// This is the default "writer" value if none is passed in the REPL options.
130130
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
131-
writer.options =
132-
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
131+
writer.options = Object.assign({},
132+
util.inspect.defaultOptions,
133+
{ showProxy: true, depth: 2 });
133134

134135
exports._builtinLibs = builtinLibs;
135136

‎test/parallel/test-stream-buffer-list.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require('../common');
44
const assert = require('assert');
55
const BufferList = require('internal/streams/buffer_list');
6+
const util = require('util');
67

78
// Test empty buffer list.
89
const emptyList = new BufferList();
@@ -30,3 +31,10 @@ assert.strictEqual(list.join(','), 'foo');
3031
const shifted = list.shift();
3132
assert.strictEqual(shifted, buf);
3233
assert.deepStrictEqual(list, new BufferList());
34+
35+
const tmp = util.inspect.defaultOptions.colors;
36+
util.inspect.defaultOptions = { colors: true };
37+
assert.strictEqual(
38+
util.inspect(list),
39+
'BufferList { length: \u001b[33m0\u001b[39m }');
40+
util.inspect.defaultOptions = { colors: tmp };

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ 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' +
58+
' Proxy [ Proxy [ {}, {} ], ' +
59+
'Proxy [ Proxy [ {}, {} ], {} ] ] ],\n' +
60+
' Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ], ' +
61+
'Proxy [ {}, {} ] ],\n' +
62+
' Proxy [ Proxy [ {}, {} ], ' +
63+
'Proxy [ Proxy [ {}, {} ], {} ] ] ] ]';
6064
assert.strictEqual(
6165
util.inspect(proxy1, { showProxy: true, depth: null }),
6266
expected1);

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

+21-13
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ 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 } }');
7373
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
74-
'{ a: { b: { c: [Object] } } }');
74+
'{ a: { b: { c: { d: 2 } } } }');
7575
assert.strictEqual(
7676
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
7777
'{ a: { b: { c: { d: 2 } } } }');
@@ -110,7 +110,7 @@ assert.strictEqual(util.inspect((new JSStream())._externalStream),
110110
assert.strictEqual(util.inspect({ a: regexp }, false, 0), '{ a: /regexp/ }');
111111
}
112112

113-
assert(/Object/.test(
113+
assert(!/Object/.test(
114114
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true)
115115
));
116116
assert(!/Object/.test(
@@ -1055,15 +1055,15 @@ if (typeof Symbol !== 'undefined') {
10551055
// Empty and circular before depth.
10561056
{
10571057
const arr = [[[[]]]];
1058-
assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]');
1058+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [] ] ] ]');
10591059
arr[0][0][0][0] = [];
1060-
assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]');
1060+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Array] ] ] ]');
10611061
arr[0][0][0] = {};
1062-
assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]');
1062+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ {} ] ] ]');
10631063
arr[0][0][0] = { a: 2 };
1064-
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
1064+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Object] ] ] ]');
10651065
arr[0][0][0] = arr;
1066-
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
1066+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Circular] ] ] ]');
10671067
}
10681068

10691069
// Corner cases.
@@ -1160,10 +1160,10 @@ if (typeof Symbol !== 'undefined') {
11601160
assert(!/1 more item/.test(util.inspect(arr)));
11611161
util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength;
11621162
assert(/1 more item/.test(util.inspect(arr)));
1163-
util.inspect.defaultOptions.depth = null;
1164-
assert(!/Object/.test(util.inspect(obj)));
1165-
util.inspect.defaultOptions.depth = oldOptions.depth;
1163+
util.inspect.defaultOptions.depth = 2;
11661164
assert(/Object/.test(util.inspect(obj)));
1165+
util.inspect.defaultOptions.depth = oldOptions.depth;
1166+
assert(!/Object/.test(util.inspect(obj)));
11671167
assert.strictEqual(
11681168
JSON.stringify(util.inspect.defaultOptions),
11691169
JSON.stringify(oldOptions)
@@ -1175,7 +1175,7 @@ if (typeof Symbol !== 'undefined') {
11751175
assert(/Object/.test(util.inspect(obj)));
11761176
util.inspect.defaultOptions = oldOptions;
11771177
assert(/1 more item/.test(util.inspect(arr)));
1178-
assert(/Object/.test(util.inspect(obj)));
1178+
assert(!/Object/.test(util.inspect(obj)));
11791179
assert.strictEqual(
11801180
JSON.stringify(util.inspect.defaultOptions),
11811181
JSON.stringify(oldOptions)
@@ -1561,11 +1561,19 @@ util.inspect(process);
15611561
let head = list;
15621562
// A linked list of length 100k should be inspectable in some way, even though
15631563
// the real cutoff value is much lower than 100k.
1564-
for (let i = 0; i < 100000; i++)
1564+
for (let i = 0; i < 100000; i++) {
15651565
head = head.next = {};
1566+
}
1567+
1568+
const res = Array(15)
1569+
.fill(0)
1570+
.map((_, i) => `{ next:\n${' '.repeat(i + 1)}`)
1571+
.join('') +
1572+
'{ next: { next: { next: { next: { next: { next:' +
1573+
' [Object] } } } } } } } } } } } } } } } } } } } } }';
15661574
assert.strictEqual(
15671575
util.inspect(list),
1568-
'{ next: { next: { next: [Object] } } }'
1576+
res
15691577
);
15701578
const longList = util.inspect(list, { depth: Infinity });
15711579
const match = longList.match(/next/g);

0 commit comments

Comments
 (0)
Please sign in to comment.