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: allow to disable only custom inspect "named" method #16266

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
10 changes: 7 additions & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,13 @@ changes:
* `colors` {boolean} If `true`, the output will be styled with ANSI color
codes. Defaults to `false`. Colors are customizable, see
[Customizing `util.inspect` colors][].
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)`
functions exported on the `object` being inspected will not be called.
Defaults to `true`.
* `customInspect` {boolean} If `false`, then custom functions exported
by the `object` being inspected, either with the `inspect` name or with
the `inspect.custom` Symbol, will not be called to generate it's
inspected value. Defaults to `true`.
* `customInspectMethod` {boolean} If `false`, then the custom inspect
method exported by `inspect` name on the `object` being inspected will
not be called. Defaults to `true`.
* `showProxy` {boolean} If `true`, then objects and functions that are
`Proxy` objects will be introspected to show their `target` and `handler`
objects. Defaults to `false`.
Expand Down
7 changes: 6 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const inspectDefaultOptions = Object.seal({
depth: 2,
colors: false,
customInspect: true,
customInspectMethod: true,
showProxy: false,
maxArrayLength: 100,
breakLength: 60
Expand Down Expand Up @@ -296,6 +297,7 @@ function inspect(obj, opts) {
depth: inspectDefaultOptions.depth,
colors: inspectDefaultOptions.colors,
customInspect: inspectDefaultOptions.customInspect,
customInspectMethod: inspectDefaultOptions.customInspectMethod,
showProxy: inspectDefaultOptions.showProxy,
maxArrayLength: inspectDefaultOptions.maxArrayLength,
breakLength: inspectDefaultOptions.breakLength,
Expand Down Expand Up @@ -420,7 +422,10 @@ function formatValue(ctx, value, recurseTimes, ln) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect) {
const maybeCustomInspect = value[customInspectSymbol] || value.inspect;
let maybeCustomInspect = value[customInspectSymbol];
if (!maybeCustomInspect && ctx.customInspectMethod && 'inspect' in value) {
maybeCustomInspect = value.inspect;
}

if (typeof maybeCustomInspect === 'function' &&
// Filter out the util module, its inspect function is special
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@ assert.doesNotThrow(() => {
true
);

// "customInspectMethod" option can disable calling inspect() on objects
assert.strictEqual(
util.inspect(subject, { customInspectMethod: false }).includes('123'),
false
);
assert.strictEqual(
util.inspect(subject, { customInspectMethod: false }).includes('inspect'),
true
);

// custom inspect() functions should be able to return other Objects
subject.inspect = () => ({ foo: 'bar' });

Expand All @@ -706,6 +716,12 @@ assert.doesNotThrow(() => {
false
);

// "customInspectMethod: false" should not override the Symbol
assert.strictEqual(
util.inspect(subject, { customInspectMethod: false }).includes('123'),
true
);

// a custom [util.inspect.custom]() should be able to return other Objects
subject[util.inspect.custom] = () => ({ foo: 'bar' });

Expand Down