Back to Biomejs

noEmptyBlockStatements

src/content/docs/linter/rules/no-empty-block-statements.mdx

latest6.0 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.3.0` - Diagnostic Category: [`lint/suspicious/noEmptyBlockStatements`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`no-empty`](https://eslint.org/docs/latest/rules/no-empty) - Same as [`no-empty-static-block`](https://eslint.org/docs/latest/rules/no-empty-static-block) - Same as [`no-empty-function`](https://eslint.org/docs/latest/rules/no-empty-function) - Same as [`@typescript-eslint/no-empty-function`](https://typescript-eslint.io/rules/no-empty-function)

How to configure

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"noEmptyBlockStatements": "error"
			}
		}
	}
}

Description

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.

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

js
function emptyFunctionBody () {}
<pre class="language-text"><code class="language-text">code-block.js:1:31 <a href="https://biomejs.dev/linter/rules/no-empty-block-statements">lint/suspicious/noEmptyBlockStatements</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected empty block.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function emptyFunctionBody () &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.</span> </code></pre>
js
try {
    doSomething();
} catch(ex) {

}
<pre class="language-text"><code class="language-text">code-block.js:3:13 <a href="https://biomejs.dev/linter/rules/no-empty-block-statements">lint/suspicious/noEmptyBlockStatements</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected empty block.</span> <strong>1 │ </strong>try &#123; <strong>2 │ </strong> doSomething(); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong>&#125; catch(ex) &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong>&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>6 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.</span> </code></pre>
js
class Foo {
  static {}
}
<pre class="language-text"><code class="language-text">code-block.js:2:3 <a href="https://biomejs.dev/linter/rules/no-empty-block-statements">lint/suspicious/noEmptyBlockStatements</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected empty block.</span> <strong>1 │ </strong>class Foo &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> static &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.</span> </code></pre>

Valid

js
function foo () {
    doSomething();
}
js
try {
  doSomething();
} catch (ex) {
  // continue regardless of error
}
</TabItem> </Tabs>