jashkenas/coffeescript

Bug?: Postfix conditional difference between try/iteration statements

STRd6 opened this issue · 2 comments

STRd6 commented

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.

  • CoffeeScript version: 2.7.3

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

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

That's fair