Back to Biomejs

useDefaultSwitchClause

src/content/docs/linter/rules/use-default-switch-clause.mdx

latest3.8 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.7.2` - Diagnostic Category: [`lint/style/useDefaultSwitchClause`](/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 [`default-case`](https://eslint.org/docs/latest/rules/default-case)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"useDefaultSwitchClause": "error"
			}
		}
	}
}

Description

Require the default clause in switch statements.

Some code conventions require that all switch statements have a default clause. The thinking is that it’s better to always explicitly state what the default behavior should be so that it’s clear whether or not the developer forgot to include the default behavior by mistake.

Examples

Invalid

js
switch (a) {
    case 1:
        /* code */
        break;
}
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/use-default-switch-clause">lint/style/useDefaultSwitchClause</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Expected a default switch clause.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>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><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>2 │ </strong> case 1: <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> /&#42; code &#42;/ <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> break; <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;">The lack of a default clause can be a possible omission.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider adding a default clause.</span> </code></pre>

Valid

js
switch (a) {
    case 1:
        /* code */
        break;
    default:
        /* code */
        break;
}
</TabItem> </Tabs>