useBlockStatementOverIife (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/useBlockStatementOverIife - This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useBlockStatementOverIife": "error" } } }}Description
Section titled “Description”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
returnstatement, avardeclaration, a call toeval, or a directive such as"use strict"; - the function is declared with the
functionkeyword and usesthis,arguments, ornew.target; - the function contains a function declaration, and the file is a script instead of a
module, such as a
.cjsfile.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”(() => { 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);})();Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.