no-empty
禁止空的區塊陳述式
空的區塊陳述式雖然技術上不是錯誤,但通常是因為未完成的重構而發生。它們可能會在閱讀程式碼時造成混淆。
規則詳細資訊
此規則禁止空的區塊陳述式。此規則會忽略包含註解的區塊陳述式(例如,在 try
陳述式的空的 catch
或 finally
區塊中,以指示無論錯誤如何都應繼續執行)。
此規則的錯誤程式碼範例
在 Playground 中開啟
/*eslint no-empty: "error"*/
if (foo)
while (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 中引入。