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 */
}
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": true
allows emptycatch
clauses (that is, which do not contain a comment)
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 中引入。