1

I'm converting some es5 into es6 and using eslint and come across the following

while(1){
  // Do something
}

I'm getting a lint error

Unexpected constant condition no-constant-condition

But I'm not sure exactly what this does. It resides inside a for loop

for(i = 0; i< arr.length; i++) {
  while(1) {
    idx = _p.indexOf(arr[i], idx);
    if (idx == -1) break;
  }
}

But there was no documentation and unable to find online what exactly while(1) does and how to write an alternative to get rid of this lint error.

  • while(1) will continuously run, until you `break` out of it. It's a short hand for DO-WHILE. So it says, while the idx does not equal -1, always run this loop. – Dan Nov 03 '17 at 11:08
  • `while(1)` is just another dirty approach for continuity – B001ᛦ Nov 03 '17 at 11:09
  • Possible duplicate of [Why does ESLint trigger lint errors on while(true) using fibers?](https://stackoverflow.com/questions/37466508/why-does-eslint-trigger-lint-errors-on-whiletrue-using-fibers) –  Nov 03 '17 at 11:37

5 Answers5

2

You could rewrite the code with a do ... while loop with a check at the end, because that is what you do.

for (i = 0; i< arr.length; i++) {
    do {
        idx = _p.indexOf(arr[i], idx);
    } while (idx !== -1)
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • But isn't it a big effort to change the compiled code after every compilation? – Roman Nov 03 '17 at 12:37
  • the source code and your compiled code has to be equal and with your solution it would be unequal. Additionally with the next compilation the changes would be gone and you have to change it again.. bad idea – Roman Nov 03 '17 at 12:50
  • every time the _es6 file_ compiles the _es5 file_ will be overriden and you have to modify the _es5 file_ again. – Roman Nov 03 '17 at 13:01
2

A Loop

Instructions are repeated n times.

While Loop

while needs a condition (a boolean value) to handle a loop. If the condition is true it executes else it does nothing.

In this case the condition is 1 and gets cast to a true.

function getBooleanValue(value) {
  return !!value
}

console.log('1 gets cast to:', getBooleanValue(1))
console.log('0 gets cast to:', getBooleanValue(0))

Infinite Loop

A loop that never terminates.

Your loop is after the cast while(true). With other words do for ever.

Why this Rule

From the Docs:

A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior. For example, the following code looks as if it is not ready for production.

if (false) {
    doSomethingUnfinished();
}

if you want to disable it: /*eslint-disable no-constant-condition*/

Community
  • 1
  • 1
Roman
  • 4,922
  • 3
  • 22
  • 31
1

while(1) creates a loop that runs forever (never terminates) unless you explicitly stop it with the break keyword. The loop will also stop if an error occurs during it's execution. These constructs can be very dangerous (crash your pc/browser) and that is why jslint warns about them.

An other way of writing this would be while(true) or using for(;;) (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for).

MEE
  • 503
  • 1
  • 11
  • 21
1

Link to docs

This rule disallows constant expressions in the test condition of: if, for, while, or do...while statement ?: ternary expression

Examples of incorrect code for this rule:

if (false) {
    doSomethingUnfinished();
}

if (void x) {
    doSomethingUnfinished();
}

for (;-2;) {
    doSomethingForever();
}

while (typeof x) {
    doSomethingForever();
}

do {
    doSomethingForever();
} while (x = -1);

var result = 0 ? a : b;

Do: while(true)

Or you can just disable that: /*eslint-disable no-constant-condition*/

  • I found this stack overflow post that talks about the ```while(true)```, it says that these rules about infinite loops are from before generators were a thing and are not even aware of fibers.You should just disable it. [Link to post](https://stackoverflow.com/questions/37466508/why-does-eslint-trigger-lint-errors-on-whiletrue-using-fibers) –  Nov 03 '17 at 11:36
0

That's simply an infinite loop. It cycles until 1 isn't true (never). As you can see, inside this loop there's a condition and a break, so the infinite loop will end when the condition is true, hence the break.

while(1) { //Cycle forever...
    idx = ...;
    if (idx == -1) break; //...until idx == -1!
}
Luca De Nardi
  • 2,280
  • 16
  • 35