no-cond-assign and while loops
See original GitHub issueI tried looking for some explanation but failed. What is an actual reasoning behind changing the default of this rule from except-parens
to always
?
The most typical example is looping over multiple matches of a single regexp.
var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
Is there a recommendation how to write this differently without breaking that rule setting? I tried do...while
but there that’s either weird while(true)
or a duplicate condition which feels rather weird.
do {
var myArray = myRe.exec(str)
if (myArray === null) break;
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
} while(true) // or while(myArray !== null)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:9
Top Results From Across the Web
While Loops | Introduction to Computer Science - CodeHS
While loops are a way to repeat a block of code. The basic syntax of a while loop is shown below. while(condition){ //...
Read more >C while and do...while Loop - Programiz
Loops are used in programming to execute a block of code repeatedly until a specified condition is met. In this tutorial, you will...
Read more >for and while loops in Python - LogRocket Blog
for and while loops are essential to Python. Learn their syntax, how to loop with numbers and lists, and important loop control statements....
Read more >Python While Loops - W3Schools
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set...
Read more >Programming - While Loop - Utah School of Computing
The while loop is used to repeat a section of code an unknown number of times until a specific condition is met. For...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In this specific situation - where there’s a gap in the language, and an active proposal to fix it (one I’m personally championing) - I’d recommend writing your own
matchAll
abstraction that overrides whatever eslint rules you need to, so that your real code doesn’t have to.In general, conflating statements (assignment) with expressions (a conditional evaluation) is a really bad idea for code clarity. Also, the return value of
a = b
is not intuitive, and it’s really difficult to determine if you meant to doa == b
ora === b
-a = b
in expression position looks like a bug.Ok thank you for the explanation. I’ll probably just go with this for now which is much more readable and later I can replace it with real
matchAll
.