Skip to content

useBlockStatementOverIife (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useBlockStatementOverIife": "error"
}
}
}
}

Prefer block statements over functions that are called immediately only to create a scope.

A function that is called as soon as it is created is known as an immediately invoked function expression (IIFE). Before let and const existed, wrapping code in such a function was the only way to stop its variables from being visible to the code around it.

Variables declared with let, const, or class are only visible inside the block statement (code wrapped in { and }) that contains them. A block statement therefore hides variables in the same way, with less code and without a function call.

This rule only reports functions that a block statement can replace without changing how the code behaves. It ignores a function that is called immediately when:

  • the result of the call is used, or the call passes arguments;
  • the function has a name, has parameters, is async, or is a generator;
  • the function contains a return statement, a var declaration, a call to eval, or a directive such as "use strict";
  • the function is declared with the function keyword and uses this, arguments, or new.target;
  • the function contains a function declaration, and the file is a script instead of a module, such as a .cjs file.
(() => {
const value = getValue();
run(value);
})();
code-block.js:1:1 lint/nursery/useBlockStatementOverIife  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function is called immediately, and a block statement can replace it.

> 1 │ (() => {
│ ^^^^^^^^
2 │ const value = getValue();
3 │ run(value);

ℹ A block statement hides let, const, and class declarations from the surrounding code in the same way, with less code and without a function call.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Use a block statement instead.

1 │ - (()·=>·{
1 │ + {
2 2 │ const value = getValue();
3 3 │ run(value);
4 │ - })();
4 │ + }
5 5 │

(function () {
const value = getValue();
run(value);
})();
code-block.js:1:1 lint/nursery/useBlockStatementOverIife  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function is called immediately, and a block statement can replace it.

> 1 │ (function () {
│ ^^^^^^^^^^^^^^
2 │ const value = getValue();
3 │ run(value);

ℹ A block statement hides let, const, and class declarations from the surrounding code in the same way, with less code and without a function call.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Use a block statement instead.

1 │ - (function·()·{
1 │ + {
2 2 │ const value = getValue();
3 3 │ run(value);
4 │ - })();
4 │ + }
5 5 │

{
const value = getValue();
run(value);
}
const result = (() => {
return getValue();
})();
(async () => {
await run();
})();
(function () {
this.run();
})();
(() => {
var count = 1;
run(count);
})();