Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: change inspect's default depth to 4 #28269

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ corresponding argument. Supported specifiers are:
circular references.
* `%o` - `Object`. A string representation of an object with generic JavaScript
object formatting. Similar to `util.inspect()` with options
`{ showHidden: true, showProxy: true }`. This will show the full object
including non-enumerable properties and proxies.
`{ showHidden: true, showProxy: true, depth: 4 }`. This will show the full
object including non-enumerable properties and proxies.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is just documenting existing behaviour. %o is already defaulting to depth of 4 before this change here.

* `%O` - `Object`. A string representation of an object with generic JavaScript
object formatting. Similar to `util.inspect()` without options. This will show
the full object not including non-enumerable properties and proxies.
Expand Down Expand Up @@ -392,6 +392,9 @@ stream.write('With ES6');
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/28269
description: The `depth` option's default value changed to `4`.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/27685
description: Circular references now include a marker to the reference.
Expand Down Expand Up @@ -459,7 +462,7 @@ changes:
* `depth` {number} Specifies the number of times to recurse while formatting
`object`. This is useful for inspecting large objects. To recurse up to
the maximum call stack size pass `Infinity` or `null`.
**Default:** `2`.
**Default:** `4`.
* `colors` {boolean} If `true`, the output is styled with ANSI color
codes. Colors are customizable. See [Customizing `util.inspect` colors][].
**Default:** `false`.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const builtInObjects = new Set(

const inspectDefaultOptions = Object.seal({
showHidden: false,
depth: 2,
depth: 4,
colors: false,
customInspect: true,
showProxy: false,
Expand Down
32 changes: 18 additions & 14 deletions test/parallel/test-util-inspect-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,24 @@ const expected1 = 'Proxy [ {}, {} ]';
const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
const expected5 = 'Proxy [\n ' +
'Proxy [ Proxy [ Proxy [Array], {} ], Proxy [ {}, {} ] ],\n' +
' Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [Array], {} ] ]' +
'\n]';
const expected6 = 'Proxy [\n' +
' Proxy [\n' +
' Proxy [ Proxy [Array], Proxy [Array] ],\n' +
' Proxy [ Proxy [Array], Proxy [Array] ]\n' +
' ],\n' +
' Proxy [\n' +
' Proxy [ Proxy [Array], Proxy [Array] ],\n' +
' Proxy [ Proxy [Array], Proxy [Array] ]\n' +
' ]\n' +
']';
const expected5 =
'Proxy [\n' +
' Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ],\n' +
' Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]\n' +
']';

const expected6 =
'Proxy [\n' +
' Proxy [\n' +
' Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ],\n' +
' Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]\n' +
' ],\n' +
' Proxy [\n' +
' Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ],\n' +
' Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]\n' +
' ]\n' +
']';

assert.strictEqual(
util.inspect(proxy1, { showProxy: true, depth: null }),
expected1);
Expand Down
63 changes: 44 additions & 19 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ assert.strictEqual(util.inspect({ a: function*() {} }),
assert.strictEqual(util.inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
assert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }');
assert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }');
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
'{ a: { b: { c: [Object] } } }');
assert.strictEqual(
util.inspect({ 'a': { 'b': { 'c': { 'd': { 'e': { 'f': {} } } } } } }),
'{\n a: {\n b: { c: { d: { e: [Object] } } }\n }\n}'
);
assert.strictEqual(
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
'{\n a: { b: { c: { d: 2 } } }\n}');
Expand Down Expand Up @@ -154,10 +156,12 @@ assert.strictEqual(util.inspect((new JSStream())._externalStream),
}

assert(/Object/.test(
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true)
util.inspect({ a: { a: { a: { a: { a: { a: {} } } } } } },
undefined, undefined, true)
));
assert(!/Object/.test(
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, null, true)
util.inspect({ a: { a: { a: { a: { a: { a: {} } } } } } },
undefined, null, true)
));

{
Expand Down Expand Up @@ -1253,18 +1257,36 @@ if (typeof Symbol !== 'undefined') {

// Empty and circular before depth.
{
const arr = [[[[]]]];
assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]');
arr[0][0][0][0] = [];
assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]');
arr[0][0][0] = {};
assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]');
arr[0][0][0] = { a: 2 };
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
arr[0][0][0] = arr;
assert.strictEqual(util.inspect(arr), '<ref *1> [ [ [ [Circular *1] ] ] ]');
const arr = [[[[[[]]]]]];
assert.strictEqual(
util.inspect(arr),
'[\n [\n [ [ [ [] ] ] ]\n ]\n]'
);
Copy link
Contributor Author

@silverwind silverwind Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not particularily happy with how these are printed, by the way. It almost seems like the linebreak logic in inspect has a bias towards depth of 2, but didn't investigate that yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is currently set to compact: 3. That means the three most inner levels are combined on a single line in case they fit into breakLength. This has "five" levels (the most inner part counts as primitive, not as level). That's why the most outer two levels create the new lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Do you recall why the particular value of 3 was chosen? Was it to accomodate a depth of 2 or are they unrelated?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking how many objects are all on the same line is difficult when it becomes to many. That's why I limited it to three by default. It is unrelated to the depth.

arr[0][0][0][0][0][0] = [];
assert.strictEqual(
util.inspect(arr),
'[\n [\n [ [ [ [Array] ] ] ]\n ]\n]'
);
arr[0][0][0][0][0] = {};
assert.strictEqual(
util.inspect(arr),
'[\n [\n [ [ [ {} ] ] ]\n ]\n]'
);
arr[0][0][0][0][0] = { a: 2 };
assert.strictEqual(
util.inspect(arr),
'[\n [\n [ [ [ [Object] ] ] ]\n ]\n]'
);
arr[0][0][0][0][0] = arr;
assert.strictEqual(
util.inspect(arr),
'<ref *1> [\n [\n [ [ [ [Circular *1] ] ] ]\n ]\n]'
);
arr[0][0][0] = arr[0][0];
assert.strictEqual(util.inspect(arr), '[ [ <ref *1> [ [Circular *1] ] ] ]');
assert.strictEqual(
util.inspect(arr),
'[ [ <ref *1> [ [Circular *1] ] ] ]'
);
}

// Corner cases.
Expand Down Expand Up @@ -1354,7 +1376,7 @@ if (typeof Symbol !== 'undefined') {
// util.inspect.defaultOptions tests.
{
const arr = new Array(101).fill();
const obj = { a: { a: { a: { a: 1 } } } };
const obj = { a: { a: { a: { a: { a: { a: 1 } } } } } };

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

Expand Down Expand Up @@ -1793,7 +1815,7 @@ util.inspect(process);
head = head.next = {};
assert.strictEqual(
util.inspect(list),
'{ next: { next: { next: [Object] } } }'
'{\n next: {\n next: { next: { next: { next: [Object] } } }\n }\n}'
);
const longList = util.inspect(list, { depth: Infinity });
const match = longList.match(/next/g);
Expand Down Expand Up @@ -2147,7 +2169,7 @@ assert.strictEqual(
getset.foo = new Set([[{ a: true }, 2, {}], 'foobar', { x: 1 }]);
assert.strictEqual(
inspect(getset, { getters: true }),
'{\n foo: [Getter/Setter] Set { [ [Object], 2, {} ], ' +
'{\n foo: [Getter/Setter] Set { [ { a: true }, 2, {} ], ' +
"'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}");
}

Expand Down Expand Up @@ -2302,7 +2324,10 @@ assert.strictEqual(
})
};

out = util.inspect(obj, { compact: 1, breakLength: Infinity, colors: true });
out = util.inspect(
obj,
{ compact: 1, depth: 2, breakLength: Infinity, colors: true }
);

expected = [
'{',
Expand Down