Skip to content

Commit cf57435

Browse files
sharmilajesupaulljharb
authored andcommitted
[guide] [eslint config] [base] [breaking] Require parens for arrow function args
1 parent 064e3bf commit cf57435

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

README.md

+27-19
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) => {
@@ -957,13 +957,16 @@ Other Style Guides
957957
958958
```javascript
959959
// bad
960-
[1, 2, 3].map(number => {
960+
[1, 2, 3].map((number) => {
961961
const nextNumber = number + 1;
962962
`A string containing the ${nextNumber}.`;
963963
});
964964

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

968971
// good
969972
[1, 2, 3].map((number) => {
@@ -1002,14 +1005,14 @@ Other Style Guides
10021005
10031006
```javascript
10041007
// bad
1005-
['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
1008+
['get', 'post', 'put'].map((httpMethod) => Object.prototype.hasOwnProperty.call(
10061009
httpMagicObjectWithAVeryLongName,
10071010
httpMethod,
10081011
)
10091012
);
10101013

10111014
// good
1012-
['get', 'post', 'put'].map(httpMethod => (
1015+
['get', 'post', 'put'].map((httpMethod) => (
10131016
Object.prototype.hasOwnProperty.call(
10141017
httpMagicObjectWithAVeryLongName,
10151018
httpMethod,
@@ -1018,22 +1021,27 @@ Other Style Guides
10181021
```
10191022
10201023
<a name="arrows--one-arg-parens"></a><a name="8.4"></a>
1021-
- [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)
1024+
- [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)
10221025
1023-
> Why? Less visual clutter.
1026+
> Why? Minimizes diff churn when adding or removing arguments.
10241027
10251028
```javascript
10261029
// bad
1027-
[1, 2, 3].map((x) => x * x);
1028-
1029-
// good
10301030
[1, 2, 3].map(x => x * x);
10311031

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

1040+
// good
1041+
[1, 2, 3].map((number) => (
1042+
`A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
1043+
));
1044+
10371045
// bad
10381046
[1, 2, 3].map(x => {
10391047
const y = x + 1;
@@ -1052,13 +1060,13 @@ Other Style Guides
10521060
10531061
```javascript
10541062
// bad
1055-
const itemHeight = item => item.height <= 256 ? item.largeSize : item.smallSize;
1063+
const itemHeight = (item) => item.height <= 256 ? item.largeSize : item.smallSize;
10561064

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

10601068
// good
1061-
const itemHeight = item => (item.height <= 256 ? item.largeSize : item.smallSize);
1069+
const itemHeight = (item) => (item.height <= 256 ? item.largeSize : item.smallSize);
10621070

10631071
// good
10641072
const itemHeight = (item) => {
@@ -1072,16 +1080,16 @@ Other Style Guides
10721080
10731081
```javascript
10741082
// bad
1075-
foo =>
1083+
(foo) =>
10761084
bar;
10771085

1078-
foo =>
1086+
(foo) =>
10791087
(bar);
10801088

10811089
// good
1082-
foo => bar;
1083-
foo => (bar);
1084-
foo => (
1090+
(foo) => bar;
1091+
(foo) => (bar);
1092+
(foo) => (
10851093
bar
10861094
)
10871095
```
@@ -1450,7 +1458,7 @@ Other Style Guides
14501458
});
14511459
14521460
// best (keeping it functional)
1453-
const increasedByOne = numbers.map(num => num + 1);
1461+
const increasedByOne = numbers.map((num) => num + 1);
14541462
```
14551463

14561464
<a name="generators--nope"></a><a name="11.2"></a>
@@ -2976,7 +2984,7 @@ Other Style Guides
29762984
// bad - raises exception
29772985
const luke = {}
29782986
const leia = {}
2979-
[luke, leia].forEach(jedi => jedi.father = 'vader')
2987+
[luke, leia].forEach((jedi) => jedi.father = 'vader')
29802988

29812989
// bad - raises exception
29822990
const reaction = "No! That’s impossible!"

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

+1-3
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

+1-1
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

+1-1
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)