Back to Biomejs

noInnerDeclarations

src/content/docs/linter/rules/no-inner-declarations.mdx

latest11.2 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/noInnerDeclarations`](/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-inner-declarations`](https://eslint.org/docs/latest/rules/no-inner-declarations)

How to configure

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

Description

Disallow function and var declarations that are accessible outside their block.

A var is accessible in the whole body of the nearest root (function, module, script, static block). To avoid confusion, they should be declared to the nearest root.

Prior to ES2015, function declarations were only allowed in the nearest root, though parsers sometimes erroneously accept them elsewhere. In ES2015, inside an ES module, a function declaration is always block-scoped.

Note that const and let declarations are block-scoped, and therefore they are not affected by this rule. Moreover, function declarations in nested blocks are allowed inside ES modules.

Examples

Invalid

cjs
if (test) {
    function f() {}
}
<pre class="language-text"><code class="language-text">code-block.cjs:2:5 <a href="https://biomejs.dev/linter/rules/no-inner-declarations">lint/correctness/noInnerDeclarations</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>function</strong></span><span style="color: Tomato;"> should be declared at the root of the </span><span style="color: Tomato;"><strong>script</strong></span><span style="color: Tomato;">.</span> <strong>1 │ </strong>if (test) &#123; <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><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>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>function</strong></span><span style="color: lightgreen;"> is accessible in the whole body of the </span><span style="color: lightgreen;"><strong>script</strong></span><span style="color: lightgreen;">. </span> <span style="color: lightgreen;">To avoid confusion, it should be declared at the root of the </span><span style="color: lightgreen;"><strong>script</strong></span><span style="color: lightgreen;">.</span> </code></pre>
js
if (test) {
    var x = 1;
}
<pre class="language-text"><code class="language-text">code-block.js:2:5 <a href="https://biomejs.dev/linter/rules/no-inner-declarations">lint/correctness/noInnerDeclarations</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>var</strong></span><span style="color: Tomato;"> should be declared at the root of the </span><span style="color: Tomato;"><strong>module</strong></span><span style="color: Tomato;">.</span> <strong>1 │ </strong>if (test) &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> var x = 1; <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>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>var</strong></span><span style="color: lightgreen;"> is accessible in the whole body of the </span><span style="color: lightgreen;"><strong>module</strong></span><span style="color: lightgreen;">. </span> <span style="color: lightgreen;">To avoid confusion, it should be declared at the root of the </span><span style="color: lightgreen;"><strong>module</strong></span><span style="color: lightgreen;">.</span> </code></pre>
cjs
function f() {
    if (test) {
        function g() {}
    }
}
<pre class="language-text"><code class="language-text">code-block.cjs:3:9 <a href="https://biomejs.dev/linter/rules/no-inner-declarations">lint/correctness/noInnerDeclarations</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>function</strong></span><span style="color: Tomato;"> should be declared at the root of the </span><span style="color: Tomato;"><strong>enclosing function</strong></span><span style="color: Tomato;">.</span> <strong>1 │ </strong>function f() &#123; <strong>2 │ </strong> if (test) &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> function g() &#123;&#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><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>4 │ </strong> &#125; <strong>5 │ </strong>&#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>function</strong></span><span style="color: lightgreen;"> is accessible in the whole body of the </span><span style="color: lightgreen;"><strong>enclosing function</strong></span><span style="color: lightgreen;">. </span> <span style="color: lightgreen;">To avoid confusion, it should be declared at the root of the </span><span style="color: lightgreen;"><strong>enclosing function</strong></span><span style="color: lightgreen;">.</span> </code></pre>
js
function f() {
    if (test) {
        var x = 1;
    }
}
<pre class="language-text"><code class="language-text">code-block.js:3:9 <a href="https://biomejs.dev/linter/rules/no-inner-declarations">lint/correctness/noInnerDeclarations</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>var</strong></span><span style="color: Tomato;"> should be declared at the root of the </span><span style="color: Tomato;"><strong>enclosing function</strong></span><span style="color: Tomato;">.</span> <strong>1 │ </strong>function f() &#123; <strong>2 │ </strong> if (test) &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> var x = 1; <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>4 │ </strong> &#125; <strong>5 │ </strong>&#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>var</strong></span><span style="color: lightgreen;"> is accessible in the whole body of the </span><span style="color: lightgreen;"><strong>enclosing function</strong></span><span style="color: lightgreen;">. </span> <span style="color: lightgreen;">To avoid confusion, it should be declared at the root of the </span><span style="color: lightgreen;"><strong>enclosing function</strong></span><span style="color: lightgreen;">.</span> </code></pre>

Valid

js
// inside a module, function declarations are block-scoped and thus allowed.
if (test) {
    function f() {}
}
export {}
js
function f() { }
js
function f() {
    function g() {}
}
js
function f() {
    var x = 1;
}
js
function f() {
    if (test) {
        const g = function() {};
    }
}
</TabItem> </Tabs>