-
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: improve inspect for (Async|Generator)Function #11210
Conversation
This allows us to use async functions.
lib/util.js
Outdated
'special'); | ||
const asyncPrefix = binding.isAsyncFunction(value) ? 'async ' : ''; | ||
return ctx.stylize( | ||
`[${asyncPrefix}Function${value.name ? `: ${value.name}` : ''}]`, |
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.
(async function() {}).constructor.name === 'AsyncFunction'
, so I’d probably want to go with that? Or just generally actually use the constructor name, if it’s available?
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.
Maybe. I didn't think about that. We don't do it for GeneratorFunction
though. Should I extend the PR to support it too?
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 mean, if I change the code to use constructor.name, this will be semver-major. I'm OK with 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.
Style nit: indent by four spaces.
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.
this will be semver-major
You mean, because it changes the output for GeneratorFunction
? Yeah, that’s probably for the best. And yes, I think extending it to GeneratorFunction
s would be a good idea, too.
test/parallel/test-util-inspect.js
Outdated
@@ -9,6 +9,7 @@ assert.strictEqual(util.inspect(false), 'false'); | |||
assert.strictEqual(util.inspect(''), "''"); | |||
assert.strictEqual(util.inspect('hello'), "'hello'"); | |||
assert.strictEqual(util.inspect(function() {}), '[Function]'); | |||
assert.strictEqual(util.inspect(async function() {}), '[async Function]'); |
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.
The original uses the capitalized constructor name (Function
). How about using the same format, AsyncFunction
?
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.
Seems a bit redundant: async AsyncFunction
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.
LGTM with a style nit.
lib/util.js
Outdated
'special'); | ||
const asyncPrefix = binding.isAsyncFunction(value) ? 'async ' : ''; | ||
return ctx.stylize( | ||
`[${asyncPrefix}Function${value.name ? `: ${value.name}` : ''}]`, |
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.
Style nit: indent by four spaces.
test/parallel/test-util-inspect.js
Outdated
@@ -9,6 +9,7 @@ assert.strictEqual(util.inspect(false), 'false'); | |||
assert.strictEqual(util.inspect(''), "''"); | |||
assert.strictEqual(util.inspect('hello'), "'hello'"); | |||
assert.strictEqual(util.inspect(function() {}), '[Function]'); | |||
assert.strictEqual(util.inspect(async function() {}), '[async Function]'); |
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.
Seems a bit redundant: async AsyncFunction
@bnoordhuis I'm changing the PR to get this output:
What do you think? |
Seems fine to me. |
1e28f15
to
cdf7faf
Compare
OK, updated. Marking semver-major because the output changes for generator functions. |
lib/util.js
Outdated
@@ -403,8 +403,9 @@ function formatValue(ctx, value, recurseTimes) { | |||
// Some type of object without properties can be shortcutted. | |||
if (keys.length === 0) { | |||
if (typeof value === 'function') { | |||
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`, | |||
'special'); | |||
const cName = value.constructor ? value.constructor.name : 'Function'; |
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.
There’s a getConstructorOf
utility inside this file for dealing with some edge cases :)
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.
Aha! Thanks, updated.
cdf7faf
to
88920b5
Compare
lib/util.js
Outdated
@@ -403,8 +403,9 @@ function formatValue(ctx, value, recurseTimes) { | |||
// Some type of object without properties can be shortcutted. | |||
if (keys.length === 0) { | |||
if (typeof value === 'function') { | |||
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`, | |||
'special'); | |||
const cName = getConstructorOf(value).name; |
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.
Sorry to comment again 😄 I’d just move the constructor
variable up before the keys.length
block and do the same constructor && constructor.name …
check that is used elsewhere in this file. I doubt there are many people who set their functions’ prototypes to null
, but I’d prefer handling these consistently…
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.
No problem :) Updated again.
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.
LGTM with @addaleax's suggestion
@targos with this being a semver-major change, do you have plans to open a PR to do this for async functions in v7 as well (without the generator function change)? |
@evanlucas I can do that |
88920b5
to
686e91a
Compare
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.
Thanks for bearing with me. LGTM! :)
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.
LGTM with style nits.
@@ -400,11 +400,14 @@ function formatValue(ctx, value, recurseTimes) { | |||
}); | |||
} | |||
|
|||
var constructor = getConstructorOf(value); |
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.
Maybe const
it while you're here. (EDIT: nevermind, I see it's being reassigned in some places.)
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 can't make it const because the value may be changed later.
lib/util.js
Outdated
// Some type of object without properties can be shortcutted. | ||
if (keys.length === 0) { | ||
if (typeof value === 'function') { | ||
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`, | ||
'special'); | ||
const cName = constructor ? constructor.name : 'Function'; |
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 have an irrational dislike for the name cName
. constructorName
or ctorName
if the former is too long?
(Applies to line 541 as well.)
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.
Changed to ctorName
lib/util.js
Outdated
'special'); | ||
const cName = constructor ? constructor.name : 'Function'; | ||
return ctx.stylize( | ||
`[${cName}${value.name ? `: ${value.name}` : ''}]`, 'special'); |
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.
Four spaces, please. :-)
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.
Done
Use the constructor name in the output, if present.
686e91a
to
c2fac57
Compare
Use the constructor name in the output, if present. This is a backport of nodejs#11210 without the semver-major change to GeneratorFunction output.
PR to v7.x-staging: #11211 |
'special'); | ||
const ctorName = constructor ? constructor.name : 'Function'; | ||
return ctx.stylize( | ||
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special'); |
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.
Nested templates can be a little bit hard to read, maybe change it to use if-else? (though this LTGM too with this unchanged, now that there is a backport PR already)
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.
Thanks a lot. LGTM.
This allows us to use async functions. PR-URL: #11210 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Use the constructor name in the output, if present. PR-URL: #11210 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Landed in 615789b...f65aa08 |
Use the constructor name in the output, if present. This is a backport of #11210 without the semver-major change to GeneratorFunction output. PR-URL: #11211 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Use the constructor name in the output, if present. This is a backport of nodejs#11210 without the semver-major change to GeneratorFunction output. PR-URL: nodejs#11211 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
This allows us to use async functions. PR-URL: nodejs#11210 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Use the constructor name in the output, if present. PR-URL: nodejs#11210 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util