src/content/docs/linter/rules/no-inner-declarations.mdx
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){
"linter": {
"rules": {
"correctness": {
"noInnerDeclarations": "error"
}
}
}
}
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.
if (test) {
function f() {}
}
if (test) {
var x = 1;
}
function f() {
if (test) {
function g() {}
}
}
function f() {
if (test) {
var x = 1;
}
}
// inside a module, function declarations are block-scoped and thus allowed.
if (test) {
function f() {}
}
export {}
function f() { }
function f() {
function g() {}
}
function f() {
var x = 1;
}
function f() {
if (test) {
const g = function() {};
}
}