Skip to content

Commit 617e3e9

Browse files
committed
util: runtime deprecation for custom .inspect()
Change documentation-only deprecation for custom inspection using `object.inspect` property to a runtime deprecation. This is a breaking change. Custom inspection via `object.inspect` is deprecated because there is a more robust Symbol-based alternative to `.inspect` and the custom inspection via `object.inspect` feature means that people can accidentally break `console.log()` simply by attaching a `.inspect` property to their objects. Note that since this is a deprecation, the custom inspection will still work. The breaking change is simply the printing of a warning which could alarm users, break tests or other things that might be dependent on specific output, etc. PR-URL: #16393 Ref: #15549 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 07d39a2 commit 617e3e9

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

doc/api/deprecations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ Type: Runtime
703703
<a id="DEP0079"></a>
704704
### DEP0079: Custom inspection function on Objects via .inspect()
705705
706-
Type: Documentation-only
706+
Type: Runtime
707707
708708
Using a property named `inspect` on an object to specify a custom inspection
709709
function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][]

doc/api/util.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ environment variable. For example: `NODE_DEBUG=fs,net,tls`.
112112
added: v0.8.0
113113
changes:
114114
- version: REPLACEME
115+
pr-url: https://github.com/nodejs/node/pull/16393
115116
description: Deprecation warnings are only emitted once for each code.
116117
-->
117118

lib/util.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,23 @@ function formatValue(ctx, value, recurseTimes, ln) {
428428
// Provide a hook for user-specified inspect functions.
429429
// Check that value is an object with an inspect function on it
430430
if (ctx.customInspect) {
431-
const maybeCustomInspect = value[customInspectSymbol] || value.inspect;
431+
let maybeCustom = value[customInspectSymbol];
432+
433+
if (!maybeCustom && value.inspect !== exports.inspect &&
434+
typeof value.inspect === 'function') {
435+
maybeCustom = deprecate(
436+
value.inspect,
437+
'Custom inspection function on Objects via .inspect() is deprecated',
438+
'DEP0079'
439+
);
440+
}
432441

433-
if (typeof maybeCustomInspect === 'function' &&
442+
if (typeof maybeCustom === 'function' &&
434443
// Filter out the util module, its inspect function is special
435-
maybeCustomInspect !== exports.inspect &&
444+
maybeCustom !== exports.inspect &&
436445
// Also filter out any prototype objects using the circular check.
437446
!(value.constructor && value.constructor.prototype === value)) {
438-
const ret = maybeCustomInspect.call(value, recurseTimes, ctx);
447+
const ret = maybeCustom.call(value, recurseTimes, ctx);
439448

440449
// If the custom inspection method returned `this`, don't go into
441450
// infinite recursion.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Test that deprecation warning for custom inspection via the `.inspect()`
5+
// property (on the target object) is emitted once and only once.
6+
7+
const util = require('util');
8+
9+
{
10+
const target = { inspect: () => 'Fhqwhgads' };
11+
// `common.expectWarning` will expect the warning exactly one time only
12+
common.expectWarning(
13+
'DeprecationWarning',
14+
'Custom inspection function on Objects via .inspect() is deprecated'
15+
);
16+
util.inspect(target); // should emit deprecation warning
17+
util.inspect(target); // should not emit deprecation warning
18+
}

test/parallel/test-util-inspect.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,9 @@ if (typeof Symbol !== 'undefined') {
11501150
}
11511151

11521152
assert.doesNotThrow(() => util.inspect(process));
1153+
1154+
// Setting custom inspect property to a non-function should do nothing.
1155+
{
1156+
const obj = { inspect: 'fhqwhgads' };
1157+
assert.strictEqual(util.inspect(obj), "{ inspect: 'fhqwhgads' }");
1158+
}

0 commit comments

Comments
 (0)