Skip to content

Commit 554ed37

Browse files
addaleaxjasnell
authored andcommitted
Revert "util: change util.inspect depth default"
This reverts commit b994b8e. This caused regressions in ecosystem code. While the change originally was semver-major and could be postponed until after Node.js 10, I think reverting it is a good choice at this point. Also, I personally do not think defaulting to a shallow inspect is a bad thing at all – quite the opposite: It makes `util.inspect()` give an overview of an object, rather than providing a full display of its contents. Changing the `depth` default to infinity fundamentally changed the role that `util.inspect()` plays, and makes output much more verbose and thus at times unusable for `console.log()`-style debugging. Fixes: #19405 Refs: #17907 PR-URL: #20089 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent 47af0a0 commit 554ed37

File tree

6 files changed

+28
-55
lines changed

6 files changed

+28
-55
lines changed

doc/api/util.md

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,6 @@ changes:
363363
- version: REPLACEME
364364
pr-url: https://github.com/nodejs/node/pull/19259
365365
description: WeakMap and WeakSet entries can now be inspected as well.
366-
- version: REPLACEME
367-
pr-url: https://github.com/nodejs/node/pull/17907
368-
description: The `depth` default changed to Infinity.
369366
- version: v9.9.0
370367
pr-url: https://github.com/nodejs/node/pull/17576
371368
description: The `compact` option is supported now.
@@ -389,6 +386,9 @@ changes:
389386
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
390387
properties will be included in the formatted result as well as [`WeakMap`][]
391388
and [`WeakSet`][] entries. **Default:** `false`.
389+
* `depth` {number} Specifies the number of times to recurse while formatting
390+
the `object`. This is useful for inspecting large complicated objects.
391+
To make it recurse indefinitely pass `null`. **Default:** `2`.
392392
* `colors` {boolean} If `true`, the output will be styled with ANSI color
393393
codes. Colors are customizable, see [Customizing `util.inspect` colors][].
394394
**Default:** `false`.
@@ -415,10 +415,7 @@ changes:
415415
objects the same as arrays. Note that no text will be reduced below 16
416416
characters, no matter the `breakLength` size. For more information, see the
417417
example below. **Default:** `true`.
418-
* `depth` {number} Specifies the number visible nested Objects in an `object`.
419-
This is useful to minimize the inspection output for large complicated
420-
objects. To make it recurse indefinitely pass `null` or `Infinity`.
421-
**Default:** `Infinity`.
418+
422419
* Returns: {string} The representation of passed object
423420

424421
The `util.inspect()` method returns a string representation of `object` that is
@@ -444,23 +441,12 @@ util.inspect(new Bar()); // 'Bar {}'
444441
util.inspect(baz); // '[foo] {}'
445442
```
446443

447-
The following example limits the inspected output of the `paths` property:
444+
The following example inspects all properties of the `util` object:
448445

449446
```js
450447
const util = require('util');
451448

452-
console.log(util.inspect(module, { depth: 0 }));
453-
// Instead of showing all entries in `paths` `[Array]` is used to limit the
454-
// output for readability:
455-
456-
// Module {
457-
// id: '<repl>',
458-
// exports: {},
459-
// parent: undefined,
460-
// filename: null,
461-
// loaded: false,
462-
// children: [],
463-
// paths: [Array] }
449+
console.log(util.inspect(util, { showHidden: true, depth: null }));
464450
```
465451

466452
Values may supply their own custom `inspect(depth, opts)` functions, when
@@ -480,7 +466,7 @@ const o = {
480466
'foo']], 4],
481467
b: new Map([['za', 1], ['zb', 'test']])
482468
};
483-
console.log(util.inspect(o, { compact: true, breakLength: 80 }));
469+
console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
484470

485471
// This will print
486472

@@ -494,7 +480,7 @@ console.log(util.inspect(o, { compact: true, breakLength: 80 }));
494480
// b: Map { 'za' => 1, 'zb' => 'test' } }
495481

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

499485
// {
500486
// a: [

lib/repl.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ function hasOwnProperty(obj, prop) {
112112
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
113113
// This is the default "writer" value if none is passed in the REPL options.
114114
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
115-
writer.options = Object.assign({},
116-
util.inspect.defaultOptions,
117-
{ showProxy: true, depth: 2 });
115+
writer.options =
116+
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
118117

119118
exports._builtinLibs = builtinLibs;
120119

lib/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const {
8181

8282
const inspectDefaultOptions = Object.seal({
8383
showHidden: false,
84-
depth: null,
84+
depth: 2,
8585
colors: false,
8686
customInspect: true,
8787
showProxy: false,

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require('../common');
44
const assert = require('assert');
55
const BufferList = require('internal/streams/buffer_list');
6-
const util = require('util');
76

87
// Test empty buffer list.
98
const emptyList = new BufferList();
@@ -31,10 +30,3 @@ assert.strictEqual(list.join(','), 'foo');
3130
const shifted = list.shift();
3231
assert.strictEqual(shifted, buf);
3332
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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ 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 [ {}, {} ], {} ],' +
51+
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], {} ],' +
5252
' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ]' +
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 [ {}, {} ], {} ] ] ] ]';
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] ] ] ]';
6258
assert.strictEqual(
6359
util.inspect(proxy1, { showProxy: true, depth: null }),
6460
expected1);

test/parallel/test-util-inspect.js

Lines changed: 11 additions & 11 deletions
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: { d: 2 } } } }');
71+
'{ a: { b: { c: [Object] } } }');
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(
@@ -1011,15 +1011,15 @@ if (typeof Symbol !== 'undefined') {
10111011
// Empty and circular before depth.
10121012
{
10131013
const arr = [[[[]]]];
1014-
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [] ] ] ]');
1014+
assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]');
10151015
arr[0][0][0][0] = [];
1016-
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Array] ] ] ]');
1016+
assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]');
10171017
arr[0][0][0] = {};
1018-
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ {} ] ] ]');
1018+
assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]');
10191019
arr[0][0][0] = { a: 2 };
1020-
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Object] ] ] ]');
1020+
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
10211021
arr[0][0][0] = arr;
1022-
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Circular] ] ] ]');
1022+
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
10231023
}
10241024

10251025
// Corner cases.
@@ -1116,10 +1116,10 @@ if (typeof Symbol !== 'undefined') {
11161116
assert(!/1 more item/.test(util.inspect(arr)));
11171117
util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength;
11181118
assert(/1 more item/.test(util.inspect(arr)));
1119-
util.inspect.defaultOptions.depth = 2;
1120-
assert(/Object/.test(util.inspect(obj)));
1121-
util.inspect.defaultOptions.depth = oldOptions.depth;
1119+
util.inspect.defaultOptions.depth = null;
11221120
assert(!/Object/.test(util.inspect(obj)));
1121+
util.inspect.defaultOptions.depth = oldOptions.depth;
1122+
assert(/Object/.test(util.inspect(obj)));
11231123
assert.strictEqual(
11241124
JSON.stringify(util.inspect.defaultOptions),
11251125
JSON.stringify(oldOptions)
@@ -1131,7 +1131,7 @@ if (typeof Symbol !== 'undefined') {
11311131
assert(/Object/.test(util.inspect(obj)));
11321132
util.inspect.defaultOptions = oldOptions;
11331133
assert(/1 more item/.test(util.inspect(arr)));
1134-
assert(!/Object/.test(util.inspect(obj)));
1134+
assert(/Object/.test(util.inspect(obj)));
11351135
assert.strictEqual(
11361136
JSON.stringify(util.inspect.defaultOptions),
11371137
JSON.stringify(oldOptions)

0 commit comments

Comments
 (0)