]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # disallow unnecessary parentheses (no-extra-parens) |
2 | ||
3 | This rule restricts the use of parentheses to only where they are necessary. | |
4 | ||
5 | ## Rule Details | |
6 | ||
7 | This 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 | ||
15 | This 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 | ||
20 | This 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 | ||
33 | Examples of **incorrect** code for this rule with the default `"all"` option: | |
34 | ||
35 | ```js | |
36 | /* eslint no-extra-parens: "error" */ | |
37 | ||
38 | a = (b * c); | |
39 | ||
40 | (a * b) + c; | |
41 | ||
42 | for (a in (b, c)); | |
43 | ||
44 | for (a in (b)); | |
45 | ||
46 | for (a of (b)); | |
47 | ||
48 | typeof (a); | |
49 | ||
50 | (function(){} ? a() : b()); | |
51 | ``` | |
52 | ||
53 | Examples 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 | ||
68 | for (a of (b, c)); | |
69 | ||
70 | for (a of b); | |
71 | ||
72 | for (a in b, c); | |
73 | ||
74 | for (a in b); | |
75 | ``` | |
76 | ||
77 | ### conditionalAssign | |
78 | ||
79 | Examples 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 | ||
84 | while ((foo = bar())) {} | |
85 | ||
86 | if ((foo = bar())) {} | |
87 | ||
88 | do; while ((foo = bar())) | |
89 | ||
90 | for (;(a = b);); | |
91 | ``` | |
92 | ||
93 | ### returnAssign | |
94 | ||
95 | Examples 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 | ||
100 | function a(b) { | |
101 | return (b = 1); | |
102 | } | |
103 | ||
104 | function a(b) { | |
105 | return b ? (c = d) : (c = e); | |
106 | } | |
107 | ||
108 | b => (b = 1); | |
109 | ||
110 | b => b ? (c = d) : (c = e); | |
111 | ``` | |
112 | ||
113 | ### nestedBinaryExpressions | |
114 | ||
115 | Examples 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 | ||
120 | x = a || (b && c); | |
121 | x = a + (b * c); | |
122 | x = (a * b) / c; | |
123 | ``` | |
124 | ||
125 | ### ignoreJSX | |
126 | ||
127 | Examples 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" }] */ | |
131 | const Component = (<div />) | |
132 | const Component = ( | |
133 | <div | |
134 | prop={true} | |
135 | /> | |
136 | ) | |
137 | ``` | |
138 | ||
139 | Examples 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" }] */ | |
143 | const Component = (<div />) | |
144 | const Component = (<div><p /></div>) | |
145 | ``` | |
146 | ||
147 | Examples 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" }] */ | |
151 | const Component = ( | |
152 | <div> | |
153 | <p /> | |
154 | </div> | |
155 | ) | |
156 | const Component = ( | |
157 | <div | |
158 | prop={true} | |
159 | /> | |
160 | ) | |
161 | ``` | |
162 | ||
163 | Examples 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" }] */ | |
167 | const Component = ( | |
168 | <div> | |
169 | <p /> | |
170 | </div> | |
171 | ) | |
172 | const Component = ( | |
173 | <div | |
174 | prop={true} | |
175 | /> | |
176 | ) | |
177 | ``` | |
178 | ||
179 | Examples 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" }] */ | |
183 | const Component = (<div />) | |
184 | const Component = (<div><p /></div>) | |
185 | ``` | |
186 | ||
187 | ### enforceForArrowConditionals | |
188 | ||
189 | Examples 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 | ||
194 | const b = a => 1 ? 2 : 3; | |
195 | const d = c => (1 ? 2 : 3); | |
196 | ``` | |
197 | ||
198 | ### enforceForSequenceExpressions | |
199 | ||
200 | Examples 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 | ||
207 | if ((val = foo(), val < 10)) {} | |
208 | ||
209 | while ((val = foo(), val < 10)); | |
210 | ``` | |
211 | ||
212 | ### enforceForNewInMemberExpressions | |
213 | ||
214 | Examples 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 | ||
219 | const foo = (new Bar()).baz; | |
220 | ||
221 | const quux = (new Bar())[baz]; | |
222 | ||
223 | (new Bar()).doSomething(); | |
224 | ``` | |
225 | ||
d3726936 TL |
226 | ### enforceForFunctionPrototypeMethods |
227 | ||
228 | Examples 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 | ||
233 | const foo = (function () {}).call(); | |
234 | ||
235 | const bar = (function () {}).apply(); | |
236 | ||
237 | const baz = (function () {}.call()); | |
238 | ||
239 | const quux = (function () {}.apply()); | |
240 | ``` | |
241 | ||
eb39fafa DC |
242 | ### functions |
243 | ||
244 | Examples 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 | ||
251 | var y = (function () {return 1;}); | |
252 | ``` | |
253 | ||
254 | Examples 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 | ||
269 | a = (b * c); | |
270 | ||
271 | (a * b) + c; | |
272 | ||
273 | typeof (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) |