Skip to content

Commit a7a883b

Browse files
authored
feat: for-direction rule add check for condition in reverse order (#17755)
feat: add check to for-direction rule when the condition is in reverse order
1 parent 1452dc9 commit a7a883b

3 files changed

Lines changed: 48 additions & 17 deletions

File tree

docs/src/rules/for-direction.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ for (var i = 10; i >= 0; i++) {
2424
for (var i = 0; i > 10; i++) {
2525
}
2626

27+
for (var i = 0; 10 > i; i--) {
28+
}
29+
2730
const n = -2;
2831
for (let i = 0; i < 10; i += n) {
2932
}
@@ -40,6 +43,9 @@ Examples of **correct** code for this rule:
4043
for (var i = 0; i < 10; i++) {
4144
}
4245

46+
for (var i = 0; 10 > i; i++) { // with counter "i" on the right
47+
}
48+
4349
for (let i = 10; i >= 0; i += this.step) { // direction unknown
4450
}
4551

lib/rules/for-direction.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,37 @@ module.exports = {
101101
}
102102
return 0;
103103
}
104+
104105
return {
105106
ForStatement(node) {
106107

107-
if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
108-
const counter = node.test.left.name;
109-
const operator = node.test.operator;
110-
const update = node.update;
108+
if (node.test && node.test.type === "BinaryExpression" && node.update) {
109+
for (const counterPosition of ["left", "right"]) {
110+
if (node.test[counterPosition].type !== "Identifier") {
111+
continue;
112+
}
111113

112-
let wrongDirection;
114+
const counter = node.test[counterPosition].name;
115+
const operator = node.test.operator;
116+
const update = node.update;
113117

114-
if (operator === "<" || operator === "<=") {
115-
wrongDirection = -1;
116-
} else if (operator === ">" || operator === ">=") {
117-
wrongDirection = 1;
118-
} else {
119-
return;
120-
}
118+
let wrongDirection;
119+
120+
if (operator === "<" || operator === "<=") {
121+
wrongDirection = counterPosition === "left" ? -1 : 1;
122+
} else if (operator === ">" || operator === ">=") {
123+
wrongDirection = counterPosition === "left" ? 1 : -1;
124+
} else {
125+
return;
126+
}
121127

122-
if (update.type === "UpdateExpression") {
123-
if (getUpdateDirection(update, counter) === wrongDirection) {
128+
if (update.type === "UpdateExpression") {
129+
if (getUpdateDirection(update, counter) === wrongDirection) {
130+
report(node);
131+
}
132+
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
124133
report(node);
125134
}
126-
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
127-
report(node);
128135
}
129136
}
130137
}

tests/lib/rules/for-direction.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ ruleTester.run("for-direction", rule, {
2828
"for(var i = 10; i > 0; i--){}",
2929
"for(var i = 10; i >= 0; i--){}",
3030

31+
// test if '++', '--' with counter 'i' on the right side of test condition
32+
"for(var i = 0; 10 > i; i++){}",
33+
"for(var i = 0; 10 >= i; i++){}",
34+
"for(var i = 10; 0 < i; i--){}",
35+
"for(var i = 10; 0 <= i; i--){}",
36+
3137
// test if '+=', '-=',
3238
"for(var i = 0; i < 10; i+=1){}",
3339
"for(var i = 0; i <= 10; i+=1){}",
@@ -44,6 +50,9 @@ ruleTester.run("for-direction", rule, {
4450
"for(var i = 0; i < MAX; i -= ~2);",
4551
"for(var i = 0, n = -1; i < MAX; i += -n);",
4652

53+
// test if '+=', '-=' with counter 'i' on the right side of test condition
54+
"for(var i = 0; 10 > i; i+=1){}",
55+
4756
// test if no update.
4857
"for(var i = 10; i > 0;){}",
4958
"for(var i = 10; i >= 0;){}",
@@ -82,6 +91,12 @@ ruleTester.run("for-direction", rule, {
8291
{ code: "for(var i = 10; i > 10; i++){}", errors: [incorrectDirection] },
8392
{ code: "for(var i = 10; i >= 0; i++){}", errors: [incorrectDirection] },
8493

94+
// test if '++', '--' with counter 'i' on the right side of test condition
95+
{ code: "for(var i = 0; 10 > i; i--){}", errors: [incorrectDirection] },
96+
{ code: "for(var i = 0; 10 >= i; i--){}", errors: [incorrectDirection] },
97+
{ code: "for(var i = 10; 10 < i; i++){}", errors: [incorrectDirection] },
98+
{ code: "for(var i = 10; 0 <= i; i++){}", errors: [incorrectDirection] },
99+
85100
// test if '+=', '-='
86101
{ code: "for(var i = 0; i < 10; i-=1){}", errors: [incorrectDirection] },
87102
{ code: "for(var i = 0; i <= 10; i-=1){}", errors: [incorrectDirection] },
@@ -96,6 +111,9 @@ ruleTester.run("for-direction", rule, {
96111
{ code: "for(var i = MIN; i <= MAX; i-=true){}", errors: [incorrectDirection] },
97112
{ code: "for(var i = 0; i < 10; i-=+5e-7){}", errors: [incorrectDirection] },
98113
{ code: "for(var i = 0; i < MAX; i += (2 - 3));", errors: [incorrectDirection] },
99-
{ code: "var n = -2; for(var i = 0; i < 10; i += n);", errors: [incorrectDirection] }
114+
{ code: "var n = -2; for(var i = 0; i < 10; i += n);", errors: [incorrectDirection] },
115+
116+
// test if '+=', '-=' with counter 'i' on the right side of test condition
117+
{ code: "for(var i = 0; 10 > i; i-=1){}", errors: [incorrectDirection] }
100118
]
101119
});

0 commit comments

Comments
 (0)