Skip to content

Commit 4499ee0

Browse files
fvgsljharb
authored andcommitted
[guide] No arrow function implicit return with side effects
1 parent c43fc74 commit 4499ee0

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ Other Style Guides
369369
```
370370
371371
<a name="arrays--callback-return"></a><a name="4.5"></a>
372-
- [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)
372+
- [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)
373373
374374
```javascript
375375
// good
@@ -915,7 +915,7 @@ Other Style Guides
915915
```
916916
917917
<a name="arrows--implicit-return"></a><a name="8.2"></a>
918-
- [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)
918+
- [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)
919919
920920
> Why? Syntactic sugar. It reads well when multiple functions are chained together.
921921
@@ -939,6 +939,24 @@ Other Style Guides
939939
[1, 2, 3].map((number, index) => ({
940940
[index]: number,
941941
}));
942+
943+
// No implicit return with side effects
944+
function foo(callback) {
945+
const val = callback();
946+
if (val === true) {
947+
// Do something if callback returns true
948+
}
949+
}
950+
951+
let bool = false;
952+
953+
// bad
954+
foo(() => bool = true);
955+
956+
// good
957+
foo(() => {
958+
bool = true;
959+
}
942960
```
943961
944962
<a name="arrows--paren-wrap"></a><a name="8.3"></a>
@@ -1354,7 +1372,9 @@ Other Style Guides
13541372
13551373
// good
13561374
let sum = 0;
1357-
numbers.forEach(num => sum += num);
1375+
numbers.forEach((num) => {
1376+
sum += num;
1377+
);
13581378
sum === 15;
13591379
13601380
// best (use the functional force)
@@ -1369,7 +1389,9 @@ Other Style Guides
13691389
13701390
// good
13711391
const increasedByOne = [];
1372-
numbers.forEach(num => increasedByOne.push(num + 1));
1392+
numbers.forEach((num) => {
1393+
increasedByOne.push(num + 1);
1394+
);
13731395
13741396
// best (keeping it functional)
13751397
const increasedByOne = numbers.map(num => num + 1);

0 commit comments

Comments
 (0)