curly

对所有控制语句强制使用一致的大括号样式

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

当一个块只包含一个语句时,JavaScript 允许省略花括号。然而,许多人认为最好的做法是永远不要在块周围省略花括号,即使它们是可选的,因为它可能导致错误并降低代码清晰度。所以以下内容:

if (foo) foo++;

可以改写为:

if (foo) {
    foo++;
}

然而,有些人更喜欢只在有多个语句要执行时才使用大括号。

规则详情

eKw1B42N5fg/8pd8784KAleB7w0UC1RrdXzmKWGwqbsPErzXsxXEw6yCIU0NoSl+t2niLg0PBmGz/ASKFvNlkn7g+nKdEAsuoQfr008S66wgvHZ8CC329hAkIRoDbo9NA/DBJBnGW/PnBvnf1E+ZNlnnXhIM+6QNBUQwXW9Sm830dO6vwU07JGk4o6de7NQwNIJvnjP34/GrFvSCd0K8mQ==

选项

all

G6F1lpCMxZMgq5rpSUinYex4KxF3HfN8CsHymSd83KRW892lpb087+xSZeqxE/J6TsGW/mXGNkNjEl+Uk/+fNQ==

/*eslint curly: "error"*/

if (foo) foo++;

while (bar)
    baz();

if (foo) {
    baz();
} else qux();

G6F1lpCMxZMgq5rpSUinYex4KxF3HfN8CsHymSd83KR73G7d+W46f2Om/RBvgFfHGAjHgMTckkrItzSliBi7Ww==

/*eslint curly: "error"*/

if (foo) {
    foo++;
}

while (bar) {
    baz();
}

if (foo) {
    baz();
} else {
    qux();
}

multi

EK9mPQae0gauIHtzw9uJbOFeo/KscTtU21Ut6wd3bDV84mHXnKIv4JHgmJHRISSLayfFcqmfDWfm3vqz33B9S/tXWePs+GVNZQc/0caApoUYR4nStYlqD6yvn4FTV9ady8d9icO63sPRKx79sxtpX+zlOiOsaEGB/cyjzDLBidQ+v4DcAU8aSvN6JZTS98YmiSoZ/CTV26X2p8f9b9BkJ0FozqMdrOEiVe4uh76ytwIM6DnHPj71dPSgJ3iHlAyInwa4GVJgauIldQj2EjwyTDW+3222IBZHLwcfeB8kZ8/efSPDmbB13Tf1ktri1XXFDex/8oL53H9AH4XPVXixGMCrux9s7Y2fUB07EEO2ljypLhyuau1tlQVRr7EP0ucRTqnOAi9CwF2mxyZDio7GrJvZUb4mL2ByZowbWnnzjr4=

78ZR1oPyaN37bpNXlEiUBCv7o2v4h9gXx6ma5pvsrEuA7xzMo0TWFWREh1oX5pxBIzm1pZl0xyV9DRki1Yo65Q==

/*eslint curly: ["error", "multi"]*/

if (foo) {
    foo++;
}

if (foo) bar();
else {
    foo++;
}

while (true) {
    doSomething();
}

for (var i=0; i < items.length; i++) {
    doSomething();
}

78ZR1oPyaN37bpNXlEiUBErVuyfQ0aR+pkPWMUMMLdXrjUml8wqbqAs6E0bNitMrXMIg/fVb1PyhsSC+FVx7hQ==

/*eslint curly: ["error", "multi"]*/

if (foo) foo++;

else foo();

while (true) {
    doSomething();
    doSomethingElse();
}

multi-line

zzzRh6jBpI9jj3+TG8sKHYmfg8dZ9TPcUgP54nI/GnexurQC6B5iSYgN5psp557rGuDuUsCfJpIIUc/YHA0zxiCvrm9OoHJWR/ar8apx/r0mNWHqH2feQ/WK4GiPhqBeLpLRta2zUNN81wGsvbQhXwWkCb7qJZS+dJU0u0igkccjEzxV4NJqpXZLULWIIl/dFtYHG7MZNXclw5k23I62Jk2e0BSI2Bgqcpza4GbLm87qGM3CEFfDzg9JyaFUjyeEWMsLOO9y5soBeGVq5XEk7Iuhdx2KKabTQXhf5CLUDVzcTWamzB09s0zFGjU7a4Xl

jFm3cFfTqRRHjQXHI1eu+3mWH06Lpr9HZId1SNEIAOTrwGhGNmGBKnQaXm/GNuC1qdJssJPp74DvZ/JUz0CKEQ==

/*eslint curly: ["error", "multi-line"]*/

if (foo)
  doSomething();
else
  doSomethingElse();

if (foo) foo(
  bar,
  baz);

jFm3cFfTqRRHjQXHI1eu+3mWH06Lpr9HZId1SNEIAOSh9lF2OJhtkTrlepf2Vbq3QMGBBye+WNYco3t88+Ubbw==

/*eslint curly: ["error", "multi-line"]*/

if (foo) foo++; else doSomething();

if (foo) foo++;
else if (bar) baz()
else doSomething();

do something();
while (foo);

while (foo
  && bar) baz();

if (foo) {
    foo++;
}

if (foo) { foo++; }

while (true) {
    doSomething();
    doSomethingElse();
}

multi-or-nest

ZBUB4xobcj3y95X7HkzXP2H4ntroX5AXFRSuKRRXxoRiFmmrZkfYlvvACIwRN1Oggp103uCtoFXWQtRecv3bYgs6HEkVwMf3Jp9rKLshpMuxWLdF+LRuY0r/1dxQDmY4iJISRFnxUjDevEp36O+MGSbSas2oCW4lfWoHxLXA9oz6adPkz7EEna8ydzo3IhFr/nMRoU7cHnd+UE95SRimXLpNfk+YUxnr62ABS8j+mg4FdhTReEsQqZfGKOnozYOhONxpxuNIKH8ylEs+4qVfE1Q4hNXBmAOBc9gQYCXYSYYlvZ3FLK1WYSYkO+iyOVam4rC/lsuWCCk4V6jlKnXSpTlFkFd4xXRCsfb90C1j/QK6MiDw6CS9GunsWiY5B6OM

RglaXeL46MGX0qnNSoXRl8d6bleqLwrIwJ9oVj8tI0+1q1V06Abvt0b3AeT1kb2VKaF98UJzWWOgMskPN/i4eQ==

/*eslint curly: ["error", "multi-or-nest"]*/

if (!foo)
    foo = {
        bar: baz,
        qux: foo
    };

while (true)
  if(foo)
      doSomething();
  else
      doSomethingElse();

if (foo) {
    foo++;
}

while (true) {
    doSomething();
}

for (var i = 0; foo; i++) {
    doSomething();
}

RglaXeL46MGX0qnNSoXRl8d6bleqLwrIwJ9oVj8tI0+QZGw+BPz4fl9aGmJEF7gl64c9umP61kw7xfXail2o5w==

/*eslint curly: ["error", "multi-or-nest"]*/

if (!foo) {
    foo = {
        bar: baz,
        qux: foo
    };
}

while (true) {
  if(foo)
      doSomething();
  else
      doSomethingElse();
}

if (foo)
    foo++;

while (true)
    doSomething();

for (var i = 0; foo; i++)
    doSomething();

Ge+p2x21iLRNoOg0BPssufkpjygS9kAtj9AraiYiFt0zIvc4+UqEuR0F8XqV+V8rGFWuA1J+9KLQLQNi/nUQpgfyPssDPy3Z7SQojWivgmjs0UmJJH33siZG+ozaYhVq

RglaXeL46MGX0qnNSoXRl8d6bleqLwrIwJ9oVj8tI0+QaO4OvjSeo2rXzxZr5TVFYuEV4XKCtWDqiLZkteeH2XA4CLjzEoTyhNbZdP3b0aM=

/*eslint curly: ["error", "multi-or-nest"]*/

if (foo)
    // some comment
    bar();

if (foo) {
    // some comment
    bar();
}

consistent

NahLVppmp12/QZYRzRQsbVZpwAmJUgJEgP1MomgAb+92EL8jjq+1+bmrXVh3H7Jps5E5uppw9PVsrKGQnXDD4pWBoFzoJliqEzbOb4G7Y665e7lVGpYi51F4sLOgGVmh1BrD61n4WG/IrnpsbHUuX6lu6vs0gHdrlK9ZEwfrieItFoHgqjhUOUf2SDeSn83jlDWV63P2j0ljp14X0m6lIu/EEeO+StOSiF0MSOBaL4x4wAyXZhPQ6goGS7kepqwTlW+Fljkz5bLhjQBtiSSXaA==

EuPzTszzPeb0nEQzLAdKRczcvF4t1jCf77wlF1bbmNgInEvRbDwhcihTcFGsbXFR07X16eGzKX7Q32egZPeebbF1zi9P1JB+tWX0uLBj+QM=

/*eslint curly: ["error", "multi", "consistent"]*/

if (foo) {
    bar();
    baz();
} else
    buz();

if (foo)
    bar();
else if (faa)
    bor();
else {
    other();
    things();
}

if (true)
    foo();
else {
    baz();
}

if (foo) {
    foo++;
}

EuPzTszzPeb0nEQzLAdKRczcvF4t1jCf77wlF1bbmNjaeb3qd0YoEwtbAoc9/DhBdaD8wwIl64M9JwPyDPm17vaje6Eq4txWN8o5DIA3SlQ=

/*eslint curly: ["error", "multi", "consistent"]*/

if (foo) {
    bar();
    baz();
} else {
    buz();
}

if (foo) {
    bar();
} else if (faa) {
    bor();
} else {
    other();
    things();
}

if (true)
    foo();
else
    baz();

if (foo)
    foo++;

何时不使用

dY5wHwl9kgsb1yUq1v+epVL7MFPD2zQVsrRXRbvuX4HoGDU1ojFvi8BcyS82uTxDoE36SAVWjZhj+C6XB28A1aDduzgopla7WzEQ4Z1mqgyrooKK4oPOsYLyC1pv3qG5evli2ucBb9QVmQ5l+FcRnj5s2BZdgbpz5fhxTPczjJA=