Closed leesei closed 5 years ago
Actually, I revise my position. I'm ambivalent towards this.
I personally dislike non-void implicit returns
@skeggse I was talking not of any personal dislike but of a feature introduced in ES6 to promote brevity and better FP-style code in JS. The "mandatory curly braces in arrow function body" rule is a judgment that that feature shouldn't have been allowed.
That's what I was responding to, yes.
first rule doesn't really matter writing array.map((x) => x*x);
isn't the worst
2 and 3 can be solved by allowing one liners but not sure if the maintainers are pro for this
That said, the current style also avoided the pitfall of returning undefined
upon missing parenthesis around object.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Returning_object_literals
var func = () => { foo: 1 }; // func() returns `undefined`
var func = () => ({ foo: 1 }); // func() returns `{ foo: 1 }`
That is probably one of the reasons for it, there was an issue relating the es6 styleguide discussion, let me see if I can find it
the current style also avoided the pitfall of returning
undefined
upon missing parenthesis
@leesei I would argue that's the programmer's burden to ensure.
@AdriVanHoudt Thanks for the issue link. It doesn't seem to make it very clear if there was a real consensus on this rule. I would love to have a renewed discussion around it.
@prashaantt we are having it right now :P the other issue was to provide context on what was already being said about the topic
The conclusion I see in #58 seems to allow for one-liners with implicit returns: https://github.com/hapijs/contrib/issues/58#issuecomment-151287951
@AdriVanHoudt Well then, what @devinivy said :)
I'll just repeat myself that disallowing single line implicit returns for violation of an arbitrary style rule diminishes the language. We should revoke that rule.
This is the revised guide for arrow function reflecting https://github.com/hapijs/contrib/issues/58#issuecomment-151287951
#### Function declaration
- Declare functions via assignment
- Arrow function arguments must be enclosed in parentheses
- Arrow function bodies must be enclosed in curly braces (except for single line [concise body](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Function_body))
- No function creation in concise body
- Object returned in concise body must be enclosed in parentheses
"Object returned in concise body must be enclosed in parentheses" is a syntax requirement. I'm not sure if it should be included in style guide.
In https://github.com/hapijs/contrib/issues/58#issuecomment-151287951, "parentheses around object creation allowed ((foo) => ({ bar: foo }))
including line breaks in the {}
" means that the concise body does not need to be in single line, and the style for object creation should be respected.
Any comment on "single-line-ness"?
Arrow function will return concise body (single expression without block) by default. This feature makes inline mapper function really elegant. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Function_body
However these two rule forbids usage of concise body:
EDIT: use term 'concise body', revised Right code