Open brettz9 opened 4 years ago
I like it, but the rule needs to be off by default for now as .matchAll
requires Node.js 12.
This is now accepted. PR welcome :)
Can anyone please provide some test cases for the same ?
Sorry for the delayed reply. Re: test cases, I can mostly just think of the failing case in the OP (and its reverse while (null !== (match = regexp.exec(str))) {
), as well as this being left as is (passing):
let match;
while ((match = regexp.exec(str)) !== null) {
// ...
}
return match; // If `match` is used outside of the block, do not report/fix
Not offering a PR here, but just thought such a rule could possibly fit your project...
Namely, preferring
String.prototype.matchAll
towhile
loopexec
assignments.As per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll#Examples , one can replace this:
..with the likes of:
I find it quite a bit more elegant and semantic, and it allows avoiding defining a variable in the parent block.
If not, no worries, just thought I'd see if it might pique the interest of others.