-
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
child_process: name anonymous functions #9880
child_process: name anonymous functions #9880
Conversation
} | ||
|
||
if (child.stdout) { | ||
if (encoding) | ||
child.stdout.setEncoding(encoding); | ||
|
||
child.stdout.addListener('data', function(chunk) { |
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.
Why not name the function inline here instead?:
child.stdout.addListener('data', function onChildStdout(chunk) {
or even shorter:
child.stdout.on('data', function onChildStdout(chunk) {
@@ -247,17 +247,18 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
} | |||
|
|||
if (options.timeout > 0) { | |||
timeoutId = setTimeout(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.
Same here, this could be:
timeoutId = setTimeout(function delayedKill() {
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.
👍
} | ||
|
||
if (child.stderr) { | ||
if (encoding) | ||
child.stderr.setEncoding(encoding); | ||
|
||
child.stderr.addListener('data', function(chunk) { |
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.
Ditto
I think arrow functions are fine for the most part, but if they result in the addition of new lines of code, I don't think it's really worth it at that point. Since the purpose is to give better stack traces, just adding a name to the anonymous functions is easiest and requires less change. |
@mscdex Thanks for the feedback, working on making those changes now. One question, var _deprecatedCustomFds = internalUtil.deprecate(function deprecateCustomFds(options) {
options.stdio = options.customFds.map(function(fd) {
return fd === -1 ? 'pipe' : fd;
});
}, 'child_process: options.customFds option is deprecated. ' +
'Use options.stdio instead.'); this first line is way too long. Do you happen to have a recommendation on function name for this particular callback? Or should I just break to a new line to follow linting and leave a verbose function name? |
Without checking with the linter, I'd be fine with something like: const _deprecatedCustomFds =
internalUtil.deprecate(function deprecateCustomFds(options) { or const _deprecatedCustomFds = internalUtil.deprecate(
function deprecateCustomFds(options) { |
2f38dd5
to
f689725
Compare
@mscdex suggested changes implemented. Thanks for your feedback. |
LGTM |
@brad-decker Looks like somebody thought there was something wrong with CI and terminated a whole bunch of jobs, including this one. (I had started a lot of them because: Code & Learn.) Bummer. Oh well. Here's another one: https://ci.nodejs.org/job/node-test-pull-request/5096/ |
'Use options.stdio instead.'); | ||
const _deprecatedCustomFds = internalUtil.deprecate( | ||
function deprecateCustomFds(options) { | ||
options.stdio = options.customFds.map(function(fd) { |
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.
Nit: maybe use an arrow function for this? It should fit in a single line.
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.
@lpinca The goal would be getting rid of the anonymous callback functions, so if we were to go the route of an arrow function it'd be declaring a new const above this declaration that defines the callback function, then passing that const to internalUtil.deprecate. I'm 👍 on doing that, but I want to make sure that that would fit the overall code style/design patterns we're wanting to achieve.
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.
@brad-decker yup, the reason for my comment was that the function was still anonymous.
Maybe it's better to just give it a 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.
I was actually thinking you were talking about changing
function deprecateCustomFds(options) {
To a const, so i misunderstood you. totally missed the call to map.
const mapCustomFds = (fd) => { return fd === -1 ? 'pipe' : fd; };
const _deprecatedCustomFds = internalUtil.deprecate(
function deprecateCustomFds(options) {
options.stdio = options.customFds.map(mapCustomFds);
}, 'child_process: options.customFds option is deprecated. ' +
'Use options.stdio instead.'
);
I wrapped the block for the arrow function in curlys to avoid lint error for no-confusing-arrow. Does this solution look okay to you?
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.
Yeah, I would only remove the return
statement.
const mapCustomFds = (fd) => fd === -1 ? 'pipe' : fd;
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.
when removing the return statement i get the no-confusing-arrow lint rule being thrown complaining about using arrow functions ambigiously with conditional expressions.
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.
Oh ops, sorry so maybe just give the original function the mapCustomFds
name without using the arrow function.
I think it's the cleanest thing to do.
Sorry again for the confusion.
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! Thanks for the feedback. 👍
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.
Addressed in new commit and rebased/squashed
@@ -70,10 +70,10 @@ exports._forkChild = function(fd) { | |||
p.open(fd); | |||
p.unref(); | |||
const control = setupChannel(process, p); | |||
process.on('newListener', function(name) { | |||
process.on('newListener', function newListenerCallback(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.
Feel free to ignore, but perhaps name this onNewListener
. Same with onRemoveListener
.
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.
yeah i think onNewListener
is better naming convention. 👍
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.
resolved in new commit and rebased.
Per nodejs#8913 i've named anonymous callback handlers, This commit is just a first stab at this looking for community feedback on style/naming conventions
f689725
to
0474df9
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.
LGTM! Thank you for the PR and for participating in the code-and-learn!
Thanks @jasnell |
Landed in bbed075. Thank you! |
Refs: nodejs#8913 PR-URL: nodejs#9880 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
Checklist
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
child_process
Description of change
Per #8913 i've named anonymous callback handlers.
I have some mixed styling in here because i'm still going over the message log from #8913 and trying to determine best practices. Is the correct implementation of current anonymous functions just to apply a name to those functions without lifting them out of the function call and using arrow functions?
In particular this file has no arrow functions in it yet, so i would assume we'd want to stick to using:
over
if that's the case is there a preferred naming schema for things like setTimeout? The names on some of these seem to be moving towards the verbose, ex:
Definitely ready to make changes to this to make it compliant.