for-direction
eslint
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"for-direction": "error",
},
},
];Enforce for loop update clause moving the counter in the right direction
for (let i = 0; i < 10; i--) {
}
for (let i = 10; i >= 0; i++) {
}
for (let i = 0; i > 10; i++) {
// counter i is on the left with >, so i++ (increasing) is the wrong direction
}
for (let i = 0; i > 0; i++) {
// counter i is on the left with >, so i++ (increasing) is the wrong direction
}
for (let i = 0; 0 < i; i++) {
// counter i is on the right with <, so i++ (increasing) is the wrong direction
}
for (let i = 0; 10 > i; i--) {
}
const n = -2;
for (let i = 0; i < 10; i += n) {
}for (let i = 0; i < 10; i++) {
}
for (let i = 0; 10 > i; i++) { // with counter "i" on the right
}
for (let i = 10; i >= 0; i += this.step) { // direction unknown
}
for (let i = MIN; i <= MAX; i -= 0) { // not increasing or decreasing
}
for (let i = 0; i < 0; i++) {
// counter i is on the left with <, so i++ (increasing) is the correct direction
// (loop never executes, but direction is consistent)
}
for (let i = 0; 0 > i; i++) {
// counter i is on the right with >, so i++ (increasing) is the correct direction
// (loop never executes, but direction is consistent)
}Examples from the ESLint documentation.
for-direction
eslint
export default [
{
rules: {
"for-direction": "error",
},
},
];for-direction
eslint plugin · correctness
{
"rules": {
"for-direction": "error"
}
}correctness/useValidForDirection
correctness · since 1.0.0
{
"linter": {
"rules": {
"correctness": {
"useValidForDirection": "error"
}
}
}
}Disallow assigning to imported bindings
Disallow duplicate conditions in if-else-if chains
Disallow \8 and \9 escape sequences in string literals
Disallow use of optional chaining in contexts where the undefined value is not allowed