Disallow Empty Block Statements (no-empty)

Empty statements usually occur due to refactoring that wasn’t completed, such as:

if (foo) {
}

Empty block statements such as this are usually an indicator of an error, or at the very least, an indicator that some refactoring is likely needed.

Rule Details

This rule is aimed at eliminating empty block statements. While not technically an error, empty block statements can be a source of confusion when reading code. A block will not be considered a warning if it contains a comment line.

The following patterns are considered problems:

/*eslint no-empty: 2*/

if (foo) {         /*error Empty block statement.*/
}

while (foo) {      /*error Empty block statement.*/
}

switch(foo) {      /*error Empty switch statement.*/
}

try {
    doSomething();
} catch(ex) {      /*error Empty block statement.*/

} finally {        /*error Empty block statement.*/

}

The following patterns are not considered problems:

/*eslint no-empty: 2*/

if (foo) {
    // empty
}

while (foo) {
    // test
}

try {
    doSomething();
} catch (ex) {
    // Do nothing
}

try {
    doSomething();
} finally {
    // Do nothing
}

Since you must always have at least a catch or a finally block for any try, it is common to have empty statements when execution should continue regardless of error.

When Not To Use It

If you intentionally use empty statements then you can disable this rule.

Version

This rule was introduced in ESLint 0.0.2.

Resources