Skip to content

Commit 820745d

Browse files
sharmilajesupaulljharb
authored andcommitted
[guide] [eslint config] [base] [breaking] Require parens for arrow function args
1 parent 8eacf24 commit 820745d

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

README.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ Other Style Guides
418418
});
419419

420420
// good
421-
[1, 2, 3].map(x => x + 1);
421+
[1, 2, 3].map((x) => x + 1);
422422

423423
// bad - no returned value means `acc` becomes undefined after the first iteration
424424
[[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
@@ -955,13 +955,16 @@ Other Style Guides
955955
956956
```javascript
957957
// bad
958-
[1, 2, 3].map(number => {
958+
[1, 2, 3].map((number) => {
959959
const nextNumber = number + 1;
960960
`A string containing the ${nextNumber}.`;
961961
});
962962

963+
// bad
964+
[1, 2, 3].map((number) => `A string containing the ${number}.`);
965+
963966
// good
964-
[1, 2, 3].map(number => `A string containing the ${number + 1}.`);
967+
[1, 2, 3].map((number) => `A string containing the ${number + 1}.`);
965968

966969
// good
967970
[1, 2, 3].map((number) => {
@@ -1000,14 +1003,14 @@ Other Style Guides
10001003
10011004
```javascript
10021005
// bad
1003-
['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
1006+
['get', 'post', 'put'].map((httpMethod) => Object.prototype.hasOwnProperty.call(
10041007
httpMagicObjectWithAVeryLongName,
10051008
httpMethod,
10061009
)
10071010
);
10081011

10091012
// good
1010-
['get', 'post', 'put'].map(httpMethod => (
1013+
['get', 'post', 'put'].map((httpMethod) => (
10111014
Object.prototype.hasOwnProperty.call(
10121015
httpMagicObjectWithAVeryLongName,
10131016
httpMethod,
@@ -1016,22 +1019,27 @@ Other Style Guides
10161019
```
10171020
10181021
<a name="arrows--one-arg-parens"></a><a name="8.4"></a>
1019-
- [8.4](#arrows--one-arg-parens) If your function takes a single argument and doesn’t use braces, omit the parentheses. Otherwise, always include parentheses around arguments for clarity and consistency. Note: it is also acceptable to always use parentheses, in which case use the [“always” option](https://eslint.org/docs/rules/arrow-parens#always) for eslint. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens.html)
1022+
- [8.4](#arrows--one-arg-parens) Always include parentheses around arguments for clarity and consistency. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens.html)
10201023
1021-
> Why? Less visual clutter.
1024+
> Why? Minimizes diff churn when adding or removing arguments.
10221025
10231026
```javascript
10241027
// bad
1025-
[1, 2, 3].map((x) => x * x);
1026-
1027-
// good
10281028
[1, 2, 3].map(x => x * x);
10291029

10301030
// good
1031+
[1, 2, 3].map((x) => x * x);
1032+
1033+
// bad
10311034
[1, 2, 3].map(number => (
10321035
`A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
10331036
));
10341037

1038+
// good
1039+
[1, 2, 3].map((number) => (
1040+
`A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
1041+
));
1042+
10351043
// bad
10361044
[1, 2, 3].map(x => {
10371045
const y = x + 1;
@@ -1050,13 +1058,13 @@ Other Style Guides
10501058
10511059
```javascript
10521060
// bad
1053-
const itemHeight = item => item.height <= 256 ? item.largeSize : item.smallSize;
1061+
const itemHeight = (item) => item.height <= 256 ? item.largeSize : item.smallSize;
10541062

10551063
// bad
10561064
const itemHeight = (item) => item.height >= 256 ? item.largeSize : item.smallSize;
10571065

10581066
// good
1059-
const itemHeight = item => (item.height <= 256 ? item.largeSize : item.smallSize);
1067+
const itemHeight = (item) => (item.height <= 256 ? item.largeSize : item.smallSize);
10601068

10611069
// good
10621070
const itemHeight = (item) => {
@@ -1070,16 +1078,16 @@ Other Style Guides
10701078
10711079
```javascript
10721080
// bad
1073-
foo =>
1081+
(foo) =>
10741082
bar;
10751083

1076-
foo =>
1084+
(foo) =>
10771085
(bar);
10781086

10791087
// good
1080-
foo => bar;
1081-
foo => (bar);
1082-
foo => (
1088+
(foo) => bar;
1089+
(foo) => (bar);
1090+
(foo) => (
10831091
bar
10841092
)
10851093
```
@@ -1448,7 +1456,7 @@ Other Style Guides
14481456
});
14491457
14501458
// best (keeping it functional)
1451-
const increasedByOne = numbers.map(num => num + 1);
1459+
const increasedByOne = numbers.map((num) => num + 1);
14521460
```
14531461

14541462
<a name="generators--nope"></a><a name="11.2"></a>
@@ -3028,7 +3036,7 @@ Other Style Guides
30283036
// bad - raises exception
30293037
const luke = {}
30303038
const leia = {}
3031-
[luke, leia].forEach(jedi => jedi.father = 'vader')
3039+
[luke, leia].forEach((jedi) => jedi.father = 'vader')
30323040

30333041
// bad - raises exception
30343042
const reaction = "No! That’s impossible!"

packages/eslint-config-airbnb-base/rules/es6.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ module.exports = {
2121

2222
// require parens in arrow function arguments
2323
// https://eslint.org/docs/rules/arrow-parens
24-
'arrow-parens': ['error', 'as-needed', {
25-
requireForBlockBody: true,
26-
}],
24+
'arrow-parens': ['error', 'always'],
2725

2826
// require space before/after arrow function's arrow
2927
// https://eslint.org/docs/rules/arrow-spacing

packages/eslint-config-airbnb-base/test/test-base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Object.keys(files).forEach((
2626

2727
// scan rules for react/ and fail if any exist
2828
const reactRuleIds = Object.keys(config.rules)
29-
.filter(ruleId => ruleId.indexOf('react/') === 0);
29+
.filter((ruleId) => ruleId.indexOf('react/') === 0);
3030
t.deepEquals(reactRuleIds, [], 'there are no react/ rules');
3131
});
3232
});

packages/eslint-config-airbnb/test/test-base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Object.keys(files).forEach((name) => {
2828

2929
// scan rules for react/ and fail if any exist
3030
const reactRuleIds = Object.keys(config.rules)
31-
.filter(ruleId => ruleId.indexOf('react/') === 0);
31+
.filter((ruleId) => ruleId.indexOf('react/') === 0);
3232
t.deepEquals(reactRuleIds, [], 'there are no react/ rules');
3333
});
3434
});

0 commit comments

Comments
 (0)