From 4499ee0094239c1e02506ee30c2a3945aa032591 Mon Sep 17 00:00:00 2001 From: Felipe Vargas Date: Mon, 12 Jun 2017 15:41:11 -0700 Subject: [PATCH] [guide] No arrow function implicit return with side effects --- README.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 69a7b998..8ff1a8f2 100644 --- a/README.md +++ b/README.md @@ -369,7 +369,7 @@ Other Style Guides ``` - - [4.5](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](http://eslint.org/docs/rules/array-callback-return) + - [4.5](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](http://eslint.org/docs/rules/array-callback-return) ```javascript // good @@ -915,7 +915,7 @@ Other Style Guides ``` - - [8.2](#arrows--implicit-return) 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. eslint: [`arrow-parens`](http://eslint.org/docs/rules/arrow-parens.html), [`arrow-body-style`](http://eslint.org/docs/rules/arrow-body-style.html) jscs: [`disallowParenthesesAroundArrowParam`](http://jscs.info/rule/disallowParenthesesAroundArrowParam), [`requireShorthandArrowFunctions`](http://jscs.info/rule/requireShorthandArrowFunctions) + - [8.2](#arrows--implicit-return) If the function body consists of a single statement returning an [expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) without side effects, omit the braces and use the implicit return. Otherwise, keep the braces and use a `return` statement. eslint: [`arrow-parens`](http://eslint.org/docs/rules/arrow-parens.html), [`arrow-body-style`](http://eslint.org/docs/rules/arrow-body-style.html) jscs: [`disallowParenthesesAroundArrowParam`](http://jscs.info/rule/disallowParenthesesAroundArrowParam), [`requireShorthandArrowFunctions`](http://jscs.info/rule/requireShorthandArrowFunctions) > Why? Syntactic sugar. It reads well when multiple functions are chained together. @@ -939,6 +939,24 @@ Other Style Guides [1, 2, 3].map((number, index) => ({ [index]: number, })); + + // No implicit return with side effects + function foo(callback) { + const val = callback(); + if (val === true) { + // Do something if callback returns true + } + } + + let bool = false; + + // bad + foo(() => bool = true); + + // good + foo(() => { + bool = true; + } ``` @@ -1354,7 +1372,9 @@ Other Style Guides // good let sum = 0; - numbers.forEach(num => sum += num); + numbers.forEach((num) => { + sum += num; + ); sum === 15; // best (use the functional force) @@ -1369,7 +1389,9 @@ Other Style Guides // good const increasedByOne = []; - numbers.forEach(num => increasedByOne.push(num + 1)); + numbers.forEach((num) => { + increasedByOne.push(num + 1); + ); // best (keeping it functional) const increasedByOne = numbers.map(num => num + 1);