jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.45k stars 1.98k forks source link

Bug?: Postfix conditional difference between try/iteration statements #5431

Closed STRd6 closed 1 year ago

STRd6 commented 1 year ago

Input Code

loop x() if y
try x() if y

Expected Behavior

if should be outside of both statements.

if (y) {
  while (true) {
    x();
  }
}

if (y) {
  try {
    x();
  } catch (error) {}
}

Current Behavior

if is inside try but outside of while.

if (y) {
  while (true) {
    x();
  }
}

try {
  if (y) {
    x();
  }
} catch (error) {}

Possible Solution

Maybe add documentation or adjust precedence to be consistent.

Context

Not sure if this is a bug but I just want to understand how postfix conditionals work with other statements accurately.

https://coffeescript.org/#try:loop%20x()%20if%20y%0Atry%20x()%20if%20y

GeoffreyBooth commented 1 year ago

I don’t think we can change this without it being a breaking change. This is one of those confusing things when everything is mushed onto one line; using indentation would eliminate the ambiguity and reliance on precedence. I’m not sure that documenting the precedence of inline try versus loop is worth it, since such ambiguous code shouldn’t be encouraged in the first place.

STRd6 commented 1 year ago

That's fair