Skip to content

Commit b994b8e

Browse files
committed
util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is changed to showing unlimited depth in case util.inspect is called directly. The default is kept as before for console.log and similar. Using console.dir will now show a depth of up to five and console.assert / console.trace will show a unlimited depth. PR-URL: #17907 Refs: #12693 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 9d39581 commit b994b8e

File tree

6 files changed

+60
-33
lines changed

6 files changed

+60
-33
lines changed

doc/api/util.md

+24-9
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ stream.write('With ES6');
327327
<!-- YAML
328328
added: v0.3.0
329329
changes:
330+
- version: REPLACEME
331+
pr-url: https://github.com/nodejs/node/pull/17907
332+
description: The `depth` default changed to Infinity.
330333
- version: REPLACEME
331334
pr-url: https://github.com/nodejs/node/pull/REPLACEME
332335
description: The `compact` option is supported now.
@@ -349,9 +352,6 @@ changes:
349352
* `options` {Object}
350353
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
351354
properties will be included in the formatted result. Defaults to `false`.
352-
* `depth` {number} Specifies the number of times to recurse while formatting
353-
the `object`. This is useful for inspecting large complicated objects.
354-
Defaults to `2`. To make it recurse indefinitely pass `null`.
355355
* `colors` {boolean} If `true`, the output will be styled with ANSI color
356356
codes. Defaults to `false`. Colors are customizable, see
357357
[Customizing `util.inspect` colors][].
@@ -362,8 +362,8 @@ changes:
362362
objects. Defaults to `false`.
363363
* `maxArrayLength` {number} Specifies the maximum number of array and
364364
`TypedArray` elements to include when formatting. Defaults to `100`. Set to
365-
`null` to show all array elements. Set to `0` or negative to show no array
366-
elements.
365+
`null` or `Infinity` to show all array elements. Set to `0` or negative to
366+
show no array elements.
367367
* `breakLength` {number} The length at which an object's keys are split
368368
across multiple lines. Set to `Infinity` to format an object as a single
369369
line. Defaults to 60 for legacy compatibility.
@@ -374,6 +374,10 @@ changes:
374374
objects the same as arrays. Note that no text will be reduced below 16
375375
characters, no matter the `breakLength` size. For more information, see the
376376
example below. Defaults to `true`.
377+
* `depth` {number} Specifies the number visible nested Objects in an `object`.
378+
This is useful to minimize the inspection output for large complicated
379+
objects. To make it recurse indefinitely pass `null` or `Infinity`. Defaults
380+
to `Infinity`.
377381

378382
The `util.inspect()` method returns a string representation of `object` that is
379383
intended for debugging. The output of `util.inspect` may change at any time
@@ -398,12 +402,23 @@ util.inspect(new Bar()); // 'Bar {}'
398402
util.inspect(baz); // '[foo] {}'
399403
```
400404

401-
The following example inspects all properties of the `util` object:
405+
The following example limits the inspected output of the `paths` property:
402406

403407
```js
404408
const util = require('util');
405409

406-
console.log(util.inspect(util, { showHidden: true, depth: null }));
410+
console.log(util.inspect(module, { depth: 0 }));
411+
// Instead of showing all entries in `paths` `[Array]` is used to limit the
412+
// output for readability:
413+
414+
// Module {
415+
// id: '<repl>',
416+
// exports: {},
417+
// parent: undefined,
418+
// filename: null,
419+
// loaded: false,
420+
// children: [],
421+
// paths: [Array] }
407422
```
408423

409424
Values may supply their own custom `inspect(depth, opts)` functions, when
@@ -423,7 +438,7 @@ const o = {
423438
'foo']], 4],
424439
b: new Map([['za', 1], ['zb', 'test']])
425440
};
426-
console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
441+
console.log(util.inspect(o, { compact: true, breakLength: 80 }));
427442

428443
// This will print
429444

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

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

442457
// {
443458
// a: [

lib/repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ function hasOwnProperty(obj, prop) {
103103
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
104104
// This is the default "writer" value if none is passed in the REPL options.
105105
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
106-
writer.options =
107-
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
106+
writer.options = Object.assign({},
107+
util.inspect.defaultOptions,
108+
{ showProxy: true, depth: 2 });
108109

109110
exports._builtinLibs = internalModule.builtinLibs;
110111

lib/util.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const {
6464

6565
const inspectDefaultOptions = Object.seal({
6666
showHidden: false,
67-
depth: 2,
67+
depth: null,
6868
colors: false,
6969
customInspect: true,
7070
showProxy: false,
@@ -317,8 +317,7 @@ Object.defineProperty(inspect, 'defaultOptions', {
317317
if (options === null || typeof options !== 'object') {
318318
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
319319
}
320-
Object.assign(inspectDefaultOptions, options);
321-
return inspectDefaultOptions;
320+
return _extend(inspectDefaultOptions, options);
322321
}
323322
});
324323

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/BufferList');
6+
const util = require('util');
67

78
// Test empty buffer list.
89
const emptyList = new BufferList();
@@ -25,3 +26,10 @@ assert.strictEqual(list.join(','), 'foo');
2526
const shifted = list.shift();
2627
assert.strictEqual(shifted, 'foo');
2728
assert.deepStrictEqual(list, new BufferList());
29+
30+
const tmp = util.inspect.defaultOptions.colors;
31+
util.inspect.defaultOptions = { colors: true };
32+
assert.strictEqual(
33+
util.inspect(list),
34+
'BufferList { length: \u001b[33m0\u001b[39m }');
35+
util.inspect.defaultOptions = { colors: tmp };

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ const expected1 = 'Proxy [ {}, {} ]';
4848
const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
4949
const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
5050
const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
51-
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], {} ],' +
51+
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ],' +
5252
' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ]' +
53-
', Proxy [ Proxy [Array], {} ] ] ]';
54-
const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], Proxy [Array]' +
55-
' ],\n Proxy [ Proxy [Array], Proxy [Array] ] ],\n' +
56-
' Proxy [ Proxy [ Proxy [Array], Proxy [Array] ],\n' +
57-
' Proxy [ Proxy [Array], Proxy [Array] ] ] ]';
53+
', Proxy [ Proxy [ {}, {} ], {} ] ] ]';
54+
const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ], ' +
55+
'Proxy [ {}, {} ] ],\n' +
56+
' Proxy [ Proxy [ {}, {} ], ' +
57+
'Proxy [ Proxy [ {}, {} ], {} ] ] ],\n' +
58+
' Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ], ' +
59+
'Proxy [ {}, {} ] ],\n' +
60+
' Proxy [ Proxy [ {}, {} ], ' +
61+
'Proxy [ Proxy [ {}, {} ], {} ] ] ] ]';
5862
assert.strictEqual(
5963
util.inspect(proxy1, { showProxy: true, depth: null }),
6064
expected1);

test/parallel/test-util-inspect.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ assert.strictEqual(util.inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
6868
assert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }');
6969
assert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }');
7070
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
71-
'{ a: { b: { c: [Object] } } }');
71+
'{ a: { b: { c: { d: 2 } } } }');
7272
assert.strictEqual(
7373
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
7474
'{ a: { b: { c: { d: 2 } } } }');
@@ -106,7 +106,7 @@ assert.strictEqual(util.inspect((new JSStream())._externalStream),
106106
assert.strictEqual(util.inspect({ a: regexp }, false, 0), '{ a: /regexp/ }');
107107
}
108108

109-
assert(/Object/.test(
109+
assert(!/Object/.test(
110110
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true)
111111
));
112112
assert(!/Object/.test(
@@ -1012,15 +1012,15 @@ if (typeof Symbol !== 'undefined') {
10121012
// Empty and circular before depth
10131013
{
10141014
const arr = [[[[]]]];
1015-
assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]');
1015+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [] ] ] ]');
10161016
arr[0][0][0][0] = [];
1017-
assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]');
1017+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Array] ] ] ]');
10181018
arr[0][0][0] = {};
1019-
assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]');
1019+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ {} ] ] ]');
10201020
arr[0][0][0] = { a: 2 };
1021-
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
1021+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Object] ] ] ]');
10221022
arr[0][0][0] = arr;
1023-
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
1023+
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Circular] ] ] ]');
10241024
}
10251025

10261026
// Corner cases.
@@ -1117,22 +1117,22 @@ if (typeof Symbol !== 'undefined') {
11171117
assert(!/1 more item/.test(util.inspect(arr)));
11181118
util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength;
11191119
assert(/1 more item/.test(util.inspect(arr)));
1120-
util.inspect.defaultOptions.depth = null;
1121-
assert(!/Object/.test(util.inspect(obj)));
1122-
util.inspect.defaultOptions.depth = oldOptions.depth;
1120+
util.inspect.defaultOptions.depth = 2;
11231121
assert(/Object/.test(util.inspect(obj)));
1122+
util.inspect.defaultOptions.depth = oldOptions.depth;
1123+
assert(!/Object/.test(util.inspect(obj)));
11241124
assert.strictEqual(
11251125
JSON.stringify(util.inspect.defaultOptions),
11261126
JSON.stringify(oldOptions)
11271127
);
11281128

11291129
// Set multiple options through object assignment
1130-
util.inspect.defaultOptions = { maxArrayLength: null, depth: null };
1130+
util.inspect.defaultOptions = { maxArrayLength: null, depth: 2 };
11311131
assert(!/1 more item/.test(util.inspect(arr)));
1132-
assert(!/Object/.test(util.inspect(obj)));
1132+
assert(/Object/.test(util.inspect(obj)));
11331133
util.inspect.defaultOptions = oldOptions;
11341134
assert(/1 more item/.test(util.inspect(arr)));
1135-
assert(/Object/.test(util.inspect(obj)));
1135+
assert(!/Object/.test(util.inspect(obj)));
11361136
assert.strictEqual(
11371137
JSON.stringify(util.inspect.defaultOptions),
11381138
JSON.stringify(oldOptions)

0 commit comments

Comments
 (0)