Back to Biomejs

noRedeclare

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

latest8.5 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/noRedeclare`](/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-redeclare`](https://eslint.org/docs/latest/rules/no-redeclare) - Same as [`@typescript-eslint/no-redeclare`](https://typescript-eslint.io/rules/no-redeclare)

How to configure

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

Description

Disallow variable, function, class, and type redeclarations in the same scope.

Examples

Invalid

js
var a = 3;
var a = 10;
<pre class="language-text"><code class="language-text">code-block.js:2:5 <a href="https://biomejs.dev/linter/rules/no-redeclare">lint/suspicious/noRedeclare</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Shouldn't redeclare 'a'. Consider to delete it or rename it.</span> <strong>1 │ </strong>var a = 3; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>var a = 10; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">'a' is defined here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>var a = 3; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>var a = 10; <strong>3 │ </strong> </code></pre>
js
let a = 3;
let a = 10;
<pre class="language-text"><code class="language-text">code-block.js:2:5 <a href="https://biomejs.dev/linter/rules/no-redeclare">lint/suspicious/noRedeclare</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Shouldn't redeclare 'a'. Consider to delete it or rename it.</span> <strong>1 │ </strong>let a = 3; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>let a = 10; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">'a' is defined here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>let a = 3; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>let a = 10; <strong>3 │ </strong> </code></pre>
js
function f() {}
function f() {}
<pre class="language-text"><code class="language-text">code-block.js:2:10 <a href="https://biomejs.dev/linter/rules/no-redeclare">lint/suspicious/noRedeclare</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Shouldn't redeclare 'f'. Consider to delete it or rename it.</span> <strong>1 │ </strong>function f() &#123;&#125; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>function f() &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">'f' is defined here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f() &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>function f() &#123;&#125; <strong>3 │ </strong> </code></pre>
js
class C {
    static {
        var c = 3;
        var c = 10;
    }
}
<pre class="language-text"><code class="language-text">code-block.js:4:13 <a href="https://biomejs.dev/linter/rules/no-redeclare">lint/suspicious/noRedeclare</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Shouldn't redeclare 'c'. Consider to delete it or rename it.</span> <strong>2 │ </strong> static &#123; <strong>3 │ </strong> var c = 3; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> var c = 10; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> &#125; <strong>6 │ </strong>&#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">'c' is defined here:</span> <strong>1 │ </strong>class C &#123; <strong>2 │ </strong> static &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> var c = 3; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>4 │ </strong> var c = 10; <strong>5 │ </strong> &#125; </code></pre>
ts
type Person = { name: string; }
class Person { name: string; }
<pre class="language-text"><code class="language-text">code-block.ts:2:7 <a href="https://biomejs.dev/linter/rules/no-redeclare">lint/suspicious/noRedeclare</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Shouldn't redeclare 'Person'. Consider to delete it or rename it.</span> <strong>1 │ </strong>type Person = &#123; name: string; &#125; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>class Person &#123; name: string; &#125; <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> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">'Person' is defined here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>type Person = &#123; name: string; &#125; <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>2 │ </strong>class Person &#123; name: string; &#125; <strong>3 │ </strong> </code></pre>

Valid

js
var a = 3;
a = 10;
ts
class Foo {
    bar(a: A);
    bar(a: A, b: B);
    bar(a: A, b: B) {}
}
</TabItem> </Tabs>