no-empty
不允许空块语句
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
此规则报告的一些问题可通过编辑器 建议 手动修复
空的块语句虽然在技术上不是错误,但通常是由于重构未完成而产生的。在阅读代码时,它们可能会造成困惑。
🌐 Empty block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. They can cause confusion when reading code.
规则详情
🌐 Rule Details
此规则不允许空块语句。此规则会忽略包含注释的块语句(例如,在 try 语句的空 catch 或 finally 块中,以表示无论是否出现错误,都应继续执行)。
🌐 This rule disallows empty block statements. This rule ignores block statements which contain a comment (for example, in an empty catch or finally block of a try statement to indicate that execution should continue regardless of errors).
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-empty: "error"*/
if (foo) {
}
while (foo) {
}
switch(foo) {
}
try {
doSomething();
} catch(ex) {
} finally {
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-empty: "error"*/
if (foo) {
// empty
}
while (foo) {
/* empty */
}
switch(foo) {
/* empty */
}
try {
doSomething();
} catch (ex) {
// continue regardless of error
}
try {
doSomething();
} finally {
/* continue regardless of error */
}
选项
🌐 Options
此规则有一个异常对象选项:
🌐 This rule has an object option for exceptions:
"allowEmptyCatch": true允许空的catch子句(也就是说,不包含注释的子句)
allowEmptyCatch
使用 { "allowEmptyCatch": true } 选项时,此规则的其他 正确 代码示例:
🌐 Examples of additional correct code for this rule with the { "allowEmptyCatch": true } option:
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
try {
doSomething();
} catch (ex) {}
try {
doSomething();
}
catch (ex) {}
finally {
/* continue regardless of error */
}
何时不使用
🌐 When Not To Use It
如果你有意使用空块语句,则可以禁用此规则。
🌐 If you intentionally use empty block statements then you can disable this rule.
相关规则
版本
此规则是在 ESLint v0.0.2 中引入。