8000 feat: add check to for-direction rule when the condition is in revers… · eslint/eslint@faeb3ee · GitHub
[go: up one dir, main page]

Skip to content

Commit faeb3ee

Browse files
feat: add check to for-direction rule when the condition is in reverse order
1 parent eb2279e commit faeb3ee

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,36 @@ module.exports = {
101101
}
102102
return 0;
103103
}
104+
105+
/**
106+
* get the counter position in test
107+
* @param {ASTNode} node the node to check
108+
* @returns {"left" | "right"} the position of the counter respect to BinaryExpression
109+
*/
110+
function getCounterPosition(node) {
111+
if (node.update.type === "UpdateExpression" || node.update.type === "AssignmentExpression") {
112+
if (node.update.argument && node.update.argument.type === "Identifier") {
113+
return node.test.left.name === node.update.argument.name ? "left" : "right";
114+
}
115+
}
116+
return "left";
117+
}
118+
104119
return {
105120
ForStatement(node) {
106121

107-
if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
108-
const counter = node.test.left.name;
122+
if (node.test && node.test.type === "BinaryExpression" && (node.test.left.type === "Identifier" || node.test.right.type === "Identifier") && node.update) {
123+
const counterPosition = getCounterPosition(node);
124+
const counter = node.test[counterPosition].name;
109125
const operator = node.test.operator;
110126
const update = node.update;
111127

112128
let wrongDirection;
113129

114130
if (operator === "<" || operator === "<=") {
115-
wrongDirection = -1;
131+
wrongDirection = counterPosition === "left" ? -1 : 1;
116132
} else if (operator === ">" || operator === ">=") {
117-
wrongDirection = 1;
133+
wrongDirection = counterPosition === "left" ? 1 : -1;
118134
} else {
119135
return;
120136
}

tests/lib/rules/for-direction.js

Lines changed: 12 additions & 0 deletions
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){}",
@@ -82,6 +88,12 @@ ruleTester.run("for-direction", rule, {
8288
{ code: "for(var i = 10; i > 10; i++){}", errors: [incorrectDirection] },
8389
{ code: "for(var i = 10; i >= 0; i++){}", errors: [incorrectDirection] },
8490

91+
// test if '++', '--' with counter 'i' on the right side of test condition
92+
{ code: "for(var i = 0; 10 > i; i--){}", errors: [incorrectDirection] },
93+
{ code: "for(var i = 0; 10 >= i; i--){}", errors: [incorrectDirection] },
94+
{ code: "for(var i = 10; 10 < i; i++){}", errors: [incorrectDirection] },
95+
{ code: "for(var i = 10; 0 <= i; i++){}", errors: [incorrectDirection] },
96+
8597
// test if '+=', '-='
8698
{ code: "for(var i = 0; i < 10; i-=1){}", errors: [incorrectDirection] },
8799
{ code: "for(var i = 0; i <= 10; i-=1){}", errors: [incorrectDirection] },

0 commit comments

Comments
 (0)
0