no-loop-func
eslint
No autofix
eslint.config.js
export default [
{
rules: {
"no-loop-func": "error",
},
},
];Disallow function declarations that contain unsafe references inside loop statements
var i = 0;
while(i < 5) {
var a = function() { return i; };
a();
i++;
}
var i = 0;
do {
function a() { return i; };
a();
i++
} while (i < 5);
let foo = 0;
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
foo += 1;
}
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
}
foo = 100;
var arr = [];
for (var i = 0; i < 5; i++) {
// …type MyType = 1;
let someArray: MyType[] = [];
for (let i = 0; i < 10; i += 1) {
someArray = someArray.filter((item: MyType) => !!item);
}Examples from the ESLint documentation.
no-loop-func
eslint
export default [
{
rules: {
"no-loop-func": "error",
},
},
];no-loop-func
eslint plugin · pedantic
{
"rules": {
"no-loop-func": "error"
}
}suspicious/noLoopFunc
suspicious · since 2.4.13
{
"linter": {
"rules": {
"suspicious": {
"noLoopFunc": "error"
}
}
}
}Disallow function declarations that contain unsafe references inside loop statements
Disallow unnecessary concatenation of literals or template literals
Enforce consistent brace style for all control statements
Disallow certain properties on certain objects