Skip to content

Commit

Permalink
feat: add check to for-direction rule when the condition is in revers…
Browse files Browse the repository at this point in the history
…e order
  • Loading branch information
ild0tt0re committed Nov 13, 2023
1 parent eb2279e commit 9617521
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 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
24 changes: 20 additions & 4 deletions lib/rules/for-direction.js
Expand Up @@ -101,20 +101,36 @@ module.exports = {
}
return 0;
}

/**
* get the counter position in test
* @param {ASTNode} node the node to check
* @returns {"left" | "right"} the position of the counter respect to BinaryExpression
*/
function getCounterPosition(node) {
if (node.update.type === "UpdateExpression" || node.update.type === "AssignmentExpression") {
if (node.update.argument && node.update.argument.type === "Identifier") {
return node.test.left.name === node.update.argument.name ? "left" : "right";
}
}
return "left";
}

return {
ForStatement(node) {

if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
const counter = node.test.left.name;
if (node.test && node.test.type === "BinaryExpression" && (node.test.left.type === "Identifier" || node.test.right.type === "Identifier") && node.update) {
const counterPosition = getCounterPosition(node);
const counter = node.test[counterPosition].name;
const operator = node.test.operator;
const update = node.update;

let wrongDirection;

if (operator === "<" || operator === "<=") {
wrongDirection = -1;
wrongDirection = counterPosition === "left" ? -1 : 1;
} else if (operator === ">" || operator === ">=") {
wrongDirection = 1;
wrongDirection = counterPosition === "left" ? 1 : -1;
} else {
return;
}
Expand Down
12 changes: 12 additions & 0 deletions 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 Down Expand Up @@ -82,6 +88,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 Down

0 comments on commit 9617521

Please sign in to comment.