Skip to content

noEmptyBlockStatements

诊断类别:lint/suspicious/noEmptyBlockStatements

¥Diagnostic Category: lint/suspicious/noEmptyBlockStatements

自从:v1.3.0 来源:

¥Since: v1.3.0 Sources:

禁止空块语句和静态块。

¥Disallow empty block statements and static blocks.

空静态块和块语句虽然在技术上不是错误,但通常是由于未完成的重构而发生的。它们可能会在阅读代码时造成混淆。

¥Empty static blocks and block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. They can cause confusion when reading code.

此规则不允许空块语句和静态块。此规则忽略包含注释的块语句或静态块(例如,在 try 语句的空 catch 或 finally 块中,以指示无论是否有错误都应继续执行)。

¥This rule disallows empty block statements and static blocks. This rule ignores block statements or static blocks 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

¥Invalid

function emptyFunctionBody () {}
code-block.js:1:31 lint/suspicious/noEmptyBlockStatements ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected empty block.

> 1 │ function emptyFunctionBody () {}
^^
2 │

Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.

try {
doSomething();
} catch(ex) {
}
code-block.js:3:13 lint/suspicious/noEmptyBlockStatements ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected empty block.

1 │ try {
2 │ doSomething();
> 3 │ } catch(ex) {
^
> 4 │
> 5 │ }
^
6 │

Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.

class Foo {
static {}
}
code-block.js:2:3 lint/suspicious/noEmptyBlockStatements ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected empty block.

1 │ class Foo {
> 2 │ static {}
^^^^^^^^^
3 │ }
4 │

Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.

¥Valid

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

¥Related links