Skip to main content

useBlockStatements

Requires following curly brace conventions. JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity.

Examples

Invalid

 if (x) x;
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:2
  
1   if (x) x;
    ---------

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1 +1 @@
0   | -  if (x) x;
  0 | +  if (x) {x;}

 if (x) {
x;
} else y;
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:2
    
1    if (x) {
2      x;
3    } else y;
   '

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1,3 +1,3 @@
0 0 |    if (x) {
1 1 |      x;
2   | -  } else y;
  2 | +  } else {y;}

if (x) {
x;
} else if (y) y;
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:3:8
  
3  } else if (y) y;
          ---------

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1,3 +1,3 @@
0 0 |   if (x) {
1 1 |     x;
2   | - } else if (y) y;
  2 | + } else if (y) {y;}

   for (;;);
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:4
  
1     for (;;);
      ---------

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1 +1 @@
0   | -    for (;;);
  0 | +    for (;;){}

   for (p in obj);
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:4
  
1     for (p in obj);
      ---------------

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1 +1 @@
0   | -    for (p in obj);
  0 | +    for (p in obj){}

  for (x of xs);
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:3
  
1    for (x of xs);
     --------------

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1 +1 @@
0   | -   for (x of xs);
  0 | +   for (x of xs){}

  do;
while (x);
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:3
    
1     do;
2     while (x);
   '

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1,2 +1,2 @@
0   | -   do;
  0 | +   do{}
1 1 |     while (x);

   while (x);
warning[useBlockStatements]: Block statements are preferred in this position.
   useBlockStatements.js:1:4
  
1     while (x);
      ----------

Suggested fix: Wrap the statement with a `JsBlockStatement`
    | @@ -1 +1 @@
0   | -    while (x);
  0 | +    while (x){}

  with (x);
error[SyntaxError]: `with` statements are not allowed in strict mode
   useBlockStatements.js:1:3
  
1    with (x);
     ^^^^^^^^^