brace-style

对块执行一致的大括号样式

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

大括号样式与编程中的 缩进风格 密切相关,描述了大括号相对于其控制语句和主体的位置。世界上可能有十几种(如果不是更多的话)牙套样式。

一种真正的大括号样式是 JavaScript 中最常见的大括号样式之一,其中块的左大括号与其对应的语句或声明位于同一行。例如:

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

一种真正的大括号样式的一种常见变体称为 Stroustrup,其中 if-else 构造中的 else 语句,以及 catchfinally,必须在前一个右大括号之后单独一行。例如:

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

pUORBVGn0FV8d+HBhkNCOxfZd8dH15hoAsTxRW1Fk3AKWLsS5WHpTjfZOkSyF39Ki519l+ZA7yCmzEzbY3GrSe27i185LJ6ds7akja4Qlb7BLje+mvW1TR74RbFMF8X2FJcWNVcNJ5yTLBa3bu8rAOMVmVmYvRhTNM7RKqVTHtL+dHAL6HleuzWUew1xV3Tic2UBDHuKexWmsx+sVlvWS1nGeiTqqaUuXrc/7hlDPn8=

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

ysRbuq+SoqJpTnEu2oYXADTNjBwjNg2JfETfl6Rej1kg7dKcZEymhx32zxEpY/L1kqaSyRSJpaA5bbbmVZx+PW65cM3NrzSxtj6hmo6MydAd0rN/RhwIiHaGel8WflMP83ghSH2aw4vqvyAPCqIKa6MZfjkYlNWav++wXOcZv+erFO1q2FLehaW/Xra3YSK8O1VVuhh44thC0JVj3031B5HkfDlmxykeznwDq2aaAFU=

规则详情

AP6ekhro8+axqk+e+DcbnMoCd4Q7bcYomZ/N8zBQHUobfLjWwfAlp0N01c3bHT9R

选项

VtL0QjEmIoPGGZlPIOT+lCPHCGcc3pzkW2DWeMVOvNfi9lWqrXd1TrfH2U3osAuE

    LfnwSXcWAHa+8XiDnCpWnhSo5+cK+X6aEDLOm74QtYPMassXR6uHPQUR8DxKUY9IMzl4sU3ChezR3vNLycsjcg+r5Rs7ToPiZuK6sk/qFSzkJ5SRUskmo81LdIYnB0QGLPWTuxkMrmwP2vqVLVIUa3cSBItccNeXKKXt/Jf0UmI/gJxUOEHG8gVi1KMVoNkHosBRE4TpC4mhLj3ds/ZkiAgKNjuh3VhVd97nJAAa//nIVtK3vZE08iqf2FXOcx76Nb7SGaVW7esfRsqaz80CqHteuBGaNK1Q36TxQ1PZVj0=

VtL0QjEmIoPGGZlPIOT+lAl4817POYOiIumol8q+y7Ih4juU5DbeYTpCHQrjX4m9

    ZoK5QQYs4d64QXQdqnl7IC5Xum5+nj2hm9Q3XJJhw51zUzmYQqtjD0EE6oH4ZQQCMuNLgHHoIdBbBr4806xBOJPNVXEPUDoaBIuHMuAkxWYe2rkj0naWSxalWqrrJSmU6r5U2XbU8Zu91i2N9fJk2G9AhomPzQ34YGJPxaLJxuhKGnS5pvozsmF1FWsvTszc

1tbs

gG4UUCWpCvzqVaU47Ruen42dWgNdsdgF3EeyrTbI3papix+3ijPkUYq9csaR/Q3NMDyagCfidAuUcF+in8FF+eY5D+gv/uzWby/fVz8ajAM=

/*eslint brace-style: "error"*/

function foo()
{
  return true;
}

if (foo)
{
  bar();
}

try
{
  somethingRisky();
} catch(e)
{
  handleError();
}

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

class C
{
    static
    {
        foo();
    }
}

gG4UUCWpCvzqVaU47Ruen42dWgNdsdgF3EeyrTbI3papix+3ijPkUYq9csaR/Q3NPuyaoOnGP71CYBmhUSAJQt8JT90r+pwG/cXv3p4k8fY=

/*eslint brace-style: "error"*/

function foo() {
  return true;
}

if (foo) {
  bar();
}

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

try {
  somethingRisky();
} catch(e) {
  handleError();
}

class C {
    static {
        foo();
    }
}

// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();

TlLpo7HIJjIy2v3f96HaCS5UpgHtBTS0XK50F3ItxGhbK5UzvO70CXKx20XCqTtlnNqZLKN+i2IOLzFxanAx2K0Fi0LeDmPmhey8dESKxb1j/ckDWatrJxtDVYRw49t+

/*eslint brace-style: ["error", "1tbs", { "allowSingleLine": true }]*/

function nop() { return; }

if (foo) { bar(); }

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

try { somethingRisky(); } catch(e) { handleError(); }

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

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

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

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

try { somethingRisky(); } catch(e) {
  handleError();
}

class C {
    static { foo(); }
}

class D { static { foo(); } }

stroustrup

TlLpo7HIJjIy2v3f96HaCZjdsK+JSijryE1rEyu3pmzKvfKYJ+Fb1oiSxFfQfKScGg5hMmwSpGKNTQbdE099NY8Pe7gHchcFuAEhbNNV860=

/*eslint brace-style: ["error", "stroustrup"]*/

function foo()
{
  return true;
}

if (foo)
{
  bar();
}

try
{
  somethingRisky();
} catch(e)
{
  handleError();
}

class C
{
    static
    {
        foo();
    }
}

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

TlLpo7HIJjIy2v3f96HaCZjdsK+JSijryE1rEyu3pmzKvfKYJ+Fb1oiSxFfQfKSccrOzHCpUI+GM1bF7EgB93vFrvnp4K4RSnp5BHN/dzSQ=

/*eslint brace-style: ["error", "stroustrup"]*/

function foo() {
  return true;
}

if (foo) {
  bar();
}

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

try {
  somethingRisky();
}
catch(e) {
  handleError();
}

class C {
    static {
        foo();
    }
}

// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();

TlLpo7HIJjIy2v3f96HaCZjdsK+JSijryE1rEyu3pmwukprkKbgvHc7VSOrOFc9ZCfnexksmjyOUqW2X9L47jYg08ybxro2xD5MCtfOUHChyHsCIIhwXralhXAWM7zw20r3Uk9CMeM5++Xo2qXrFFQ==

/*eslint brace-style: ["error", "stroustrup", { "allowSingleLine": true }]*/

function nop() { return; }

if (foo) { bar(); }

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

try { somethingRisky(); }
catch(e) { handleError(); }

class C {
    static { foo(); }
}

class D { static { foo(); } }

allman

TlLpo7HIJjIy2v3f96HaCZZMpkWF5BQzOPAU/hHPjCaS0MSIrbZGmeJO3g+xmyCGMVQgiAxByRBbgfJKXUcx3ExtMPLRbkRxR8IIXJvvd6M=

/*eslint brace-style: ["error", "allman"]*/

function foo() {
  return true;
}

if (foo)
{
  bar(); }

try
{
  somethingRisky();
} catch(e)
{
  handleError();
}

class C {
    static {
        foo();
    }
}

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

TlLpo7HIJjIy2v3f96HaCZZMpkWF5BQzOPAU/hHPjCbdkPHbXjpQC6p4FPLbdxfZ435QQL/N3AImjOwATRwII8KR/kQ78vUGikKAQ9BpA9A=

/*eslint brace-style: ["error", "allman"]*/

function foo()
{
  return true;
}

if (foo)
{
  bar();
}

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

try
{
  somethingRisky();
}
catch(e)
{
  handleError();
}

class C
{
    static
    {
        foo();
    }
}

// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();

TlLpo7HIJjIy2v3f96HaCc8GSPA4zu3OcpWq3SVajfopn10Jt6dCa2MI4Na60zeUbrNRLr/lPAeRbmInE17wp8GHlSPeiMYv8J8pVVyhXs2QbJX3+BMUTdFa3GZ50uzEw0wO6+++c3pWn8aKRyiL9w==

/*eslint brace-style: ["error", "allman", { "allowSingleLine": true }]*/

function nop() { return; }

if (foo) { bar(); }

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

try { somethingRisky(); }
catch(e) { handleError(); }

class C
{
    static { foo(); }

    static
    { foo(); }
}

class D { static { foo(); } }

何时不使用

yan8Lu3SBv/U45H/dAru2t0je6Cz7TZLoFM8UKe3/otsqpiC6g09EHO6c1OIqJrxymUVRkLZ2uLgguLrm7TVgZrjJamjqPtStEthYuzZLpSGP+hqOXcXOyZ+Cl9tEXYs