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-constant-condition should allow for while(true) idiom

See original GitHub issue

What version of ESLint are you using?


What configuration and parser (Espree, Babel-ESLint, etc.) are you using?

env: es6 + node parser: Espree config: “no-constant-condition”: “error”

What did you do? Please include the actual source code causing the issue.

while (true)
    if (doSomething())

What did you expect to happen?

I expected no error at the while, because its dependent statement has a break, and in this case it’s equivalent to for(;;). Maybe I’m the only one who uses this idiom.

What actually happened? Please include the actual, raw output from ESLint.

line 15, col 1, Error - Unexpected constant condition. (no-constant-condition)

line 15, col 1 is the “w” in while.

What do you suggest?

There are three approaches to fixing this:

  1. Ignore it and hope I go away. 😉
  2. Always allow while (true) the way for (;;) is allowed.
  3. Add an “allow-while-true” option.

I’ll be happy to implement it, but let me know which approach you want to take.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:23 (21 by maintainers)

github_iconTop GitHub Comments

dead-claudiacommented, Apr 17, 2016

I feel an extra option for no-constant-condition to allow explicit boolean literals would be nice. I personally find while (true) more readable and the intent more explicit than for (;;). Also, there’s the do { ... } while (false) idiom, for which there is no substitute other than labelled blocks, which IMHO are ugly.

for (;;) {
  if (condition()) return null

while (true) {
  if (condition()) return null

As for boolean literal conditions in if (true), etc., that can still be caught by no-unreachable-code.

So 👍 from me.

aparajitacommented, Apr 27, 2016

@kaicataldo Go for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does ESLint trigger lint errors on while(true) using fibers?
I have come JS code with some infinite loops (we're using node-fibers to allow things to happen) ...
Read more >
no-constant-condition - ESLint - Pluggable JavaScript Linter
This rule disallows constant expressions in the test condition of: if , for , while , or do...while statement ?: ternary expression.
Read more >
VBA While Loop - A Complete Guide - Excel Macro Mastery
A condition is a statement that evaluates to true or false. They are mostly used with Loops and If statements. When you create...
Read more >
Loops: while(), for() and do .. while() - Physics and Astronomy
The while() loop repeats as long as the condition is true (non-zero). If the condition is false the body of the loop never...
Read more >
Python Refactoring: "while True" Infinite Loops & The "input ... ▻ Write clean and efficient Python code with this bite-sized series of free & practical Python tips.
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 Post

No results found

github_iconTop Related Hashnode Post

No results found