no-loop-statement
Disallow imperative loop statements.
Targeted pattern scopeâ
This rule targets for, for..in, for..of, while, and do..while statements.
What this rule reportsâ
- Loop statements that mutate control flow imperatively
Why this rule existsâ
Declarative transforms (map, filter, reduce) are usually easier to reason about in immutable systems.
â Incorrectâ
for (const item of items) {
sum += item;
}
â Correctâ
const sum = items.reduce((acc, item) => acc + item, 0);
Additional examplesâ
// â Imperative loop with mutable accumulator
let totalByUser = 0;
for (const order of orders) {
if (order.userId === currentUserId) {
totalByUser += order.total;
}
}
// â
Composed filter + reduce pipeline
const totalByUser = orders
.filter((order) => order.userId === currentUserId)
.reduce((sum, order) => sum + order.total, 0);
ESLint flat config exampleâ
import immutable from "eslint-plugin-immutable-2";
export default [
{
files: ["**/*.{js,mjs,cjs,ts,mts,cts,tsx}"],
plugins: { immutable },
rules: {
"immutable/no-loop-statement": "error",
},
},
];
When not to use itâ
Loops can be the clearest option for early exits, streaming parsers, and certain performance-sensitive hotspots. If your team intentionally uses for loops in those places, scope-disable this rule there and keep it active elsewhere.
Rule catalog ID: R906