]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/arrow-parens.md
import 8.4.0 source
[pve-eslint.git] / eslint / docs / rules / arrow-parens.md
CommitLineData
eb39fafa
DC
1# Require parens in arrow function arguments (arrow-parens)
2
3Arrow functions can omit parentheses when they have exactly one parameter. In all other cases the parameter(s) must
4be wrapped in parentheses. This rule enforces the consistent use of parentheses in arrow functions.
5
6## Rule Details
7
8This rule enforces parentheses around arrow function parameters regardless of arity. For example:
9
10```js
11/*eslint-env es6*/
12
13// Bad
14a => {}
15
16// Good
17(a) => {}
18```
19
20Following this style will help you find arrow functions (`=>`) which may be mistakenly included in a condition
21when a comparison such as `>=` was the intent.
22
eb39fafa
DC
23```js
24/*eslint-env es6*/
25
26// Bad
27if (a => 2) {
28}
29
30// Good
31if (a >= 2) {
32}
33```
34
35The rule can also be configured to discourage the use of parens when they are not required:
36
37```js
38/*eslint-env es6*/
39
40// Bad
41(a) => {}
42
43// Good
44a => {}
45```
46
47## Options
48
49This rule has a string option and an object one.
50
51String options are:
52
53* `"always"` (default) requires parens around arguments in all cases.
5422a9cc 54* `"as-needed"` enforces no parens where they can be omitted.
eb39fafa
DC
55
56Object properties for variants of the `"as-needed"` option:
57
58* `"requireForBlockBody": true` modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).
59
60### always
61
62Examples of **incorrect** code for this rule with the default `"always"` option:
63
64```js
65/*eslint arrow-parens: ["error", "always"]*/
66/*eslint-env es6*/
67
68a => {};
69a => a;
70a => {'\n'};
71a.then(foo => {});
72a.then(foo => a);
73a(foo => { if (true) {} });
74```
75
76Examples of **correct** code for this rule with the default `"always"` option:
77
78```js
79/*eslint arrow-parens: ["error", "always"]*/
80/*eslint-env es6*/
81
82() => {};
83(a) => {};
84(a) => a;
85(a) => {'\n'}
86a.then((foo) => {});
87a.then((foo) => { if (true) {} });
88```
89
90#### If Statements
91
92One of the benefits of this option is that it prevents the incorrect use of arrow functions in conditionals:
93
94```js
95/*eslint-env es6*/
96
97var a = 1;
98var b = 2;
99// ...
100if (a => b) {
101 console.log('bigger');
102} else {
103 console.log('smaller');
104}
105// outputs 'bigger', not smaller as expected
106```
107
108The contents of the `if` statement is an arrow function, not a comparison.
109
110If the arrow function is intentional, it should be wrapped in parens to remove ambiguity.
111
112```js
113/*eslint-env es6*/
114
115var a = 1;
116var b = 0;
117// ...
118if ((a) => b) {
119 console.log('truthy value returned');
120} else {
121 console.log('falsey value returned');
122}
123// outputs 'truthy value returned'
124```
125
126The following is another example of this behavior:
127
128```js
129/*eslint-env es6*/
130
131var a = 1, b = 2, c = 3, d = 4;
132var f = a => b ? c: d;
133// f = ?
134```
135
136`f` is an arrow function which takes `a` as an argument and returns the result of `b ? c: d`.
137
138This should be rewritten like so:
139
140```js
141/*eslint-env es6*/
142
143var a = 1, b = 2, c = 3, d = 4;
144var f = (a) => b ? c: d;
145```
146
147### as-needed
148
149Examples of **incorrect** code for this rule with the `"as-needed"` option:
150
151```js
152/*eslint arrow-parens: ["error", "as-needed"]*/
153/*eslint-env es6*/
154
155(a) => {};
156(a) => a;
157(a) => {'\n'};
158a.then((foo) => {});
159a.then((foo) => a);
160a((foo) => { if (true) {} });
ebb53d86
TL
161const f = /** @type {number} */(a) => a + a;
162const g = /* comment */ (a) => a + a;
163const h = (a) /* comment */ => a + a;
eb39fafa
DC
164```
165
166Examples of **correct** code for this rule with the `"as-needed"` option:
167
168```js
169/*eslint arrow-parens: ["error", "as-needed"]*/
170/*eslint-env es6*/
171
172() => {};
173a => {};
174a => a;
175a => {'\n'};
176a.then(foo => {});
177a.then(foo => { if (true) {} });
178(a, b, c) => a;
179(a = 10) => a;
180([a, b]) => a;
181({a, b}) => a;
ebb53d86
TL
182const f = (/** @type {number} */a) => a + a;
183const g = (/* comment */ a) => a + a;
184const h = (a /* comment */) => a + a;
eb39fafa
DC
185```
186
187### requireForBlockBody
188
189Examples of **incorrect** code for the `{ "requireForBlockBody": true }` option:
190
191```js
192/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
193/*eslint-env es6*/
194
195(a) => a;
196a => {};
197a => {'\n'};
198a.map((x) => x * x);
199a.map(x => {
200 return x * x;
201});
202a.then(foo => {});
203```
204
205Examples of **correct** code for the `{ "requireForBlockBody": true }` option:
206
207```js
208/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
209/*eslint-env es6*/
210
211(a) => {};
212(a) => {'\n'};
213a => ({});
214() => {};
215a => a;
216a.then((foo) => {});
217a.then((foo) => { if (true) {} });
218a((foo) => { if (true) {} });
219(a, b, c) => a;
220(a = 10) => a;
221([a, b]) => a;
222({a, b}) => a;
223```
224
225## Further Reading
226
227* The `"as-needed", { "requireForBlockBody": true }` rule is directly inspired by the Airbnb
228 [JS Style Guide](https://github.com/airbnb/javascript#arrows--one-arg-parens).