util: remove custom inspection function

This removes the deprecated custom inspection function and fixes
all tests accordingly.

Refs: https://github.com/nodejs/node/issues/15549

PR-URL: https://github.com/nodejs/node/pull/20722
Refs: https://github.com/nodejs/node/issues/15549
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-05-14 18:44:30 +02:00
parent 456a8193cb
commit 27df81cd18
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
6 changed files with 37 additions and 116 deletions

View File

@ -718,7 +718,7 @@ Type: Runtime
<a id="DEP0079"></a> <a id="DEP0079"></a>
### DEP0079: Custom inspection function on Objects via .inspect() ### DEP0079: Custom inspection function on Objects via .inspect()
Type: Runtime Type: End-of-Life
Using a property named `inspect` on an object to specify a custom inspection Using a property named `inspect` on an object to specify a custom inspection
function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][] function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][]

View File

@ -393,8 +393,9 @@ changes:
* `colors` {boolean} If `true`, the output will be styled with ANSI color * `colors` {boolean} If `true`, the output will be styled with ANSI color
codes. Colors are customizable, see [Customizing `util.inspect` colors][]. codes. Colors are customizable, see [Customizing `util.inspect` colors][].
**Default:** `false`. **Default:** `false`.
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)` * `customInspect` {boolean} If `false`, then
functions will not be called. **Default:** `true`. `[util.inspect.custom](depth, opts)` functions will not be called.
**Default:** `true`.
* `showProxy` {boolean} If `true`, then objects and functions that are * `showProxy` {boolean} If `true`, then objects and functions that are
`Proxy` objects will be introspected to show their `target` and `handler` `Proxy` objects will be introspected to show their `target` and `handler`
objects. **Default:** `false`. objects. **Default:** `false`.
@ -416,7 +417,6 @@ changes:
objects the same as arrays. Note that no text will be reduced below 16 objects the same as arrays. Note that no text will be reduced below 16
characters, no matter the `breakLength` size. For more information, see the characters, no matter the `breakLength` size. For more information, see the
example below. **Default:** `true`. example below. **Default:** `true`.
* Returns: {string} The representation of passed object * Returns: {string} The representation of passed object
The `util.inspect()` method returns a string representation of `object` that is The `util.inspect()` method returns a string representation of `object` that is
@ -450,10 +450,6 @@ const util = require('util');
console.log(util.inspect(util, { showHidden: true, depth: null })); console.log(util.inspect(util, { showHidden: true, depth: null }));
``` ```
Values may supply their own custom `inspect(depth, opts)` functions, when
called these receive the current `depth` in the recursive inspection, as well as
the options object passed to `util.inspect()`.
The following example highlights the difference with the `compact` option: The following example highlights the difference with the `compact` option:
```js ```js
@ -568,9 +564,9 @@ terminals.
<!-- type=misc --> <!-- type=misc -->
Objects may also define their own `[util.inspect.custom](depth, opts)` Objects may also define their own `[util.inspect.custom](depth, opts)` function
(or the equivalent but deprecated `inspect(depth, opts)`) function that that `util.inspect()` will invoke and use the result of when inspecting the
`util.inspect()` will invoke and use the result of when inspecting the object: object:
```js ```js
const util = require('util'); const util = require('util');

View File

@ -447,17 +447,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
// Provide a hook for user-specified inspect functions. // Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it // Check that value is an object with an inspect function on it
if (ctx.customInspect) { if (ctx.customInspect) {
let maybeCustom = value[customInspectSymbol]; const maybeCustom = value[customInspectSymbol];
if (!maybeCustom && value.inspect !== exports.inspect &&
typeof value.inspect === 'function') {
maybeCustom = deprecate(
value.inspect,
'Custom inspection function on Objects via .inspect() is deprecated',
'DEP0079'
);
}
if (typeof maybeCustom === 'function' && if (typeof maybeCustom === 'function' &&
// Filter out the util module, its inspect function is special // Filter out the util module, its inspect function is special
maybeCustom !== exports.inspect && maybeCustom !== exports.inspect &&

View File

@ -22,6 +22,7 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const util = require('util');
assert.ok(process.stdout.writable); assert.ok(process.stdout.writable);
assert.ok(process.stderr.writable); assert.ok(process.stderr.writable);
@ -46,8 +47,8 @@ assert.throws(() => console.timeEnd(Symbol('test')),
TypeError); TypeError);
// an Object with a custom .inspect() function // An Object with a custom inspect function.
const custom_inspect = { foo: 'bar', inspect: () => 'inspect' }; const custom_inspect = { foo: 'bar', [util.inspect.custom]: () => 'inspect' };
const strings = []; const strings = [];
const errStrings = []; const errStrings = [];
@ -192,9 +193,11 @@ for (const expected of expectedStrings) {
} }
assert.strictEqual(strings.shift(), assert.strictEqual(strings.shift(),
"{ foo: 'bar', inspect: [Function: inspect] }\n"); "{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
'[Function: [util.inspect.custom]] }\n');
assert.strictEqual(strings.shift(), assert.strictEqual(strings.shift(),
"{ foo: 'bar', inspect: [Function: inspect] }\n"); "{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
'[Function: [util.inspect.custom]] }\n');
assert.ok(strings.shift().includes('foo: [Object]')); assert.ok(strings.shift().includes('foo: [Object]'));
assert.strictEqual(strings.shift().includes('baz'), false); assert.strictEqual(strings.shift().includes('baz'), false);
assert.strictEqual(strings.shift(), 'inspect inspect\n'); assert.strictEqual(strings.shift(), 'inspect inspect\n');

View File

@ -1,19 +0,0 @@
'use strict';
const common = require('../common');
// Test that deprecation warning for custom inspection via the `.inspect()`
// property (on the target object) is emitted once and only once.
const util = require('util');
{
const target = { inspect: () => 'Fhqwhgads' };
// `common.expectWarning` will expect the warning exactly one time only
common.expectWarning(
'DeprecationWarning',
'Custom inspection function on Objects via .inspect() is deprecated',
'DEP0079'
);
util.inspect(target); // should emit deprecation warning
util.inspect(target); // should not emit deprecation warning
}

View File

@ -531,10 +531,10 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
); );
} }
// GH-1941 // https://github.com/nodejs/node-v0.x-archive/issues/1941
assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}'); assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
// GH-1944 // https://github.com/nodejs/node-v0.x-archive/issues/1944
{ {
const d = new Date(); const d = new Date();
d.toUTCString = null; d.toUTCString = null;
@ -549,20 +549,20 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
} }
// Should not throw. // Should not throw.
const r = /regexp/;
r.toString = null;
util.inspect(r);
// Bug with user-supplied inspect function returns non-string.
util.inspect([{ inspect: () => 123 }]);
// GH-2225
{ {
const x = { inspect: util.inspect }; const r = /regexp/;
assert.strictEqual(util.inspect(x).includes('inspect'), true); r.toString = null;
util.inspect(r);
} }
// util.inspect should display the escaped value of a key. // See https://github.com/nodejs/node-v0.x-archive/issues/2225
{
const x = { [util.inspect.custom]: util.inspect };
assert(util.inspect(x).includes(
'[Symbol(util.inspect.custom)]: \n { [Function: inspect]'));
}
// `util.inspect` should display the escaped value of a key.
{ {
const w = { const w = {
'\\': 1, '\\': 1,
@ -660,8 +660,8 @@ util.inspect({ hasOwnProperty: null });
} }
{ {
// "customInspect" option can enable/disable calling inspect() on objects. // "customInspect" option can enable/disable calling [util.inspect.custom]().
const subject = { inspect: () => 123 }; const subject = { [util.inspect.custom]: () => 123 };
assert.strictEqual( assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('123'), util.inspect(subject, { customInspect: true }).includes('123'),
@ -680,31 +680,6 @@ util.inspect({ hasOwnProperty: null });
true true
); );
// Custom inspect() functions should be able to return other Objects.
subject.inspect = () => ({ foo: 'bar' });
assert.strictEqual(util.inspect(subject), '{ foo: \'bar\' }');
subject.inspect = (depth, opts) => {
assert.strictEqual(opts.customInspectOptions, true);
};
util.inspect(subject, { customInspectOptions: true });
}
{
// "customInspect" option can enable/disable calling [util.inspect.custom]().
const subject = { [util.inspect.custom]: () => 123 };
assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('123'),
true
);
assert.strictEqual(
util.inspect(subject, { customInspect: false }).includes('123'),
false
);
// A custom [util.inspect.custom]() should be able to return other Objects. // A custom [util.inspect.custom]() should be able to return other Objects.
subject[util.inspect.custom] = () => ({ foo: 'bar' }); subject[util.inspect.custom] = () => ({ foo: 'bar' });
@ -717,43 +692,16 @@ util.inspect({ hasOwnProperty: null });
util.inspect(subject, { customInspectOptions: true }); util.inspect(subject, { customInspectOptions: true });
} }
{
// [util.inspect.custom] takes precedence over inspect.
const subject = {
[util.inspect.custom]() { return 123; },
inspect() { return 456; }
};
assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('123'),
true
);
assert.strictEqual(
util.inspect(subject, { customInspect: false }).includes('123'),
false
);
assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('456'),
false
);
assert.strictEqual(
util.inspect(subject, { customInspect: false }).includes('456'),
false
);
}
{ {
// Returning `this` from a custom inspection function works. // Returning `this` from a custom inspection function works.
assert.strictEqual(util.inspect({ a: 123, inspect() { return this; } }),
'{ a: 123, inspect: [Function: inspect] }');
const subject = { a: 123, [util.inspect.custom]() { return this; } }; const subject = { a: 123, [util.inspect.custom]() { return this; } };
const UIC = 'util.inspect.custom'; const UIC = 'util.inspect.custom';
assert.strictEqual(util.inspect(subject), assert.strictEqual(util.inspect(subject),
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`); `{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
} }
// util.inspect with "colors" option should produce as many lines as without it. // Using `util.inspect` with "colors" option should produce as many lines as
// without it.
{ {
function testLines(input) { function testLines(input) {
const countLines = (str) => (str.match(/\n/g) || []).length; const countLines = (str) => (str.match(/\n/g) || []).length;
@ -1160,8 +1108,11 @@ util.inspect(process);
// Setting custom inspect property to a non-function should do nothing. // Setting custom inspect property to a non-function should do nothing.
{ {
const obj = { inspect: 'fhqwhgads' }; const obj = { [util.inspect.custom]: 'fhqwhgads' };
assert.strictEqual(util.inspect(obj), "{ inspect: 'fhqwhgads' }"); assert.strictEqual(
util.inspect(obj),
"{ [Symbol(util.inspect.custom)]: 'fhqwhgads' }"
);
} }
{ {