From bc9faf6fec80e7f4cfac347c433aad44991ecd74 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Thu, 12 Nov 2015 08:14:26 +0100 Subject: [PATCH 1/5] Bring back note about braces --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7aa0062a7e..a8c2eafa86 100644 --- a/README.md +++ b/README.md @@ -699,7 +699,7 @@ Other Style Guides }); ``` - - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise use a `return` statement. + - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise add the parentheses, braces, and use a `return` statement. > Why? Syntactic sugar. It reads well when multiple functions are chained together. From 32ec9dd03a62b520292486d21b247f7ef03950aa Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Wed, 2 Dec 2015 11:52:08 +0100 Subject: [PATCH 2/5] Make 8.2 and 8.4 simpler and more explicit --- README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a8c2eafa86..a99558ec96 100644 --- a/README.md +++ b/README.md @@ -699,7 +699,7 @@ Other Style Guides }); ``` - - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise add the parentheses, braces, and use a `return` statement. + - [8.2](#8.2) If the function body consists of a single expression, omit the braces and use the implicit return. Otherwise, keep the braces and use a `return` statement. > Why? Syntactic sugar. It reads well when multiple functions are chained together. @@ -743,18 +743,30 @@ Other Style Guides ``` - - [8.4](#8.4) If your function only takes a single argument, feel free to omit the parentheses. + - [8.4](#8.4) If your function takes a single argument and consists of a single expression, omit the parentheses. > Why? Less visual clutter. eslint rules: [`arrow-parens`](http://eslint.org/docs/rules/arrow-parens.html). ```js + // bad + [1, 2, 3].map((x) => x * x); + // good [1, 2, 3].map(x => x * x); + // bad + [1, 2, 3].reduce(x => { + const y = x + 1; + return x * y; + }); + // good - [1, 2, 3].reduce((y, x) => x + y); + [1, 2, 3].reduce((x) => { + const y = x + 1; + return x * y; + }); ``` **[⬆ back to top](#table-of-contents)** From b54ce438c8e590ae25154277d69a802d247a28d1 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Wed, 2 Dec 2015 11:58:01 +0100 Subject: [PATCH 3/5] Clarify 8.4 --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a99558ec96..73ed24fc8b 100644 --- a/README.md +++ b/README.md @@ -743,7 +743,7 @@ Other Style Guides ``` - - [8.4](#8.4) If your function takes a single argument and consists of a single expression, omit the parentheses. + - [8.4](#8.4) If your function takes a single argument and doesn’t use braces, omit the parentheses. > Why? Less visual clutter. @@ -756,6 +756,12 @@ Other Style Guides // good [1, 2, 3].map(x => x * x); + // good + [1, 2, 3].map(number => ( + `A long string with the ${number}. It’s so long that we’ve broken it ` + + 'over multiple lines!' + )); + // bad [1, 2, 3].reduce(x => { const y = x + 1; From 766f3585db359b93e77d441a5a1a2deddfbef506 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Wed, 2 Dec 2015 11:58:26 +0100 Subject: [PATCH 4/5] Fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73ed24fc8b..94a1ec9a7d 100644 --- a/README.md +++ b/README.md @@ -763,13 +763,13 @@ Other Style Guides )); // bad - [1, 2, 3].reduce(x => { + [1, 2, 3].map(x => { const y = x + 1; return x * y; }); // good - [1, 2, 3].reduce((x) => { + [1, 2, 3].map((x) => { const y = x + 1; return x * y; }); From 4ca5764a306365a11a83e8dfd533b754ed9f65b0 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Mon, 4 Jan 2016 20:35:37 +0100 Subject: [PATCH 5/5] Require parentheses As suggested by @ljharb in https://github.com/airbnb/javascript/pull/579#issuecomment-166981740 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 94a1ec9a7d..cebc053bcc 100644 --- a/README.md +++ b/README.md @@ -743,7 +743,7 @@ Other Style Guides ``` - - [8.4](#8.4) If your function takes a single argument and doesn’t use braces, omit the parentheses. + - [8.4](#8.4) If your function takes a single argument and doesn’t use braces, omit the parentheses. Otherwise, always include parentheses around arguments. > Why? Less visual clutter.