Skip to content

noUselessLoneBlockStatements (since v1.3.3)

Diagnostic Category: lint/complexity/noUselessLoneBlockStatements

Sources:

Disallow unnecessary nested block statements.

In JavaScript, prior to ES6, standalone code blocks delimited by curly braces do not create a new scope and have no use. In ES6, code blocks may create a new scope if a block-level binding (let and const), a class declaration or a function declaration (in strict mode) are present. A block is not considered redundant in these cases.

{}
complexity/noUselessLoneBlockStatements.js:1:1 lint/complexity/noUselessLoneBlockStatements ━━━━━━━━━━

   This block statement doesn't serve any purpose and can be safely removed.
  
  > 1 │ {}
   ^^
    2 │ 
  
   Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
  
if (foo) {
bar();
{
baz();
}
}
complexity/noUselessLoneBlockStatements.js:3:3 lint/complexity/noUselessLoneBlockStatements  FIXABLE  ━━━━━━━━━━

   This block statement doesn't serve any purpose and can be safely removed.
  
    1 │ if (foo) {
    2 │   bar();
  > 3 │   {
     ^
  > 4 │     baz();
  > 5 │   }
     ^
    6 │ }
    7 │ 
  
   Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
  
   Safe fix: Remove redundant block.
  
    1 1  if (foo) {
    2 2    bar();
    3  - ··{
    4 3      baz();
    5  - ··}
    6 4  }
    7 5  
  
while (foo) {
bar();
}