1 # disallow unnecessary parentheses (no-extra-parens)
3 This rule restricts the use of parentheses to only where they are necessary.
7 This rule always ignores extra parentheses around the following:
9 * RegExp literals such as `(/abc/).test(var)` to avoid conflicts with the [wrap-regex](wrap-regex.md) rule
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
11 * arrow function arguments to avoid conflicts with the [arrow-parens](arrow-parens.md) rule
15 This rule has a string option:
17 * `"all"` (default) disallows unnecessary parentheses around *any* expression
18 * `"functions"` disallows unnecessary parentheses *only* around function expressions
20 This rule has an object option for exceptions to the `"all"` option:
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
29 * `"enforceForFunctionPrototypeMethods": false` allows extra parentheses around immediate `.call` and `.apply` method calls on function expressions and around function expressions in the same context.
33 Examples of **incorrect** code for this rule with the default `"all"` option:
36 /* eslint no-extra-parens: "error" */
50 (function(){} ? a() : b());
61 Examples of **correct** code for this rule with the default `"all"` option:
64 /* eslint no-extra-parens: "error" */
68 (Object.prototype.toString.call());
72 (function(){}) ? a() : b();
95 Examples of **correct** code for this rule with the `"all"` and `{ "conditionalAssign": false }` options:
98 /* eslint no-extra-parens: ["error", "all", { "conditionalAssign": false }] */
100 while ((foo = bar())) {}
102 if ((foo = bar())) {}
104 do; while ((foo = bar()))
111 Examples of **correct** code for this rule with the `"all"` and `{ "returnAssign": false }` options:
114 /* eslint no-extra-parens: ["error", "all", { "returnAssign": false }] */
121 return b ? (c = d) : (c = e);
126 b => b ? (c = d) : (c = e);
129 ### nestedBinaryExpressions
131 Examples of **correct** code for this rule with the `"all"` and `{ "nestedBinaryExpressions": false }` options:
134 /* eslint no-extra-parens: ["error", "all", { "nestedBinaryExpressions": false }] */
143 Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "all" }` options:
146 /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "all" }] */
147 const Component = (<div />)
155 Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options:
158 /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
159 const Component = (<div />)
160 const Component = (<div><p /></div>)
163 Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options:
166 /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
179 Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options:
182 /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
195 Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options:
198 /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
199 const Component = (<div />)
200 const Component = (<div><p /></div>)
203 ### enforceForArrowConditionals
205 Examples of **correct** code for this rule with the `"all"` and `{ "enforceForArrowConditionals": false }` options:
208 /* eslint no-extra-parens: ["error", "all", { "enforceForArrowConditionals": false }] */
210 const b = a => 1 ? 2 : 3;
211 const d = c => (1 ? 2 : 3);
214 ### enforceForSequenceExpressions
216 Examples of **correct** code for this rule with the `"all"` and `{ "enforceForSequenceExpressions": false }` options:
219 /* eslint no-extra-parens: ["error", "all", { "enforceForSequenceExpressions": false }] */
223 if ((val = foo(), val < 10)) {}
225 while ((val = foo(), val < 10));
228 ### enforceForNewInMemberExpressions
230 Examples of **correct** code for this rule with the `"all"` and `{ "enforceForNewInMemberExpressions": false }` options:
233 /* eslint no-extra-parens: ["error", "all", { "enforceForNewInMemberExpressions": false }] */
235 const foo = (new Bar()).baz;
237 const quux = (new Bar())[baz];
239 (new Bar()).doSomething();
242 ### enforceForFunctionPrototypeMethods
244 Examples of **correct** code for this rule with the `"all"` and `{ "enforceForFunctionPrototypeMethods": false }` options:
247 /* eslint no-extra-parens: ["error", "all", { "enforceForFunctionPrototypeMethods": false }] */
249 const foo = (function () {}).call();
251 const bar = (function () {}).apply();
253 const baz = (function () {}.call());
255 const quux = (function () {}.apply());
260 Examples of **incorrect** code for this rule with the `"functions"` option:
263 /* eslint no-extra-parens: ["error", "functions"] */
265 ((function foo() {}))();
267 var y = (function () {return 1;});
270 Examples of **correct** code for this rule with the `"functions"` option:
273 /* eslint no-extra-parens: ["error", "functions"] */
277 (Object.prototype.toString.call());
279 ({}.toString.call());
281 (function(){} ? a() : b());
294 * [MDN: Operator Precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
298 * [arrow-parens](arrow-parens.md)
299 * [no-cond-assign](no-cond-assign.md)
300 * [no-return-assign](no-return-assign.md)