Disallow unnecessary parentheses
Some problems reported by this rule are automatically fixable by the --fix
command line option
This rule restricts the use of parentheses to only where they are necessary.
This rule always ignores extra parentheses around the following:
(/abc/).test(var)
to avoid conflicts with the wrap-regex rulevar x = (function () {})();
and var x = (function () {}());
to avoid conflicts with the wrap-iife ruleThis rule has a string option:
"all"
(default) disallows unnecessary parentheses around any expression"functions"
disallows unnecessary parentheses only around function expressionsThis rule has an object option for exceptions to the "all"
option:
"conditionalAssign": false
allows extra parentheses around assignments in conditional test expressions"returnAssign": false
allows extra parentheses around assignments in return
statements"nestedBinaryExpressions": false
allows extra parentheses in nested binary expressions"ignoreJSX": "none|all|multi-line|single-line"
allows extra parentheses around no/all/multi-line/single-line JSX components. Defaults to none
."enforceForArrowConditionals": false
allows extra parentheses around ternary expressions which are the body of an arrow function"enforceForSequenceExpressions": false
allows extra parentheses around sequence expressions"enforceForNewInMemberExpressions": false
allows extra parentheses around new
expressions in member expressions"enforceForFunctionPrototypeMethods": false
allows extra parentheses around immediate .call
and .apply
method calls on function expressions and around function expressions in the same context."allowParensAfterCommentPattern": "any-string-pattern"
allows extra parentheses preceded by a comment that matches a regular expression.Examples of incorrect code for this rule with the default "all"
option:
Examples of correct code for this rule with the default "all"
option:
Examples of correct code for this rule with the "all"
and { "conditionalAssign": false }
options:
Examples of correct code for this rule with the "all"
and { "returnAssign": false }
options:
Examples of correct code for this rule with the "all"
and { "nestedBinaryExpressions": false }
options:
Examples of correct code for this rule with the all
and { "ignoreJSX": "all" }
options:
Examples of incorrect code for this rule with the all
and { "ignoreJSX": "multi-line" }
options:
Examples of correct code for this rule with the all
and { "ignoreJSX": "multi-line" }
options:
Examples of incorrect code for this rule with the all
and { "ignoreJSX": "single-line" }
options:
Examples of correct code for this rule with the all
and { "ignoreJSX": "single-line" }
options:
Examples of correct code for this rule with the "all"
and { "enforceForArrowConditionals": false }
options:
Examples of correct code for this rule with the "all"
and { "enforceForSequenceExpressions": false }
options:
Examples of correct code for this rule with the "all"
and { "enforceForNewInMemberExpressions": false }
options:
Examples of correct code for this rule with the "all"
and { "enforceForFunctionPrototypeMethods": false }
options:
To make this rule allow extra parentheses preceded by specific comments, set this option to a string pattern that will be passed to the RegExp
constructor.
Examples of correct code for this rule with the "all"
and { "allowParensAfterCommentPattern": "@type" }
options:
/* eslint no-extra-parens: ["error", "all", { "allowParensAfterCommentPattern": "@type" }] */
const span = /**@type {HTMLSpanElement}*/(event.currentTarget);
if (/** @type {Foo | Bar} */(options).baz) console.log('Lint free');
foo(/** @type {Bar} */ (bar), options, {
name: "name",
path: "path",
});
if (foo) {
/** @type {Bar} */
(bar).prop = false;
}
Examples of incorrect code for this rule with the "functions"
option:
Examples of correct code for this rule with the "functions"
option:
This rule was introduced in ESLint v0.1.4.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-extra-parens