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
Fix: no-plusplus allow comma operands in for afterthought (fixes #13005) #13024
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,15 @@ ruleTester.run("no-plusplus", rule, { | |
|
||
// With "allowForLoopAfterthoughts" allowed | ||
{ code: "var foo = 0; foo=+1;", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (i = 0; i < l; i++) { console.log(i); }", options: [{ allowForLoopAfterthoughts: true }] } | ||
{ code: "for (i = 0; i < l; i++) { console.log(i); }", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (var i = 0, j = i + 1; j < example.length; i++, j++) {}", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; i--, foo());", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; foo(), --i);", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; foo(), ++i, bar);", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; i++, (++j, k--));", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; foo(), (bar(), i++), baz());", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; (--i, j += 2), bar = j + 1);", options: [{ allowForLoopAfterthoughts: true }] }, | ||
{ code: "for (;; a, (i--, (b, ++j, c)), d);", options: [{ allowForLoopAfterthoughts: true }] } | ||
], | ||
|
||
invalid: [ | ||
|
@@ -58,6 +66,16 @@ ruleTester.run("no-plusplus", rule, { | |
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (i = 0; i < l; foo, i++) { console.log(i); }", | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "++" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
|
||
// With "allowForLoopAfterthoughts" allowed | ||
{ | ||
|
@@ -81,6 +99,72 @@ ruleTester.run("no-plusplus", rule, { | |
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (i++;;);", | ||
options: [{ allowForLoopAfterthoughts: true }], | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "++" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (;--i;);", | ||
options: [{ allowForLoopAfterthoughts: true }], | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "--" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (;;) ++i;", | ||
options: [{ allowForLoopAfterthoughts: true }], | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "++" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (;; i = j++);", | ||
options: [{ allowForLoopAfterthoughts: true }], | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "++" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (;; i++, f(--j));", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking.Just questions for understanding the proper behavior of this option. It seems the option so the case like And so, the below cases(nested sequence expression) are intended in the same manner? for (;; (++a, (++b, ++c) ) {} // it's warned in the PR version. no-plusplus for `b` and `c` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't think of that. It's done now, thanks! |
||
options: [{ allowForLoopAfterthoughts: true }], | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "--" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
}, | ||
{ | ||
code: "for (;; foo + (i++, bar));", | ||
options: [{ allowForLoopAfterthoughts: true }], | ||
errors: [{ | ||
messageId: "unexpectedUnaryOp", | ||
data: { | ||
operator: "++" | ||
}, | ||
type: "UpdateExpression" | ||
}] | ||
} | ||
] | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @yeonjuan mentioned, it appears that this check only looks up one level, which could mean some patterns will still be flagged as incorrect.
It may be an unlikely case, but for the sake of completeness, it probably makes sense to continue looking up the AST if the parent is a
SequenceExpression
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's done now! It makes sense to me, too.