question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-cond-assign and while loops

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9

github_iconTop GitHub Comments

4reactions
ljharbcommented, Jun 6, 2017

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 do a == b or a === b - a = b in expression position looks like a bug.

2reactions
danielkczcommented, Jun 6, 2017

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.

function matchAll(regex, text) {
  const matches = []
  let match = null
  // eslint-disable-next-line no-cond-assign
  while ((match = regex.exec(text)) !== null) {
    matches.push(match)
  }
  return matches
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found