`do` expression conflicting with `do ... while` grammar?

# Bradley Meck (9 years ago)

how will people distinguish do ... while from do expressions? I tried to find the exact grammar but couldn't after a bit of searching.

For example in current JS,

do {
  console.log(1);
}
while (true);

would cause infinite number of console.logs. But if the do is seen as an expression statement it would only log once.

Any pointer to where the grammar is discussed would be quite helpful.

# Isiah Meadows (9 years ago)

I'm interested in where the (stage 1?) proposal is as well.

# Caitlin Potter (9 years ago)

current impl in v8 is to only parse do-expressions in expression contexts (as a PrimaryExpr, iirc). So it never gets mixed up with do-while loops, which is fine (imo).

I recall seeing discussion about this in tc39 notes at some point, afaik the conclusion was that do-expressions dont work in Statement contexts, because theres no use case

# Bradley Meck (9 years ago)

Does that mean the following would parse as a do expression?

function foo () {
  return
  do {
    1;
  }
  while (true);
}
# Caitlin Potter (9 years ago)

id have to check the ASI rules, but I expect that would parse as a do-while loop and result in a SyntaxError. Interesting test case, though. should add to v8's test suite.

# Claude Pache (9 years ago)

Le 22 févr. 2016 à 17:47, Bradley Meck <bradley.meck at gmail.com> a écrit :

Does that mean the following would parse as a do expression?

function foo () {
  return
  do {
    1;
  }
  while (true);
}

No, because return cannot be followed be a LineTerminator, so that ASI will introduce a semicolon after it.

This is the same issue as:

function foo() {
    return   // an implied semicolon is inserted here
    { bar: 1 }; // interpreted as a block, not as an object literal
}
# Caitlin Potter (9 years ago)

oh, somehow I totally missed the 'while (true)' at the end of that snippet -- so I retract the SyntaxError thing. but yeah, would be a do-while loop