Skip to content

Commit

Permalink
util: deprecate obj.inspect for custom inspection
Browse files Browse the repository at this point in the history
The existence of `obj.inspect()` for custom inspection can cause people
to unintentionally break `console.log()` and friends. This is a
documentation-only deprecation that can hopefully land in 8.x.

PR-URL: #15631
Refs: #15549
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Trott authored and MylesBorins committed Oct 3, 2017
1 parent da7c87f commit 97f7590
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
12 changes: 12 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,16 @@ difference is that `querystring.parse()` does url decoding:
{ '%E5%A5%BD': '1' }
```
<a id="DEP0079"></a>
### DEP0079: Custom inspection function on Objects via .inspect()
Type: Documentation-only
Using a property named `inspect` on an object to specify a custom inspection
function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][]
instead. For backwards compatibility with Node.js prior to version 6.4.0, both
may be specified.
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
Expand Down Expand Up @@ -692,6 +702,8 @@ difference is that `querystring.parse()` does url decoding:
[`util._extend()`]: util.html#util_util_extend_target_source
[`util.debug()`]: util.html#util_util_debug_string
[`util.error()`]: util.html#util_util_error_strings
[`util.inspect()`]: util.html#util_util_inspect_object_options
[`util.inspect.custom`]: util.html#util_util_inspect_custom
[`util.isArray()`]: util.html#util_util_isarray_object
[`util.isBoolean()`]: util.html#util_util_isboolean_object
[`util.isBuffer()`]: util.html#util_util_isbuffer_object
Expand Down
21 changes: 3 additions & 18 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ terminals.
<!-- type=misc -->

Objects may also define their own `[util.inspect.custom](depth, opts)`
(or, equivalently `inspect(depth, opts)`) function that `util.inspect()` will
invoke and use the result of when inspecting the object:
(or the equivalent but deprecated `inspect(depth, opts)`) function that
`util.inspect()` will invoke and use the result of when inspecting the object:

```js
const util = require('util');
Expand All @@ -388,7 +388,7 @@ class Box {
this.value = value;
}

inspect(depth, options) {
[util.inspect.custom](depth, options) {
if (depth < 0) {
return options.stylize('[Box]', 'special');
}
Expand Down Expand Up @@ -427,21 +427,6 @@ util.inspect(obj);
// Returns: "{ bar: 'baz' }"
```

A custom inspection method can alternatively be provided by exposing
an `inspect(depth, opts)` method on the object:

```js
const util = require('util');

const obj = { foo: 'this will not show up in the inspect() output' };
obj.inspect = function(depth) {
return { bar: 'baz' };
};

util.inspect(obj);
// Returns: "{ bar: 'baz' }"
```

### util.inspect.custom
<!-- YAML
added: v6.6.0
Expand Down

0 comments on commit 97f7590

Please sign in to comment.