disallow unnecessary parentheses (no-extra-parens)

禁止冗余的括号(no-extra-parens)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

This rule restricts the use of parentheses to only where they are necessary.

该规则对圆括号的使用做了限制,只有在必要的地方才能使用。

Rule Details

This rule always ignores extra parentheses around the following:

该规则总是忽略以下情况的额外的圆括号:

Options

This rule has a string option:

该规则有一个字符串选项:

This rule has an object option for exceptions to the "all" option:

该规则对于 "all" 选项有例外情况,是个对象:

all

Examples of incorrect code for this rule with the default "all" option:

默认选项 "all"错误 代码示例:

/* eslint no-extra-parens: "error" */

a = (b * c);

(a * b) + c;

for (a in (b, c));

for (a in (b));

for (a of (b));

typeof (a);

(function(){} ? a() : b());

Examples of correct code for this rule with the default "all" option:

默认选项 "all"正确 代码示例:

/* eslint no-extra-parens: "error" */

(0).toString();

(Object.prototype.toString.call());

({}.toString.call());

(function(){}) ? a() : b();

(/^a$/).test(x);

for (a of (b, c));

for (a of b);

for (a in b, c);

for (a in b);

conditionalAssign

Examples of correct code for this rule with the "all" and { "conditionalAssign": false } options:

选项 "all"{ "conditionalAssign": false }正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { "conditionalAssign": false }] */

while ((foo = bar())) {}

if ((foo = bar())) {}

do; while ((foo = bar()))

for (;(a = b););

returnAssign

Examples of correct code for this rule with the "all" and { "returnAssign": false } options:

选项 "all"{ "returnAssign": false }正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { "returnAssign": false }] */

function a(b) {
  return (b = 1);
}

function a(b) {
  return b ? (c = d) : (c = e);
}

b => (b = 1);

b => b ? (c = d) : (c = e);

nestedBinaryExpressions

Examples of correct code for this rule with the "all" and { "nestedBinaryExpressions": false } options:

选项 "all"{ "nestedBinaryExpressions": false }正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { "nestedBinaryExpressions": false }] */

x = a || (b && c);
x = a + (b * c);
x = (a * b) / c;

ignoreJSX

Examples of correct code for this rule with the all and { "ignoreJSX": "all" } options:

选项 all{ "ignoreJSX": "all" } 正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "all" }] */
const Component = (<div />)
const Component = (
    <div
        prop={true}
    />
)

Examples of incorrect code for this rule with the all and { "ignoreJSX": "multi-line" } options:

选项 all{ "ignoreJSX": "multi-line" }错误 代码示例:

/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
const Component = (<div />)
const Component = (<div><p /></div>)

Examples of correct code for this rule with the all and { "ignoreJSX": "multi-line" } options:

选项 all{ "ignoreJSX": "multi-line" }正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
const Component = (
    <div>
        <p />
    </div>
)
const Component = (
    <div
        prop={true}
    />
)

Examples of incorrect code for this rule with the all and { "ignoreJSX": "single-line" } options:

选项 all{ "ignoreJSX": "single-line" }错误 代码示例:

/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
const Component = (
    <div>
        <p />
    </div>
)
const Component = (
    <div
        prop={true}
    />
)

Examples of correct code for this rule with the all and { "ignoreJSX": "single-line" } options:

选项 all{ "ignoreJSX": "single-line" }正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
const Component = (<div />)
const Component = (<div><p /></div>)

enforceForArrowConditionals

Examples of correct code for this rule with the "all" and { "enforceForArrowConditionals": false } options:

选项 "all" and { "enforceForArrowConditionals": false }正确 代码示例:

/* eslint no-extra-parens: ["error", "all", { "enforceForArrowConditionals": false }] */

const b = a => 1 ? 2 : 3;
const d = c => (1 ? 2 : 3);

functions

Examples of incorrect code for this rule with the "functions" option:

选项 "functions"错误 代码示例:

/* eslint no-extra-parens: ["error", "functions"] */

((function foo() {}))();

var y = (function () {return 1;});

Examples of correct code for this rule with the "functions" option:

选项 "functions"正确 代码示例:

/* eslint no-extra-parens: ["error", "functions"] */

(0).toString();

(Object.prototype.toString.call());

({}.toString.call());

(function(){} ? a() : b());

(/^a$/).test(x);

a = (b * c);

(a * b) + c;

typeof (a);

Further Reading

Version

This rule was introduced in ESLint 0.1.4.

该规则在 ESLint 0.1.4 中被引入。

Resources