Back to Biomejs

noNonoctalDecimalEscape

src/content/docs/linter/rules/no-nonoctal-decimal-escape.mdx

latest8.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.0.0` - Diagnostic Category: [`lint/correctness/noNonoctalDecimalEscape`](/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 [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-nonoctal-decimal-escape`](https://eslint.org/docs/latest/rules/no-nonoctal-decimal-escape)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"noNonoctalDecimalEscape": "error"
			}
		}
	}
}

Description

Disallow \8 and \9 escape sequences in string literals.

Since ECMAScript 2021, the escape sequences \8 and \9 have been defined as non-octal decimal escape sequences. However, most JavaScript engines consider them to be "useless" escapes. For example:

js
"\8" === "8"; // true
"\9" === "9"; // true

Although this syntax is deprecated, it is still supported for compatibility reasons. If the ECMAScript host is not a web browser, this syntax is optional. However, web browsers are still required to support it, but only in non-strict mode. Regardless of your targeted environment, it is recommended to avoid using these escape sequences in new code.

Examples

Invalid

js
const x = "\8";
<pre class="language-text"><code class="language-text">code-block.js:1:12 <a href="https://biomejs.dev/linter/rules/no-nonoctal-decimal-escape">lint/correctness/noNonoctalDecimalEscape</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Don't use </span><span style="color: Tomato;"><strong>&#96;&#92;8&#96;</strong></span><span style="color: Tomato;"> and </span><span style="color: Tomato;"><strong>&#96;&#92;9&#96;</strong></span><span style="color: Tomato;"> escape sequences in string literals.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const x = &quot;&#92;8&quot;; <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;">The nonoctal decimal escape is a deprecated syntax that is left for compatibility and should not be used.</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 </span><span style="color: lightgreen;"><strong>&#92;8</strong></span><span style="color: lightgreen;"> with </span><span style="color: lightgreen;"><strong>8</strong></span><span style="color: lightgreen;">. This maintains the current functionality.</span> <strong> 1 │ </strong>const<span style="opacity: 0.8;">·</span>x<span style="opacity: 0.8;">·</span>=<span style="opacity: 0.8;">·</span>&quot;<span style="color: Tomato;">&#92;</span>8&quot;; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>
js
const x = "Don't use \8 escape.";
<pre class="language-text"><code class="language-text">code-block.js:1:22 <a href="https://biomejs.dev/linter/rules/no-nonoctal-decimal-escape">lint/correctness/noNonoctalDecimalEscape</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Don't use </span><span style="color: Tomato;"><strong>&#96;&#92;8&#96;</strong></span><span style="color: Tomato;"> and </span><span style="color: Tomato;"><strong>&#96;&#92;9&#96;</strong></span><span style="color: Tomato;"> escape sequences in string literals.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const x = &quot;Don't use &#92;8 escape.&quot;; <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;">The nonoctal decimal escape is a deprecated syntax that is left for compatibility and should not be used.</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 </span><span style="color: lightgreen;"><strong>&#92;8</strong></span><span style="color: lightgreen;"> with </span><span style="color: lightgreen;"><strong>8</strong></span><span style="color: lightgreen;">. This maintains the current functionality.</span> <strong> 1 │ </strong>const<span style="opacity: 0.8;">·</span>x<span style="opacity: 0.8;">·</span>=<span style="opacity: 0.8;">·</span>&quot;Don't<span style="opacity: 0.8;">·</span>use<span style="opacity: 0.8;">·</span><span style="color: Tomato;">&#92;</span>8<span style="opacity: 0.8;">·</span>escape.&quot;; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>
js
const x = "Don't use \9 escape.";
<pre class="language-text"><code class="language-text">code-block.js:1:22 <a href="https://biomejs.dev/linter/rules/no-nonoctal-decimal-escape">lint/correctness/noNonoctalDecimalEscape</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Don't use </span><span style="color: Tomato;"><strong>&#96;&#92;8&#96;</strong></span><span style="color: Tomato;"> and </span><span style="color: Tomato;"><strong>&#96;&#92;9&#96;</strong></span><span style="color: Tomato;"> escape sequences in string literals.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const x = &quot;Don't use &#92;9 escape.&quot;; <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;">The nonoctal decimal escape is a deprecated syntax that is left for compatibility and should not be used.</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 </span><span style="color: lightgreen;"><strong>&#92;9</strong></span><span style="color: lightgreen;"> with </span><span style="color: lightgreen;"><strong>9</strong></span><span style="color: lightgreen;">. This maintains the current functionality.</span> <strong> 1 │ </strong>const<span style="opacity: 0.8;">·</span>x<span style="opacity: 0.8;">·</span>=<span style="opacity: 0.8;">·</span>&quot;Don't<span style="opacity: 0.8;">·</span>use<span style="opacity: 0.8;">·</span><span style="color: Tomato;">&#92;</span>9<span style="opacity: 0.8;">·</span>escape.&quot;; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>

Valid

js
const x = "8";
js
const x = "Don't use \\8 and \\9 escapes.";
</TabItem> </Tabs>