-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: deprecate obj.inspect for custom inspection #15631
Conversation
doc/api/util.md
Outdated
@@ -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, equivalently but deprecated, `inspect(depth, opts)`) function that |
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.
not sure, but maybe "equivalent but deprecated"?
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.
Yes, you're correct. Even better might be or the equivalent but deprecated
which is what I'll switch it to in a minute.
Just to give context, the alternative was only introduced a little more than a year ago in 6.4.0. I am perfectly fine with landing this in 8.x but I’d like to wait a little longer until we consider a runtime-deprecation. |
Labeling |
can we check ecosystem usage of this before we land this? |
@evanlucas The same question was asked in #15549 and @refack answered (in #15549 (comment)) like this:
|
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.
+1 as a docs-only deprecation
querying for |
4c7ecb6
to
37b9faa
Compare
Can someone confirm; will |
Yes, the string inspect will still work. This is a docs only deprecation for now. Which means no runtime changes at all |
doc/api/deprecations.md
Outdated
|
||
Using a property named `inspect` on an object to specify a custom inspection | ||
function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][] | ||
instead. |
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.
It might be helpful to note that, if compatibility is desired, having the method present under both names is always going to work?
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.
I like the idea but I'm struggling with how to word it in a way that doesn't encourage people to use both if they're only using the Symbol. We don't want to create more messages if/when we move to a runtime deprecation. Suggestions welcome.
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.
@Trott The thing is, even if we move to a runtime deprecation, the symbol property will be the first one that’s detected, and the string property ignored – so if both are present, the runtime deprecation won’t be emitted… or am I misunderstanding what you’re saying?
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.
@addaleax Good point. I've added some text in a separate commit.
AFAICT the long term "plan" is to runtime deprecate then remove, but that cycle requires at least two major versions, and future consensus. |
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. Refs: nodejs#15549
37b9faa
to
5dd64ef
Compare
5dd64ef
to
1a791e8
Compare
doc/api/deprecations.md
Outdated
@@ -707,7 +707,8 @@ 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. | |||
instead. (For backwards compatibility with Node.js prior to version 6.4.0, both | |||
may be specified.) |
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.
instead (For backwards compatibility with Node.js prior to version 6.4.0, both may be specified).
?
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.
I think since it's a whole sentence (not just a clause) inside the parentheses the period should be inside.
http://www.thepunctuationguide.com/parentheses.html
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.
Perhaps the parentheses are unnecessary anyway?
Landed in c09c04f I removed the braces while landing. |
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>
This does not land cleanly on 8.x. Could you please manually backport. |
nvm I figured it out |
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>
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>
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: nodejs/node#15631 Refs: nodejs/node#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>
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>
Notable Changes: * deps: * update npm to 5.4.2 #15600 * upgrade libuv to 1.15.0 #15745 * update V8 to 6.1.534.42 #15393 * dgram: * support for setting dgram socket buffer size #13623 * fs: * add support O_DSYNC file open constant #15451 * util: * deprecate obj.inspect for custom inspection #15631 * tools, build: * there is a fancy new macOS installer #15179 * Added new collaborator * bmeurer - Benedikt Meurer - https://github.com/bmeurer * kfarnung - Kyle Farnung - https://github.com/kfarnung PR-URL: #15762
Notable Changes: * deps: * update npm to 5.4.2 #15600 * upgrade libuv to 1.15.0 #15745 * update V8 to 6.1.534.42 #15393 * dgram: * support for setting dgram socket buffer size #13623 * fs: * add support O_DSYNC file open constant #15451 * util: * deprecate obj.inspect for custom inspection #15631 * tools, build: * there is a fancy new macOS installer #15179 * Added new collaborator * bmeurer - Benedikt Meurer - https://github.com/bmeurer * kfarnung - Kyle Farnung - https://github.com/kfarnung PR-URL: #15762
Notable Changes: * deps: * update npm to 5.4.2 nodejs/node#15600 * upgrade libuv to 1.15.0 nodejs/node#15745 * update V8 to 6.1.534.42 nodejs/node#15393 * dgram: * support for setting dgram socket buffer size nodejs/node#13623 * fs: * add support O_DSYNC file open constant nodejs/node#15451 * util: * deprecate obj.inspect for custom inspection nodejs/node#15631 * tools, build: * there is a fancy new macOS installer nodejs/node#15179 * Added new collaborator * bmeurer - Benedikt Meurer - https://github.com/bmeurer * kfarnung - Kyle Farnung - https://github.com/kfarnung PR-URL: nodejs/node#15762
I saw the change, read documentation and played with the Node and the code, and still cannot quite get it... In order to provide custom console output for each of my custom types I used to do this: MyType.prototype.inspect = function(){
return 'CUSTOM VALUE';
} How am I to change this code for the latest Node.js? |
@vitaly-t basically, whenever you want your custom inspect function and care about older Node versions, also do: MyType.prototype.inspect = function(){
return 'CUSTOM VALUE';
};
if (util.inspect.custom) {
MyType.prototype[util.inspect.custom] = MyType.prototype.inspect;
} If you do not care about Node < 6.4.0, you can (and probably should) just use MyType.prototype[util.inspect.custom] = function(){
return 'CUSTOM VALUE';
}; The reason for this is that it’s hard to tell for Node whether |
@addaleax Thank you! But if this is the case, then why the first code below works fine, while the one after doesn't? Works: const obj = {
inspect: () => {
return 'INSPECT';
}
};
console.log(obj);
//=> INSPECT Doesn't work: const obj = {
util: {
inspect: {
custom: () => {
return 'INSPECT';
}
}
}
};
console.log(obj);
//=> { util: { inspect: { custom: [Function: custom] } } } Node.js v8.7.0 |
@vitaly-t it's const obj = {
[require('util').inspect.custom]() {
return 'INSPECT';
}
};
console.log(obj); |
@ljharb Ouch,... tricky! I think documentation needs to get better on this one 😄 Thank you! Also, I'd like to figure out what that |
PRs are definitely very welcome!
I am not sure if this is helpful or not, but: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol (i.e. there isn’t really much more information contained in the value of |
Release team were -1 on landing on v6.x, if you disagree let us know. |
@gibfahn on landing the deprecation, the new symbol, or either? |
On landing the deprecation, though it wasn't a heavy -1, as with all backports we'd like to hear others' thoughts (we have a week to decide). Which is the new symbol? I don't see it in this PR. |
The existence of
obj.inspect()
for custom inspection can cause peopleto unintentionally break
console.log()
and friends. This is adocumentation-only deprecation that can hopefully land in 8.x.
Refs: #15549
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util