prettier icon indicating copy to clipboard operation
prettier copied to clipboard

TryCatch block moves the comment an annoying location

Open adamrybak opened this issue 3 years ago • 3 comments

Prettier 2.7.1 Playground link

Input:

try {
  console.log("try");
} // this is a comment
catch (error) {
  console.error(error);
}

Output:

try {
  console.log("try");
} catch (error) {
  // this is a comment
  console.error(error);
}

Expected behavior:

try {
  console.log("try");
} // this is a comment
catch (error) {
  console.error(error);
}

adamrybak avatar Aug 03 '22 23:08 adamrybak

I guess it should be

try {
  console.log("try");
} catch (error) { // this is a comment
  console.error(error);
}

dimaMachina avatar Aug 04 '22 22:08 dimaMachina

I guess it should be

try {
  console.log("try");
} catch (error) { // this is a comment
  console.error(error);
}

No, because the intention is to separate the try catch blocks to make the code more readable and make it more consistent with how if else blocks currently behave.

Example:

if (condition) {
  return "yes";
} // comment
else {
  return "no";
}

adamrybak avatar Aug 04 '22 23:08 adamrybak

Currently the two types of blocks behave differently:

Input:

try {
  console.log("try");
}
//
catch (error) {
  console.log(error);
} 
//
finally {
  console.log("fin");
}

if (condition) {
  return "yes";
}
//
else {
  return "no";
}

Output:

try {
  console.log("try");
} catch (error) {
  //
  console.log(error);
} finally {
  //
  console.log("fin");
}

if (condition) {
  return "yes";
}
//
else {
  return "no";
}

adamrybak avatar Aug 05 '22 01:08 adamrybak