Back to Biomejs

noConfusingLabels

src/content/docs/linter/rules/no-confusing-labels.mdx

latest7.5 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.0.0` - Diagnostic Category: [`lint/suspicious/noConfusingLabels`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Inspired from [`no-labels`](https://eslint.org/docs/latest/rules/no-labels)

How to configure

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

Description

Disallow labeled statements that are not loops.

Labeled statements in JavaScript are used in conjunction with break and continue to control flow around multiple loops. Their use for other statements is suspicious and unfamiliar.

The rule ignores reactive Svelte statements in Svelte components.

Examples

Invalid

js
label: f();
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-confusing-labels">lint/suspicious/noConfusingLabels</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>label</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>label: f(); <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Only loops should be labeled. </span> <span style="color: lightgreen;">The use of labels for other statements is suspicious and unfamiliar.</span> </code></pre>
js
label: {
    f();
    break label;
}
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-confusing-labels">lint/suspicious/noConfusingLabels</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>label</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>label: &#123; <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>2 │ </strong> f(); <strong>3 │ </strong> break label; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Only loops should be labeled. </span> <span style="color: lightgreen;">The use of labels for other statements is suspicious and unfamiliar.</span> </code></pre>
js
label: if (a) {
    f()
    break label;
}
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-confusing-labels">lint/suspicious/noConfusingLabels</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>label</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>label: if (a) &#123; <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>2 │ </strong> f() <strong>3 │ </strong> break label; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Only loops should be labeled. </span> <span style="color: lightgreen;">The use of labels for other statements is suspicious and unfamiliar.</span> </code></pre>
js
label: switch (a) {
    case 0:
        break label;
}
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-confusing-labels">lint/suspicious/noConfusingLabels</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>label</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>label: switch (a) &#123; <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>2 │ </strong> case 0: <strong>3 │ </strong> break label; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Only loops should be labeled. </span> <span style="color: lightgreen;">The use of labels for other statements is suspicious and unfamiliar.</span> </code></pre>

Valid

js
outer: while (a) {
    while(b) {
        break outer;
    }
}
svelte
<script>
$: { /* reactive block */ }
</script>

Options

Use the options to allow specific labels in your code. Labels can be used to mark code that should be removed under certain conditions, such as in production builds. Some bundlers, such as esbuild and Vite, can be configured to remove labeled statements.

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"noConfusingLabels": {
					"options": {
						"allowedLabels": [
							"DEV"
						]
					}
				}
			}
		}
	}
}

js
DEV: assertSomeCondition();
</TabItem> </Tabs>