no-extra-parens

禁止不必要的括号

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

该规则将括号的使用限制在必要的地方。

规则详情

此规则始终忽略以下内容周围的额外括号:

  • (/abc/).test(var) 等 RegExp 字面,以避免与 wrap-regex 规则冲突
  • 立即调用的函数表达式(也称为 IIFE),例如 var x = (function () {})();var x = (function () {}());,以避免与 wrap-iife 规则冲突
  • 箭头函数参数以避免与 arrow-parens 规则冲突

选项

9NHbribgvlAIiqeVNtZmFM4vxdVdCG3JUSov7ZLYtDzFNHKDsMdAhRCfDUY7wqW3

    wQS6o1GBDm7ItUlaHAf3WjDho0GfF3ojQ+6aADuVP3DuZ+9NLRPQ4YXkQsWBFeNBlqpKL2Na5Kgbl6yXOAB0ddHtnkzqQaGYqDrgkB6ef9fRnyBl7BsHZXwX9tRguqHfvgHg+64DU55q5aM39O8xKMuLmrR8QWEtbduI/fI49d00imxlrAZQGazK6neHNLTykEFupvJDyjMaBTFXCAUE1zy6Cpv9Z064pOuLfSOW5N5hkP/mFeFm8UhGsEi+4Jaj

9NHbribgvlAIiqeVNtZmFJBXFzE5ZrwC3cI4vwFctVVa2JWrIng+jbVspdDBYGumg/1ftdWcetPVavklw2tcc3zZeeXfLtWfunfPfGG06BhV/15e9uQjgR5bLDDQqnHs

    Ae0E4rRlyWO+vXe1o6mu3RWfgysrwjMwVBVFvGWUa0nbPtaW4TYWg8+XhB7cv2lyz/A0twLkcgR3QupbY9TMxw6+n1lRFSuches3KTUCY7cK9uOdSjpf6sLTKEZYlNNx3Ocd53K0nSktt4byjWt+hOASIYIEnR9OeOcvCl1wPzZNXmGNl6LnTgouZnRp1MG5TGqAFwDpOkN4a/Tl3utKSipkPjKN+J9F7uaGMt/wA1dZzUTx+lB+Z9iEAnM4FmitFeYyLnYTEOpxeC+SkrXu4wi1SmejFSjB4hF4lLADU7aOfLXbURoa8iZZzfNmpeeZaUJV9utyI3VInPpzvMDMIWQSGqNk7Qc+K3klXwx7/Q8fiF7FzqHxhs1LLLix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rdG1WE/tYrNFJe3orC3tAGNnAoD71e8dziWRA3qhKo3qLq6U+Ma7QJTYJBCVq4nZawywXRtNtQ40T0pFILF8JL3kBE0baNA==

all

1usau4ITNzPbSW1XK9odV6Oy3LQmRZT0GsoF9ajOIR3BsFczZqIgD7JzxbP+x/9EDFFURizSfafekUN5iEzCANZmsrCWAfc/hoWLnaLzsuU=

/* 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());

class A {
    [(x)] = 1;
}

class B {
    x = (y + z);
}

1usau4ITNzPbSW1XK9odV6Oy3LQmRZT0GsoF9ajOIR3BsFczZqIgD7JzxbP+x/9EJL+010+6wLKyFpT2GSrG3crkaoPUYSgJ2Ls1abPRBIk=

/* 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);

class A {
    [x] = 1;
}

class B {
    x = y + z;
}

conditionalAssign

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5y2ZeeloIcxp5X2yqERyB1TRo1skWoVadob4gyV2KF5XutRLjjObAZV9284BEHsQ1zZiKRJ8SiyRo0/LtwTZwKidxAN7s8bDsyBACfmw4jcwI=

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

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

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

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

for (;(a = b););

returnAssign

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5yuuFSRnIpVluQLmsnewuIdHF2yM0/Q9WELCQNE9hQVHBARmn3hhNdNFybhbwcS5V4jzZTDHHgNXrZyeEj6eFghg==

/* 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

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5yFFaShWkw9O+i391c2UD6iAU5Pk2JxmoB8DMxhqRDeYv4ODNTVnFAqPmyhdVkWrtgl+BLCv+DKfeWkugEQ5pDB1PzjP77L2L4yTBdctcRins=

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

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

ignoreJSX

dnC4C79+++7hmkZ7ssvw8x46GD2Q81ODWCDHT11bA+VdBl2cNdaFWj9dPfFVvg//Un0TTdiX3/S2rrTepttZP9UUdv07BjEj1rsXYr1nKKCEG+9USZ90H6CQg4FEHkj5xbxToD7Uvzpbw7RMtkmLPQ==

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

5In/j5kw9CNVpmz/H6UfIXoP6XXGt5ngDLexzMn+fezcMFveI6lVKGI9GTDe5TEiYCN78a6/nh7rqG7tKYyLY8Aq1G2cLPcAb0CB7MNMjSZh3GVC2JP6e8VVigywfQHDeU6oIsx30E6RfMxmM4KeLIMJ+HrJ/bEg8QFfpyUqL40=

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

dnC4C79+++7hmkZ7ssvw8x46GD2Q81ODWCDHT11bA+VdBl2cNdaFWj9dPfFVvg//ArrpH2SkyFJPlJK1rizEVHKRqv5xeqL8uNPCNQqoAmkG+Gosy/Ug37FE/EhWCqcf55uUCUAeLP1VYjC6gpYbPA==

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

5In/j5kw9CNVpmz/H6UfIXoP6XXGt5ngDLexzMn+fezcMFveI6lVKGI9GTDe5TEi69bLsya4d1SB88agd+o2cS/GJde1wxNbRDuTYVGD4yKuRUvYwVd9JE2P+66IDOLY88xEI+c6072Ia6cQmpzwIxp3TYTHpcTjnZF6ugrXptc=

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

dnC4C79+++7hmkZ7ssvw8x46GD2Q81ODWCDHT11bA+VdBl2cNdaFWj9dPfFVvg//Jvxg9OHPSG4duNZmDxvQ9URY0zvbeZnIWOHuZvlzZa8sqKVzLxehTDeYLNhs8k6sVUlGpy7H6GjNEYc8P8zhUQ==

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

enforceForArrowConditionals

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5yxn43MPFN0ewssxVXQJrYyui6h2vhhBNlcsh8G21dBgHN13eb2Edi5pwRkCfsbhHxc+q8ozZEnTCMtvOC3IMJ8isWaX4u4azNMNBlEBrswY4=

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

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

enforceForSequenceExpressions

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5yZGvBfiho51q/X1Q0NOPFCaVHDYJ3GhMW9EWUxUrsWW+2O3KhuK5qhVYXAKSK8WDlscH6GIY07Apu+cly/8eK4wUaNvgIaBhXyFHLIEum2Pw=

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

(a, b);

if ((val = foo(), val < 10)) {}

while ((val = foo(), val < 10));

enforceForNewInMemberExpressions

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5yawjap1dGVp4xurfNNzgM9OCReras+EvsptOZNzWXoGd996y0KyVnVtkclz1elkoiqYne1ng1IfRmc8R3S1xMonEor57aQtBVXJK41P+6FGAOQE/SYrvO3AwjjwFfEAze

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

const foo = (new Bar()).baz;

const quux = (new Bar())[baz];

(new Bar()).doSomething();

enforceForFunctionPrototypeMethods

dnC4C79+++7hmkZ7ssvw8+MZKD9UBiCSI5Sr8gevMe3GMWp2/zImo8ZmTmuaII5ySkzpqMA+P/7fx7+HmOt1b7ZhVLkh2ErykZr1sR2n35yCfeyzKgVG9p9upDGz25sQCxnKww5KuI4JpkXKvyN99eGZcfAbYmrq9dLqpCix8s3mDVPX6XN8HIve0erWyG3L

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

const foo = (function () {}).call();

const bar = (function () {}).apply();

const baz = (function () {}.call());

const quux = (function () {}.apply());

functions

dnC4C79+++7hmkZ7ssvw87gQegpk86/T0P5rEO9VMnVvxifH3HaKRdaQbtugUww5pYjVF2ydag5h53aj7HSWe2sqioGUX7Cy5AoqV1dp+D0=

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

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

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

dnC4C79+++7hmkZ7ssvw87gQegpk86/T0P5rEO9VMnVvxifH3HaKRdaQbtugUww5DYJ4+fQuF9iz5s545vc+0e84Y1e80FrWvgEXd56s5vg=

/* 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);