Skip to content

Commit 4eb5399

Browse files
committed
util: add a "customInspect" option to util.inspect()
For disabling calling the custom `inspect()` function when defined on an object that is being inspected.
1 parent e3ee289 commit 4eb5399

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/api/util.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ formatted string:
8383
- `colors` - if `true`, then the output will be styled with ANSI color codes.
8484
Defaults to `false`. Colors are customizable, see below.
8585

86+
- `customInspect` - if `false`, then custom `inspect()` functions defined on the
87+
objects being inspected won't be called. Defaults to `true`.
88+
8689
Example of inspecting all properties of the `util` object:
8790

8891
var util = require('util');

lib/util.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function inspect(obj, opts/* legacy: showHidden, depth, colors*/) {
132132
if (typeof ctx.showHidden === 'undefined') ctx.showHidden = false;
133133
if (typeof ctx.depth === 'undefined') ctx.depth = 2;
134134
if (typeof ctx.colors === 'undefined') ctx.colors = false;
135+
if (typeof ctx.customInspect === 'undefined') ctx.customInspect = true;
135136
if (ctx.colors) ctx.stylize = stylizeWithColor;
136137
return formatValue(ctx, obj, ctx.depth);
137138
}
@@ -200,7 +201,7 @@ function arrayToHash(array) {
200201
function formatValue(ctx, value, recurseTimes) {
201202
// Provide a hook for user-specified inspect functions.
202203
// Check that value is an object with an inspect function on it
203-
if (value && typeof value.inspect === 'function' &&
204+
if (ctx.customInspect && value && typeof value.inspect === 'function' &&
204205
// Filter out the util module, it's inspect function is special
205206
value.inspect !== exports.inspect &&
206207
// Also filter out any prototype objects using the circular check.

test/simple/test-util-inspect.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,11 @@ assert(util.inspect(subject, { colors: true }).indexOf('\u001b[32m') !== -1);
149149
assert(util.inspect(subject, { depth: 2 }).indexOf('c: [Object]') !== -1);
150150
assert(util.inspect(subject, { depth: 0 }).indexOf('a: [Object]') !== -1);
151151
assert(util.inspect(subject, { depth: null }).indexOf('{ d: 0 }') !== -1);
152+
153+
// "customInspect" option can enable/disable calling inspect() on objects
154+
subject = { inspect: function() { return 123; } };
155+
156+
assert(util.inspect(subject, { customInspect: true }).indexOf('123') !== -1);
157+
assert(util.inspect(subject, { customInspect: true }).indexOf('inspect') === -1);
158+
assert(util.inspect(subject, { customInspect: false }).indexOf('123') === -1);
159+
assert(util.inspect(subject, { customInspect: false }).indexOf('inspect') !== -1);

0 commit comments

Comments
 (0)