版本

brace-style

強制區塊使用一致的大括號樣式

🔧 可修正

此規則報告的一些問題可以通過 --fix 命令列選項自動修正

此規則已在 ESLint v8.53.0 中棄用。請使用 @stylistic/eslint-plugin-js 中的相應規則

大括號樣式與程式設計中的縮排樣式密切相關,並描述了大括號相對於其控制語句和主體的放置位置。世界上可能有一打甚至更多的的大括號樣式。

單一真大括號樣式是 JavaScript 中最常見的大括號樣式之一,其中區塊的開頭大括號與其對應的語句或宣告放在同一行。例如

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

單一真大括號樣式的一個常見變體稱為 Stroustrup,其中 if-else 結構中的 else 語句,以及 catchfinally 必須在前面的結尾大括號之後的單獨一行上。例如

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

另一種樣式稱為 Allman,其中所有的大括號都應該在其自己的行上,沒有任何額外的縮排。例如

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

雖然沒有任何樣式被認為比其他樣式更好,但大多數開發人員都同意,在整個專案中保持一致的樣式對於其長期可維護性非常重要。

規則詳情

此規則強制區塊使用一致的大括號樣式。

選項

此規則具有字串選項

  • "1tbs" (預設) 強制單一真大括號樣式
  • "stroustrup" 強制 Stroustrup 樣式
  • "allman" 強制 Allman 樣式

此規則有一個例外物件選項

  • "allowSingleLine": true (預設 false) 允許區塊的開頭和結尾大括號在同一行上

1tbs

此規則在使用預設 "1tbs" 選項時的不正確程式碼範例

在遊樂場中開啟
/*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();
    }
}

此規則在使用預設 "1tbs" 選項時的正確程式碼範例

在遊樂場中開啟
/*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();

此規則在使用 "1tbs", { "allowSingleLine": true } 選項時的正確程式碼範例

在遊樂場中開啟
/*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

此規則在使用 "stroustrup" 選項時的不正確程式碼範例

在遊樂場中開啟
/*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();
}

此規則在使用 "stroustrup" 選項時的正確程式碼範例

在遊樂場中開啟
/*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();

此規則在使用 "stroustrup", { "allowSingleLine": true } 選項時的正確程式碼範例

在遊樂場中開啟
/*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

此規則在使用 "allman" 選項時的不正確程式碼範例

在遊樂場中開啟
/*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();
}

此規則在使用 "allman" 選項時的正確程式碼範例

在遊樂場中開啟
/*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();

此規則在使用 "allman", { "allowSingleLine": true } 選項時的正確程式碼範例

在遊樂場中開啟
/*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(); } }

何時不應使用

如果您不想強制執行特定的大括號樣式,請不要啟用此規則。

版本

此規則在 ESLint v0.0.7 中引入。

延伸閱讀

資源

變更語言