Skip to content

Commit 27df81c

Browse files
committed
util: remove custom inspection function
This removes the deprecated custom inspection function and fixes all tests accordingly. Refs: #15549 PR-URL: #20722 Refs: #15549 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 456a819 commit 27df81c

File tree

6 files changed

+36
-115
lines changed

6 files changed

+36
-115
lines changed

doc/api/deprecations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ Type: Runtime
718718
<a id="DEP0079"></a>
719719
### DEP0079: Custom inspection function on Objects via .inspect()
720720
721-
Type: Runtime
721+
Type: End-of-Life
722722
723723
Using a property named `inspect` on an object to specify a custom inspection
724724
function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][]

doc/api/util.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,9 @@ changes:
393393
* `colors` {boolean} If `true`, the output will be styled with ANSI color
394394
codes. Colors are customizable, see [Customizing `util.inspect` colors][].
395395
**Default:** `false`.
396-
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)`
397-
functions will not be called. **Default:** `true`.
396+
* `customInspect` {boolean} If `false`, then
397+
`[util.inspect.custom](depth, opts)` functions will not be called.
398+
**Default:** `true`.
398399
* `showProxy` {boolean} If `true`, then objects and functions that are
399400
`Proxy` objects will be introspected to show their `target` and `handler`
400401
objects. **Default:** `false`.
@@ -416,7 +417,6 @@ changes:
416417
objects the same as arrays. Note that no text will be reduced below 16
417418
characters, no matter the `breakLength` size. For more information, see the
418419
example below. **Default:** `true`.
419-
420420
* Returns: {string} The representation of passed object
421421

422422
The `util.inspect()` method returns a string representation of `object` that is
@@ -450,10 +450,6 @@ const util = require('util');
450450
console.log(util.inspect(util, { showHidden: true, depth: null }));
451451
```
452452

453-
Values may supply their own custom `inspect(depth, opts)` functions, when
454-
called these receive the current `depth` in the recursive inspection, as well as
455-
the options object passed to `util.inspect()`.
456-
457453
The following example highlights the difference with the `compact` option:
458454

459455
```js
@@ -568,9 +564,9 @@ terminals.
568564

569565
<!-- type=misc -->
570566

571-
Objects may also define their own `[util.inspect.custom](depth, opts)`
572-
(or the equivalent but deprecated `inspect(depth, opts)`) function that
573-
`util.inspect()` will invoke and use the result of when inspecting the object:
567+
Objects may also define their own `[util.inspect.custom](depth, opts)` function
568+
that `util.inspect()` will invoke and use the result of when inspecting the
569+
object:
574570

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

lib/util.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
447447
// Provide a hook for user-specified inspect functions.
448448
// Check that value is an object with an inspect function on it
449449
if (ctx.customInspect) {
450-
let maybeCustom = value[customInspectSymbol];
451-
452-
if (!maybeCustom && value.inspect !== exports.inspect &&
453-
typeof value.inspect === 'function') {
454-
maybeCustom = deprecate(
455-
value.inspect,
456-
'Custom inspection function on Objects via .inspect() is deprecated',
457-
'DEP0079'
458-
);
459-
}
460-
450+
const maybeCustom = value[customInspectSymbol];
461451
if (typeof maybeCustom === 'function' &&
462452
// Filter out the util module, its inspect function is special
463453
maybeCustom !== exports.inspect &&

test/parallel/test-console.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25+
const util = require('util');
2526

2627
assert.ok(process.stdout.writable);
2728
assert.ok(process.stderr.writable);
@@ -46,8 +47,8 @@ assert.throws(() => console.timeEnd(Symbol('test')),
4647
TypeError);
4748

4849

49-
// an Object with a custom .inspect() function
50-
const custom_inspect = { foo: 'bar', inspect: () => 'inspect' };
50+
// An Object with a custom inspect function.
51+
const custom_inspect = { foo: 'bar', [util.inspect.custom]: () => 'inspect' };
5152

5253
const strings = [];
5354
const errStrings = [];
@@ -192,9 +193,11 @@ for (const expected of expectedStrings) {
192193
}
193194

194195
assert.strictEqual(strings.shift(),
195-
"{ foo: 'bar', inspect: [Function: inspect] }\n");
196+
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
197+
'[Function: [util.inspect.custom]] }\n');
196198
assert.strictEqual(strings.shift(),
197-
"{ foo: 'bar', inspect: [Function: inspect] }\n");
199+
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
200+
'[Function: [util.inspect.custom]] }\n');
198201
assert.ok(strings.shift().includes('foo: [Object]'));
199202
assert.strictEqual(strings.shift().includes('baz'), false);
200203
assert.strictEqual(strings.shift(), 'inspect inspect\n');

test/parallel/test-util-inspect-deprecated.js

-19
This file was deleted.

test/parallel/test-util-inspect.js

+21-70
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,10 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
531531
);
532532
}
533533

534-
// GH-1941
534+
// https://github.com/nodejs/node-v0.x-archive/issues/1941
535535
assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
536536

537-
// GH-1944
537+
// https://github.com/nodejs/node-v0.x-archive/issues/1944
538538
{
539539
const d = new Date();
540540
d.toUTCString = null;
@@ -549,20 +549,20 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
549549
}
550550

551551
// Should not throw.
552-
const r = /regexp/;
553-
r.toString = null;
554-
util.inspect(r);
555-
556-
// Bug with user-supplied inspect function returns non-string.
557-
util.inspect([{ inspect: () => 123 }]);
552+
{
553+
const r = /regexp/;
554+
r.toString = null;
555+
util.inspect(r);
556+
}
558557

559-
// GH-2225
558+
// See https://github.com/nodejs/node-v0.x-archive/issues/2225
560559
{
561-
const x = { inspect: util.inspect };
562-
assert.strictEqual(util.inspect(x).includes('inspect'), true);
560+
const x = { [util.inspect.custom]: util.inspect };
561+
assert(util.inspect(x).includes(
562+
'[Symbol(util.inspect.custom)]: \n { [Function: inspect]'));
563563
}
564564

565-
// util.inspect should display the escaped value of a key.
565+
// `util.inspect` should display the escaped value of a key.
566566
{
567567
const w = {
568568
'\\': 1,
@@ -660,8 +660,8 @@ util.inspect({ hasOwnProperty: null });
660660
}
661661

662662
{
663-
// "customInspect" option can enable/disable calling inspect() on objects.
664-
const subject = { inspect: () => 123 };
663+
// "customInspect" option can enable/disable calling [util.inspect.custom]().
664+
const subject = { [util.inspect.custom]: () => 123 };
665665

666666
assert.strictEqual(
667667
util.inspect(subject, { customInspect: true }).includes('123'),
@@ -680,31 +680,6 @@ util.inspect({ hasOwnProperty: null });
680680
true
681681
);
682682

683-
// Custom inspect() functions should be able to return other Objects.
684-
subject.inspect = () => ({ foo: 'bar' });
685-
686-
assert.strictEqual(util.inspect(subject), '{ foo: \'bar\' }');
687-
688-
subject.inspect = (depth, opts) => {
689-
assert.strictEqual(opts.customInspectOptions, true);
690-
};
691-
692-
util.inspect(subject, { customInspectOptions: true });
693-
}
694-
695-
{
696-
// "customInspect" option can enable/disable calling [util.inspect.custom]().
697-
const subject = { [util.inspect.custom]: () => 123 };
698-
699-
assert.strictEqual(
700-
util.inspect(subject, { customInspect: true }).includes('123'),
701-
true
702-
);
703-
assert.strictEqual(
704-
util.inspect(subject, { customInspect: false }).includes('123'),
705-
false
706-
);
707-
708683
// A custom [util.inspect.custom]() should be able to return other Objects.
709684
subject[util.inspect.custom] = () => ({ foo: 'bar' });
710685

@@ -717,43 +692,16 @@ util.inspect({ hasOwnProperty: null });
717692
util.inspect(subject, { customInspectOptions: true });
718693
}
719694

720-
{
721-
// [util.inspect.custom] takes precedence over inspect.
722-
const subject = {
723-
[util.inspect.custom]() { return 123; },
724-
inspect() { return 456; }
725-
};
726-
727-
assert.strictEqual(
728-
util.inspect(subject, { customInspect: true }).includes('123'),
729-
true
730-
);
731-
assert.strictEqual(
732-
util.inspect(subject, { customInspect: false }).includes('123'),
733-
false
734-
);
735-
assert.strictEqual(
736-
util.inspect(subject, { customInspect: true }).includes('456'),
737-
false
738-
);
739-
assert.strictEqual(
740-
util.inspect(subject, { customInspect: false }).includes('456'),
741-
false
742-
);
743-
}
744-
745695
{
746696
// Returning `this` from a custom inspection function works.
747-
assert.strictEqual(util.inspect({ a: 123, inspect() { return this; } }),
748-
'{ a: 123, inspect: [Function: inspect] }');
749-
750697
const subject = { a: 123, [util.inspect.custom]() { return this; } };
751698
const UIC = 'util.inspect.custom';
752699
assert.strictEqual(util.inspect(subject),
753700
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
754701
}
755702

756-
// util.inspect with "colors" option should produce as many lines as without it.
703+
// Using `util.inspect` with "colors" option should produce as many lines as
704+
// without it.
757705
{
758706
function testLines(input) {
759707
const countLines = (str) => (str.match(/\n/g) || []).length;
@@ -1160,8 +1108,11 @@ util.inspect(process);
11601108

11611109
// Setting custom inspect property to a non-function should do nothing.
11621110
{
1163-
const obj = { inspect: 'fhqwhgads' };
1164-
assert.strictEqual(util.inspect(obj), "{ inspect: 'fhqwhgads' }");
1111+
const obj = { [util.inspect.custom]: 'fhqwhgads' };
1112+
assert.strictEqual(
1113+
util.inspect(obj),
1114+
"{ [Symbol(util.inspect.custom)]: 'fhqwhgads' }"
1115+
);
11651116
}
11661117

11671118
{

0 commit comments

Comments
 (0)