istanbuljs/istanbuljs

Limitations related to while(true) loops

cyberixae opened this issue · 0 comments

I keep running into code coverage calculation issues related to while(true) { ... } loops. Instabul seems to think that the code branches at such spots. It really does not since there are no circumstances where the literal true would not be true.

The main usecase where I use while(true) loops is when I work with generators that are able to generate an arbitrary amount of output. In TypeScript such generators would have return type never. Because of this the type checker is able to verify that the function is not accidentally terminated and will continue yielding values indefinitely.

function* greeter( ): Generator<string, never, undefined> {
  while(true) {  // <- replacing true with pretty much anything else would be a type error
    yield 'hello'
  }
}

In JavaScript I could replace the literal true with a variable that would let me trick Istanbul into reaching the unreachable branch during one of the test runs. However, doing that would create a type error. I would then need to silence the type error which I am hesitant to do since I find the type error quite useful while I am working with the code.

It would be nice if Istanbul would consider the while(true) loop to be a completely separate case from any other while loops and would not consider while(true) loops as places where the code branches into two separate paths. I am wondering if there is some fundamental underlying reason that prevents addressing this issue. However, I wasn't able to find a mention of this problem in any of the open or closed issues.

One year ago, I asked on StackOverflow about this issue in hopes I would find some ignore command that would let me work around the issue. However, the question has not received any answers. Today I asked about TypeScript specific workarounds in another thread. This time I got some responses. However, all of them suffer from the limitation described above.

Yesterday, I took a quick look at Istanbul source code. I thought perhaps I could create a new test case for while(true) under istanbuljs/packages/istanbul-lib-instrument/test/specs/while.yaml to verify the issue. However, I didn't exactly understand the format of the expected result.