no-inner-declarations
Disallow function and, optionally, var declarations in nested blocks
Outside strict mode, a function declared inside an if or a loop is also visible after the block, but it only has a value once the block has run. Whether a call works depends on which branch was taken.
In strict mode the function belongs to its block, like a let, so by default it isn't reported there. Modules and class bodies are always strict. That leaves CommonJS files and scripts without "use strict".
Configure#
Off by default. To turn it on:
[lint.rules]
"no-inner-declarations" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"no-inner-declarations" = [
"error",
"functions", # "functions" or "both"
{
blockScopedFunctions = "allow", # "allow" or "disallow"
},
]Examples#
if (process.env.DEBUG) {
function log(message) {Move function declaration to program root.
console.log(message);
}
}
log("ready"); // TypeError unless DEBUG is setfunction log(message) {
if (process.env.DEBUG) {
console.log(message);
}
}
log("ready");Not reported either, because the file is in strict mode and log stays in its block:
"use strict";
if (process.env.DEBUG) {
function log(message) {
console.log(message);
}
log("ready");
}Options#
[lint.rules]
no-inner-declarations = ["error", "both", { blockScopedFunctions = "disallow" }]The first option says what to look at:
| Option | What it does |
|---|---|
"functions" (default) | Report function declarations in nested blocks. |
"both" | Also report var declarations in nested blocks. |
A var belongs to the whole function, no matter which block it's written in:
"both"1 problemif (user.isAdmin) {
var role = "admin";Move variable declaration to program root.
}"both"No problemslet role;
if (user.isAdmin) {
role = "admin";
}The second option is an object:
| Option | Default | What it does |
|---|---|---|
blockScopedFunctions | "allow" | With "disallow", report functions in nested blocks in strict mode too. |
namespaces | "disallow" | Only for an oxlint config, where the rule also reports declarations directly inside a TypeScript namespace. "allow" turns that off. bunfig.toml rejects this option. |
"functions", { "blockScopedFunctions": "disallow" }1 problemif (process.env.DEBUG) {
function log(message) {Move function declaration to program root.
console.log(message);
}
log("ready");
}