]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-extra-parens.md
import and build new upstream release 7.2.0
[pve-eslint.git] / eslint / docs / rules / no-extra-parens.md
CommitLineData
eb39fafa
DC
1# disallow unnecessary parentheses (no-extra-parens)
2
3This rule restricts the use of parentheses to only where they are necessary.
4
5## Rule Details
6
7This rule always ignores extra parentheses around the following:
8
9* RegExp literals such as `(/abc/).test(var)` to avoid conflicts with the [wrap-regex](wrap-regex.md) rule
d3726936 10* immediately-invoked function expressions (also known as IIFEs) such as `var x = (function () {})();` and `var x = (function () {}());` to avoid conflicts with the [wrap-iife](wrap-iife.md) rule
eb39fafa
DC
11* arrow function arguments to avoid conflicts with the [arrow-parens](arrow-parens.md) rule
12
13## Options
14
15This rule has a string option:
16
17* `"all"` (default) disallows unnecessary parentheses around *any* expression
18* `"functions"` disallows unnecessary parentheses *only* around function expressions
19
20This rule has an object option for exceptions to the `"all"` option:
21
22* `"conditionalAssign": false` allows extra parentheses around assignments in conditional test expressions
23* `"returnAssign": false` allows extra parentheses around assignments in `return` statements
24* `"nestedBinaryExpressions": false` allows extra parentheses in nested binary expressions
25* `"ignoreJSX": "none|all|multi-line|single-line"` allows extra parentheses around no/all/multi-line/single-line JSX components. Defaults to `none`.
26* `"enforceForArrowConditionals": false` allows extra parentheses around ternary expressions which are the body of an arrow function
27* `"enforceForSequenceExpressions": false` allows extra parentheses around sequence expressions
28* `"enforceForNewInMemberExpressions": false` allows extra parentheses around `new` expressions in member expressions
d3726936 29* `"enforceForFunctionPrototypeMethods": false` allows extra parentheses around immediate `.call` and `.apply` method calls on function expressions and around function expressions in the same context.
eb39fafa
DC
30
31### all
32
33Examples of **incorrect** code for this rule with the default `"all"` option:
34
35```js
36/* eslint no-extra-parens: "error" */
37
38a = (b * c);
39
40(a * b) + c;
41
42for (a in (b, c));
43
44for (a in (b));
45
46for (a of (b));
47
48typeof (a);
49
50(function(){} ? a() : b());
51```
52
53Examples of **correct** code for this rule with the default `"all"` option:
54
55```js
56/* eslint no-extra-parens: "error" */
57
58(0).toString();
59
60(Object.prototype.toString.call());
61
62({}.toString.call());
63
64(function(){}) ? a() : b();
65
66(/^a$/).test(x);
67
68for (a of (b, c));
69
70for (a of b);
71
72for (a in b, c);
73
74for (a in b);
75```
76
77### conditionalAssign
78
79Examples of **correct** code for this rule with the `"all"` and `{ "conditionalAssign": false }` options:
80
81```js
82/* eslint no-extra-parens: ["error", "all", { "conditionalAssign": false }] */
83
84while ((foo = bar())) {}
85
86if ((foo = bar())) {}
87
88do; while ((foo = bar()))
89
90for (;(a = b););
91```
92
93### returnAssign
94
95Examples of **correct** code for this rule with the `"all"` and `{ "returnAssign": false }` options:
96
97```js
98/* eslint no-extra-parens: ["error", "all", { "returnAssign": false }] */
99
100function a(b) {
101 return (b = 1);
102}
103
104function a(b) {
105 return b ? (c = d) : (c = e);
106}
107
108b => (b = 1);
109
110b => b ? (c = d) : (c = e);
111```
112
113### nestedBinaryExpressions
114
115Examples of **correct** code for this rule with the `"all"` and `{ "nestedBinaryExpressions": false }` options:
116
117```js
118/* eslint no-extra-parens: ["error", "all", { "nestedBinaryExpressions": false }] */
119
120x = a || (b && c);
121x = a + (b * c);
122x = (a * b) / c;
123```
124
125### ignoreJSX
126
127Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "all" }` options:
128
129```js
130/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "all" }] */
131const Component = (<div />)
132const Component = (
133 <div
134 prop={true}
135 />
136)
137```
138
139Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options:
140
141```js
142/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
143const Component = (<div />)
144const Component = (<div><p /></div>)
145```
146
147Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options:
148
149```js
150/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
151const Component = (
152 <div>
153 <p />
154 </div>
155)
156const Component = (
157 <div
158 prop={true}
159 />
160)
161```
162
163Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options:
164
165```js
166/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
167const Component = (
168 <div>
169 <p />
170 </div>
171)
172const Component = (
173 <div
174 prop={true}
175 />
176)
177```
178
179Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options:
180
181```js
182/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
183const Component = (<div />)
184const Component = (<div><p /></div>)
185```
186
187### enforceForArrowConditionals
188
189Examples of **correct** code for this rule with the `"all"` and `{ "enforceForArrowConditionals": false }` options:
190
191```js
192/* eslint no-extra-parens: ["error", "all", { "enforceForArrowConditionals": false }] */
193
194const b = a => 1 ? 2 : 3;
195const d = c => (1 ? 2 : 3);
196```
197
198### enforceForSequenceExpressions
199
200Examples of **correct** code for this rule with the `"all"` and `{ "enforceForSequenceExpressions": false }` options:
201
202```js
203/* eslint no-extra-parens: ["error", "all", { "enforceForSequenceExpressions": false }] */
204
205(a, b);
206
207if ((val = foo(), val < 10)) {}
208
209while ((val = foo(), val < 10));
210```
211
212### enforceForNewInMemberExpressions
213
214Examples of **correct** code for this rule with the `"all"` and `{ "enforceForNewInMemberExpressions": false }` options:
215
216```js
217/* eslint no-extra-parens: ["error", "all", { "enforceForNewInMemberExpressions": false }] */
218
219const foo = (new Bar()).baz;
220
221const quux = (new Bar())[baz];
222
223(new Bar()).doSomething();
224```
225
d3726936
TL
226### enforceForFunctionPrototypeMethods
227
228Examples of **correct** code for this rule with the `"all"` and `{ "enforceForFunctionPrototypeMethods": false }` options:
229
230```js
231/* eslint no-extra-parens: ["error", "all", { "enforceForFunctionPrototypeMethods": false }] */
232
233const foo = (function () {}).call();
234
235const bar = (function () {}).apply();
236
237const baz = (function () {}.call());
238
239const quux = (function () {}.apply());
240```
241
eb39fafa
DC
242### functions
243
244Examples of **incorrect** code for this rule with the `"functions"` option:
245
246```js
247/* eslint no-extra-parens: ["error", "functions"] */
248
249((function foo() {}))();
250
251var y = (function () {return 1;});
252```
253
254Examples of **correct** code for this rule with the `"functions"` option:
255
256```js
257/* eslint no-extra-parens: ["error", "functions"] */
258
259(0).toString();
260
261(Object.prototype.toString.call());
262
263({}.toString.call());
264
265(function(){} ? a() : b());
266
267(/^a$/).test(x);
268
269a = (b * c);
270
271(a * b) + c;
272
273typeof (a);
274```
275
276## Further Reading
277
278* [MDN: Operator Precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
279
280## Related Rules
281
282* [arrow-parens](arrow-parens.md)
283* [no-cond-assign](no-cond-assign.md)
284* [no-return-assign](no-return-assign.md)