版本

no-empty

禁止空的區塊陳述式

建議

設定檔中使用來自 @eslint/jsrecommended 設定會啟用此規則

💡 有建議

此規則回報的一些問題可以透過編輯器的建議手動修正

空的區塊陳述式雖然技術上不是錯誤,但通常是因為未完成的重構而發生。它們可能會在閱讀程式碼時造成混淆。

規則詳細資訊

此規則禁止空的區塊陳述式。此規則會忽略包含註解的區塊陳述式(例如,在 try 陳述式的空的 catchfinally 區塊中,以指示無論錯誤如何都應繼續執行)。

此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-empty: "error"*/

if (foo) {
}

while (foo) {
}

switch(foo) {
}

try {
    doSomething();
} catch(ex) {

} finally {

}

此規則的正確程式碼範例

在 Playground 中開啟
/*eslint no-empty: "error"*/

if (foo) {
    // empty
}

while (foo) {
    /* empty */
}

try {
    doSomething();
} catch (ex) {
    // continue regardless of error
}

try {
    doSomething();
} finally {
    /* continue regardless of error */
}

選項

此規則有一個物件選項用於例外情況

  • "allowEmptyCatch": true 允許空的 catch 子句(即不包含註解的子句)

allowEmptyCatch

使用 { "allowEmptyCatch": true } 選項時,此規則的其他正確程式碼範例

在 Playground 中開啟
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
try {
    doSomething();
} catch (ex) {}

try {
    doSomething();
}
catch (ex) {}
finally {
    /* continue regardless of error */
}

何時不該使用

如果您有意使用空的區塊陳述式,則可以停用此規則。

版本

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

資源

變更語言