Back to Biomejs

noShadow

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

latest8.8 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.0.0` - Diagnostic Category: [`lint/nursery/noShadow`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`no-shadow`](https://eslint.org/docs/latest/rules/no-shadow)

How to configure

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

Description

Disallow variable declarations from shadowing variables declared in the outer scope.

Shadowing is the process by which a local variable shares the same name as a variable in its containing scope. This can cause confusion while reading the code and make it impossible to access the global variable.

See also: noShadowRestrictedNames

Examples

Invalid

js
const foo = "bar";
if (true) {
   const foo = "baz";
}
<pre class="language-text"><code class="language-text">code-block.js:3:10 <a href="https://biomejs.dev/linter/rules/no-shadow">lint/nursery/noShadow</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This variable shadows another variable with the same name in the outer scope.</span> <strong>1 │ </strong>const foo = &quot;bar&quot;; <strong>2 │ </strong>if (true) &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> const foo = &quot;baz&quot;; <strong> │ </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>&#125; <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This is the shadowed variable, which is now inaccessible in the inner scope.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const foo = &quot;bar&quot;; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>if (true) &#123; <strong>3 │ </strong> const foo = &quot;baz&quot;; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider renaming this variable. It's easy to confuse the origin of variables if they share the same name.</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> </code></pre>

Variable declarations in functions can shadow variables in the outer scope:

js
const foo = "bar";
const bar = function () {
    const foo = 10;
}
<pre class="language-text"><code class="language-text">code-block.js:3:11 <a href="https://biomejs.dev/linter/rules/no-shadow">lint/nursery/noShadow</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This variable shadows another variable with the same name in the outer scope.</span> <strong>1 │ </strong>const foo = &quot;bar&quot;; <strong>2 │ </strong>const bar = function () &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> const foo = 10; <strong> │ </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>&#125; <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This is the shadowed variable, which is now inaccessible in the inner scope.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const foo = &quot;bar&quot;; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>const bar = function () &#123; <strong>3 │ </strong> const foo = 10; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider renaming this variable. It's easy to confuse the origin of variables if they share the same name.</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> </code></pre>

Function argument names can shadow variables in the outer scope:

js
const foo = "bar";
function bar(foo) {
    foo = 10;
}
<pre class="language-text"><code class="language-text">code-block.js:2:14 <a href="https://biomejs.dev/linter/rules/no-shadow">lint/nursery/noShadow</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This variable shadows another variable with the same name in the outer scope.</span> <strong>1 │ </strong>const foo = &quot;bar&quot;; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>function bar(foo) &#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>3 │ </strong> foo = 10; <strong>4 │ </strong>&#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This is the shadowed variable, which is now inaccessible in the inner scope.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const foo = &quot;bar&quot;; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>function bar(foo) &#123; <strong>3 │ </strong> foo = 10; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider renaming this variable. It's easy to confuse the origin of variables if they share the same name.</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> </code></pre>

Valid

js
const foo = "bar";
if (true) {
   const qux = "baz";
}
</TabItem> </Tabs>