Skip to content

Commit 849aaae

Browse files
addaleaxBridgeAR
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. PR-URL: #20017 Fixes: #19405 Refs: #17907 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 85373ae commit 849aaae

File tree

6 files changed

+28
-55
lines changed

6 files changed

+28
-55
lines changed

doc/api/util.md

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

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

448-
The following example limits the inspected output of the `paths` property:
445+
The following example inspects all properties of the `util` object:
449446

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

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

467453
Values may supply their own custom `inspect(depth, opts)` functions, when
@@ -481,7 +467,7 @@ const o = {
481467
'foo']], 4],
482468
b: new Map([['za', 1], ['zb', 'test']])
483469
};
484-
console.log(util.inspect(o, { compact: true, breakLength: 80 }));
470+
console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
485471

486472
// This will print
487473

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

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

500486
// {
501487
// a: [

lib/repl.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ function hasOwnProperty(obj, prop) {
115115
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
116116
// This is the default "writer" value if none is passed in the REPL options.
117117
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
118-
writer.options = Object.assign({},
119-
util.inspect.defaultOptions,
120-
{ showProxy: true, depth: 2 });
118+
writer.options =
119+
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
121120

122121
exports._builtinLibs = builtinLibs;
123122

lib/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const {
8080

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

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

-8
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

+6-10
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

+11-11
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)