Back to Biomejs

noUselessLoneBlockStatements

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

latest5.4 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.3` - Diagnostic Category: [`lint/complexity/noUselessLoneBlockStatements`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-lone-blocks`](https://eslint.org/docs/latest/rules/no-lone-blocks)

How to configure

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noUselessLoneBlockStatements": "error"
			}
		}
	}
}

Description

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.

Examples

Invalid

js
{}
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-useless-lone-block-statements">lint/complexity/noUselessLoneBlockStatements</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This block statement doesn't serve any purpose and can be safely removed.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&#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;">Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.</span> </code></pre>
js
if (foo) {
  bar();
  {
    baz();
  }
}
<pre class="language-text"><code class="language-text">code-block.js:3:3 <a href="https://biomejs.dev/linter/rules/no-useless-lone-block-statements">lint/complexity/noUselessLoneBlockStatements</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This block statement doesn't serve any purpose and can be safely removed.</span> <strong>1 │ </strong>if (foo) &#123; <strong>2 │ </strong> bar(); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> baz(); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong> &#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>6 │ </strong>&#125; <strong>7 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove redundant block.</span> <strong>1</strong> <strong>1</strong><strong> │ </strong> if (foo) &#123; <strong>2</strong> <strong>2</strong><strong> │ </strong> bar(); <strong>3</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;"><span style="opacity: 0.8;"><strong>·</strong></span></span><span style="color: Tomato;"><span style="opacity: 0.8;"><strong>·</strong></span></span><span style="color: Tomato;"><strong>&#123;</strong></span> <strong>4</strong> <strong>3</strong><strong> │ </strong> baz(); <strong>5</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;"><span style="opacity: 0.8;"><strong>·</strong></span></span><span style="color: Tomato;"><span style="opacity: 0.8;"><strong>·</strong></span></span><span style="color: Tomato;"><strong>&#125;</strong></span> <strong>6</strong> <strong>4</strong><strong> │ </strong> &#125; <strong>7</strong> <strong>5</strong><strong> │ </strong> </code></pre>

Valid

js
while (foo) {
  bar();
}
</TabItem> </Tabs>