Back to Biomejs

noDivRegex

src/content/docs/linter/rules/no-div-regex.mdx

latest4.4 KB
Original Source

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

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.12` - Diagnostic Category: [`lint/nursery/noDivRegex`](/reference/diagnostics#diagnostic-category) - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-div-regex`](https://eslint.org/docs/latest/rules/no-div-regex)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noDivRegex": "error"
			}
		}
	}
}

Description

Disallow equal signs explicitly at the beginning of regular expressions.

This rule forbids equal signs (=) after the slash (/) at the beginning of a regular expression literal, because the characters /= can be confused with a division assignment operator.

Examples

Invalid

js
function bar() {
  return /=foo/;
}
<pre class="language-text"><code class="language-text">code-block.js:2:10 <a href="https://biomejs.dev/linter/rules/no-div-regex">lint/nursery/noDivRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Avoid using an equal sign directly after the slash at the beginning of a regular expression literal.</span> <strong>1 │ </strong>function bar() &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> return /=foo/; <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>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The characters </span><span style="color: lightgreen;"><strong>/=</strong></span><span style="color: lightgreen;"> can be confused with a division assignment operator. Replace the equal sign (</span><span style="color: lightgreen;"><strong>=</strong></span><span style="color: lightgreen;">) with </span><span style="color: lightgreen;"><strong>[=]</strong></span><span style="color: lightgreen;"> to prevent confusion.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Replace with </span><span style="color: lightgreen;"><strong>[=]</strong></span><span style="color: lightgreen;">.</span> <strong> 2 │ </strong><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span>return<span style="opacity: 0.8;">·</span>/<span style="color: MediumSeaGreen;">[</span>=<span style="color: MediumSeaGreen;">]</span>foo/; <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

js
function bar() {
  return /[=]foo/;
}
</TabItem> </Tabs>