sonarjs/function-inside-loop
eslint-plugin-sonarjs
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/function-inside-loop": "error",
},
},
];var funs = [];
for (var i = 0; i < 13; i++) {
funs[i] = function() { // Non-Compliant
return i;
};
}
console.log(funs[0]()); // 13 instead of 0
console.log(funs[1]()); // 13 instead of 1
console.log(funs[2]()); // 13 instead of 2
console.log(funs[3]()); // 13 instead of 3
...The documentation shows no correct example for this rule.
Examples from the ESLint documentation.
sonarjs/function-inside-loop
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/function-inside-loop": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
A "for" loop update clause should move the counter in the right direction
Functions should always return the same type
Future reserved words should not be used as identifiers
Generators should explicitly "yield" a value