noLoopFunc
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noLoopFunc - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-loop-func - Same as
@typescript-eslint/no-loop-func
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noLoopFunc": "error" } } }}Description
Section titled “Description”Disallow functions declared inside loops that capture unsafe outer variables.
Functions created in loops can easily observe values from a later iteration instead of the iteration where they were created. This rule reports functions that capture outer bindings which may be reassigned while the loop continues.
The rule ignores plain immediately invoked function expressions (IIFEs), but still reports async, generator, and self-referential IIFEs because they can escape the current iteration.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”Using var for the iteration variable creates a single binding shared across all iterations, so it’s unsafe to capture.
for (var i = 0; i < 10; i++) { handlers.push(() => i);}code-block.js:2:19 lint/nursery/noLoopFunc ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function declared in a loop contains unsafe references to outer variables.
1 │ for (var i = 0; i < 10; i++) {
> 2 │ handlers.push(() => i);
│ ^^^^^^^
3 │ }
4 │
ℹ Loop-created functions often run after the loop continues, but captured outer bindings like var variables are reused instead of copied per iteration.
ℹ The following variables were detected: i
ℹ Move the function outside the loop, capture only per-iteration bindings, or avoid mutating the captured variable across iterations.
ℹ 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.
let value = 0;for (let i = 0; i < 10; i++) { queue.push(function () { return value; }); value += 1;}code-block.js:3:16 lint/nursery/noLoopFunc ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function declared in a loop contains unsafe references to outer variables.
1 │ let value = 0;
2 │ for (let i = 0; i < 10; i++) {
> 3 │ queue.push(function () {
│ ^^^^^^^^^^^^^
> 4 │ return value;
> 5 │ });
│ ^
6 │ value += 1;
7 │ }
ℹ Loop-created functions often run after the loop continues, but captured outer bindings like var variables are reused instead of copied per iteration.
ℹ The following variables were detected: value
ℹ Move the function outside the loop, capture only per-iteration bindings, or avoid mutating the captured variable across iterations.
ℹ 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.
Using let or const for the iteration variable creates a fresh binding each iteration, so it’s safe to capture.
for (let i = 0; i < 10; i++) { handlers.push(() => i);}for (var i = 0; i < 10; i++) { const current = i; queue.push(function() { return current; });}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.