Back to Biomejs

noFallthroughSwitchClause

src/content/docs/linter/rules/no-fallthrough-switch-clause.mdx

latest3.3 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/noFallthroughSwitchClause`](/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 [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-fallthrough`](https://eslint.org/docs/latest/rules/no-fallthrough)

How to configure

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

Description

Disallow fallthrough of switch clauses.

Switch clauses in switch statements fall through by default. This can lead to unexpected behavior when forgotten.

The rule doesn't take process.exit() in consideration.

Examples

Invalid

js
switch (bar) {
	case 0:
		a();
	case 1:
		b();
}
<pre class="language-text"><code class="language-text">code-block.js:2:2 <a href="https://biomejs.dev/linter/rules/no-fallthrough-switch-clause">lint/suspicious/noFallthroughSwitchClause</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This case is falling through to the next case.</span> <strong>1 │ </strong>switch (bar) &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> case 0: <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;">&gt;</span></strong> <strong>3 │ </strong> a(); <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>4 │ </strong> case 1: <strong>5 │ </strong> b(); <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Add a &#96;break&#96; or &#96;return&#96; statement to the end of this case to prevent fallthrough.</span> </code></pre>

Valid

js
switch (foo) {
	case 1:
    case 2:
		doSomething();
		break;
    case 3: {
        if (cond) {
            break;
        } else {
            break;
        }
    }
	case 4:
		doSomething();
}
</TabItem> </Tabs>