# no-empty
禁止空块语句
配置文件中的 "extends": "eslint:recommended" 属性启用了该规则
空块语句虽然不是技术错误,但通常是由于未完成的重构而发生的。它们在阅读代码时会引起混乱。
# 规则详情
此规则不允许空块语句。此规则忽略包含注释的块语句(例如,在 try 语句的空 catch 或 finally 块中,以指示无论错误如何都应继续执行)。
此规则的错误代码示例:
/*eslint no-empty: "error"*/
if (foo) {
}
while (foo) {
}
switch(foo) {
}
try {
    doSomething();
} catch(ex) {
} finally {
}
此规则的正确代码示例:
/*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 } 选项的其他正确代码示例:
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
try {
    doSomething();
} catch (ex) {}
try {
    doSomething();
}
catch (ex) {}
finally {
    /* continue regardless of error */
}
# 何时不使用
如果您有意使用空块语句,则可以禁用此规则。
