-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: remove custom inspection function #20722
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,8 +393,9 @@ changes: | |
* `colors` {boolean} If `true`, the output will be styled with ANSI color | ||
codes. Colors are customizable, see [Customizing `util.inspect` colors][]. | ||
**Default:** `false`. | ||
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)` | ||
functions will not be called. **Default:** `true`. | ||
* `customInspect` {boolean} If `false`, then | ||
`[util.inspect.custom](depth, opts)` functions will not be called. | ||
**Default:** `true`. | ||
* `showProxy` {boolean} If `true`, then objects and functions that are | ||
`Proxy` objects will be introspected to show their `target` and `handler` | ||
objects. **Default:** `false`. | ||
|
@@ -416,7 +417,6 @@ changes: | |
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 | ||
example below. **Default:** `true`. | ||
|
||
* Returns: {string} The representation of passed object | ||
|
||
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 })); | ||
``` | ||
|
||
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: | ||
|
||
```js | ||
|
@@ -568,9 +564,9 @@ terminals. | |
|
||
<!-- type=misc --> | ||
|
||
Objects may also define their own `[util.inspect.custom](depth, opts)` | ||
(or the equivalent but deprecated `inspect(depth, opts)`) function that | ||
`util.inspect()` will invoke and use the result of when inspecting the object: | ||
Objects may also define their own `[util.inspect.custom](depth, opts)` function | ||
that `util.inspect()` will invoke and use the result of when inspecting the | ||
object: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There’s also the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
|
||
```js | ||
const util = require('util'); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -532,10 +532,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 {}'); | ||
|
||
// GH-1944 | ||
// https://github.com/nodejs/node-v0.x-archive/issues/1944 | ||
{ | ||
const d = new Date(); | ||
d.toUTCString = null; | ||
|
@@ -550,20 +550,20 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}'); | |
} | ||
|
||
// 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 }]); | ||
{ | ||
const r = /regexp/; | ||
r.toString = null; | ||
util.inspect(r); | ||
} | ||
|
||
// GH-2225 | ||
// See https://github.com/nodejs/node-v0.x-archive/issues/2225 | ||
{ | ||
const x = { inspect: util.inspect }; | ||
assert.strictEqual(util.inspect(x).includes('inspect'), true); | ||
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. | ||
// `util.inspect` should display the escaped value of a key. | ||
{ | ||
const w = { | ||
'\\': 1, | ||
|
@@ -661,8 +661,8 @@ util.inspect({ hasOwnProperty: null }); | |
} | ||
|
||
{ | ||
// "customInspect" option can enable/disable calling inspect() on objects. | ||
const subject = { inspect: () => 123 }; | ||
// "customInspect" option can enable/disable calling [util.inspect.custom](). | ||
const subject = { [util.inspect.custom]: () => 123 }; | ||
|
||
assert.strictEqual( | ||
util.inspect(subject, { customInspect: true }).includes('123'), | ||
|
@@ -681,31 +681,6 @@ util.inspect({ hasOwnProperty: null }); | |
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. | ||
subject[util.inspect.custom] = () => ({ foo: 'bar' }); | ||
|
||
|
@@ -718,43 +693,16 @@ util.inspect({ hasOwnProperty: null }); | |
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. | ||
assert.strictEqual(util.inspect({ a: 123, inspect() { return this; } }), | ||
'{ a: 123, inspect: [Function: inspect] }'); | ||
|
||
const subject = { a: 123, [util.inspect.custom]() { return this; } }; | ||
const UIC = 'util.inspect.custom'; | ||
assert.strictEqual(util.inspect(subject), | ||
`{ 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) { | ||
const countLines = (str) => (str.match(/\n/g) || []).length; | ||
|
@@ -1162,8 +1110,11 @@ util.inspect(process); | |
|
||
// Setting custom inspect property to a non-function should do nothing. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/property/symbol/ ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it is still a property. It is just accessed with a symbol. |
||
{ | ||
const obj = { inspect: 'fhqwhgads' }; | ||
assert.strictEqual(util.inspect(obj), "{ inspect: 'fhqwhgads' }"); | ||
const obj = { [util.inspect.custom]: 'fhqwhgads' }; | ||
assert.strictEqual( | ||
util.inspect(obj), | ||
"{ [Symbol(util.inspect.custom)]: 'fhqwhgads' }" | ||
); | ||
} | ||
|
||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can many functions be called during the inspection? Does this mean nested objects with own custom functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, it is the same function (if it does not replace itself during custom inspection ;-) ).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again: functions do seem to be correct as it is not about a specific custom inspect function but about custom inspect functions in general.