no-lone-blocks
Disallow blocks that do nothing
Braces that aren't the body of an if, a loop or a function only do something if they contain a let, const or class declaration, or a function declaration in strict mode. Those are scoped to the block. Otherwise the braces change nothing. They're often left over after a condition was deleted, so a reader wonders what's missing.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-lone-blocks" = "error" # or "warn"Examples#
example.js2 problems
{Block is redundant.
loadConfig();
}
function start() {
connect();
{Nested block is redundant.
listen();
}
}example.jsNo problems
loadConfig();
function start() {
connect();
listen();
}Not reported, because started only exists inside the braces:
example.jsNo problems
{
const started = Date.now();
loadConfig();
console.log(Date.now() - started);
}Braces around all the statements in a case are fine too:
example.jsNo problems
switch (event.type) {
case "open": {
open(event.id);
break;
}
}A block that's the only thing in a function or in another block is reported even if it declares something. The braces around it already limit the scope:
example.js1 problem
function start() {
{Nested block is redundant.
const port = 3000;
listen(port);
}
}