Skip to content

Commit

Permalink
feat: for-direction rule add check for condition in reverse order (#1…
Browse files Browse the repository at this point in the history
…7755)

feat: add check to for-direction rule when the condition is in reverse order
  • Loading branch information
ild0tt0re committed Nov 17, 2023
1 parent 1452dc9 commit a7a883b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
6 changes: 6 additions & 0 deletions docs/src/rules/for-direction.md
Expand Up @@ -24,6 +24,9 @@ for (var i = 10; i >= 0; i++) {
for (var i = 0; i > 10; i++) {
}

for (var i = 0; 10 > i; i--) {
}

const n = -2;
for (let i = 0; i < 10; i += n) {
}
Expand All @@ -40,6 +43,9 @@ Examples of **correct** code for this rule:
for (var i = 0; i < 10; i++) {
}

for (var i = 0; 10 > i; i++) { // with counter "i" on the right
}

for (let i = 10; i >= 0; i += this.step) { // direction unknown
}

Expand Down
39 changes: 23 additions & 16 deletions lib/rules/for-direction.js
Expand Up @@ -101,30 +101,37 @@ module.exports = {
}
return 0;
}

return {
ForStatement(node) {

if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
const counter = node.test.left.name;
const operator = node.test.operator;
const update = node.update;
if (node.test && node.test.type === "BinaryExpression" && node.update) {
for (const counterPosition of ["left", "right"]) {
if (node.test[counterPosition].type !== "Identifier") {
continue;
}

let wrongDirection;
const counter = node.test[counterPosition].name;
const operator = node.test.operator;
const update = node.update;

if (operator === "<" || operator === "<=") {
wrongDirection = -1;
} else if (operator === ">" || operator === ">=") {
wrongDirection = 1;
} else {
return;
}
let wrongDirection;

if (operator === "<" || operator === "<=") {
wrongDirection = counterPosition === "left" ? -1 : 1;
} else if (operator === ">" || operator === ">=") {
wrongDirection = counterPosition === "left" ? 1 : -1;
} else {
return;
}

if (update.type === "UpdateExpression") {
if (getUpdateDirection(update, counter) === wrongDirection) {
if (update.type === "UpdateExpression") {
if (getUpdateDirection(update, counter) === wrongDirection) {
report(node);
}
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
report(node);
}
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
report(node);
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion tests/lib/rules/for-direction.js
Expand Up @@ -28,6 +28,12 @@ ruleTester.run("for-direction", rule, {
"for(var i = 10; i > 0; i--){}",
"for(var i = 10; i >= 0; i--){}",

// test if '++', '--' with counter 'i' on the right side of test condition
"for(var i = 0; 10 > i; i++){}",
"for(var i = 0; 10 >= i; i++){}",
"for(var i = 10; 0 < i; i--){}",
"for(var i = 10; 0 <= i; i--){}",

// test if '+=', '-=',
"for(var i = 0; i < 10; i+=1){}",
"for(var i = 0; i <= 10; i+=1){}",
Expand All @@ -44,6 +50,9 @@ ruleTester.run("for-direction", rule, {
"for(var i = 0; i < MAX; i -= ~2);",
"for(var i = 0, n = -1; i < MAX; i += -n);",

// test if '+=', '-=' with counter 'i' on the right side of test condition
"for(var i = 0; 10 > i; i+=1){}",

// test if no update.
"for(var i = 10; i > 0;){}",
"for(var i = 10; i >= 0;){}",
Expand Down Expand Up @@ -82,6 +91,12 @@ ruleTester.run("for-direction", rule, {
{ code: "for(var i = 10; i > 10; i++){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; i >= 0; i++){}", errors: [incorrectDirection] },

// test if '++', '--' with counter 'i' on the right side of test condition
{ code: "for(var i = 0; 10 > i; i--){}", errors: [incorrectDirection] },
{ code: "for(var i = 0; 10 >= i; i--){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; 10 < i; i++){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; 0 <= i; i++){}", errors: [incorrectDirection] },

// test if '+=', '-='
{ code: "for(var i = 0; i < 10; i-=1){}", errors: [incorrectDirection] },
{ code: "for(var i = 0; i <= 10; i-=1){}", errors: [incorrectDirection] },
Expand All @@ -96,6 +111,9 @@ ruleTester.run("for-direction", rule, {
{ code: "for(var i = MIN; i <= MAX; i-=true){}", errors: [incorrectDirection] },
{ code: "for(var i = 0; i < 10; i-=+5e-7){}", errors: [incorrectDirection] },
{ code: "for(var i = 0; i < MAX; i += (2 - 3));", errors: [incorrectDirection] },
{ code: "var n = -2; for(var i = 0; i < 10; i += n);", errors: [incorrectDirection] }
{ code: "var n = -2; for(var i = 0; i < 10; i += n);", errors: [incorrectDirection] },

// test if '+=', '-=' with counter 'i' on the right side of test condition
{ code: "for(var i = 0; 10 > i; i-=1){}", errors: [incorrectDirection] }
]
});

0 comments on commit a7a883b

Please sign in to comment.