-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Docs: Javascript style guide - multi-line arrow functions are okay without braces #9203
Comments
I think we should distinguish the two cases... In my opinion this is good: list.map( item =>
<ul>
<li>{ item.name }</li>
</ul>
); because the single root element provides the same visual help of a couple of parentheses. On the other hand the first example can't work correctly without parentheses. The parentheses are needed to help the engine distinguish the object literal
That is, now that people are getting more and more familiar with ES2015, to return an object literal by wrapping it into parentheses is becoming an idiomatic form. |
I'm actually for using braces on multi-line. list.map( item =>
<li>
<span>Name: { item.name }</span>
</li>
); I still like to add the parens: list.map( item => (
<li>
<span>Name: { item.name }</span>
</li>
) ); I don't feel too strongly about this as I do find this opinion hard to defend. list.map( item => {
const name = capitalize(item.name);
return (
<li>
<span>Name: { name }</span>
</li>
);
) );
Also AirBnB recommends using parens for multi-liners. Testing the following on chrome & FF actually works fine but I still don't completely trust ASI not to error when omitting the parens (and not using babel). a = b =>
1
console.log( a() ) // 1 Still I feel like there's room for extra clarity here - I'd vote for adding wrapping parens to any multi-line implicitly returning arrow functions (MLIRAFs for short 😂) I've got a PR coming with a couple of edits to the JS guidelines as I didn't realise there was one in progress (I think it's merged now though). |
@spen you've a good point on consistency between implicit and explicit |
#9037 also removed the text from the original comment that referred to the arrow function usage as being "bad". To me personally, specific usage of arrow functions falls on the side of developer preference, not warranting an explicit guideline one way or the other. |
Closing because I feel this issue was resolved in #9037. |
This issue has been marked as stale because it hasn't been updated in a while. It will be closed in a week. |
As sparked in a conversation here, @spen had the idea that we should revise our javascript style guide to allow for multiline arrow functions without braces.
The current style guide explicitly says:
But it is becoming an increasingly common pattern to utilize brace-less multiline arrow functions.
What is everyones thoughts ? Is it a good idea to update the docs and allow items like so:
The text was updated successfully, but these errors were encountered: