Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: for-direction rule add check for condition in reverse order #17755

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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] }
]
});