Back to Biomejs

noWith

src/content/docs/linter/rules/no-with.mdx

latest3.7 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/noWith`](/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-with`](https://eslint.org/docs/latest/rules/no-with)

How to configure

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

Description

Disallow with statements in non-strict contexts.

The with statement is potentially problematic because it adds members of an object to the current scope, making it impossible to tell what a variable inside the block actually refers to.

Examples

Invalid

cjs
function f() {
  with (point) {
    r = Math.sqrt(x * x + y * y); // is r a member of point?
  }
}
<pre class="language-text"><code class="language-text">code-block.cjs:2:3 <a href="https://biomejs.dev/linter/rules/no-with">lint/suspicious/noWith</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Unexpected use of </span><span style="color: Tomato;"><strong>with</strong></span><span style="color: Tomato;"> statement.</span> <strong>1 │ </strong>function f() &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> with (point) &#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;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> r = Math.sqrt(x &#42; x + y &#42; y); // is r a member of point? <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> &#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong>&#125; <strong>6 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The with statement is potentially problematic because it adds members of an object to the current </span> <span style="color: lightgreen;">scope, making it impossible to tell what a variable inside the block actually refers to.</span> </code></pre> </TabItem> </Tabs>