Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questionable position of block comments inside if-else block #5012

Open
maximbaz opened this issue Aug 23, 2018 · 2 comments
Open

Questionable position of block comments inside if-else block #5012

maximbaz opened this issue Aug 23, 2018 · 2 comments
Labels
area:comments Issues with how Prettier prints comments lang:javascript Issues affecting JS

Comments

@maximbaz
Copy link

Prettier 1.14.2
Playground link

Input:

if (one) {
  console.log("one");
} else /* if (two) */ {
  console.log("another");
}

Output:

if (one) {
  console.log("one");
} /* if (two) */ else {
  console.log("another");
}

Expected behavior:

if (one) {
  console.log("one");
} else /* if (two) */ {
  console.log("another");
}

I realize that this is a difficult problem to solve and I've seen that you already have quite a number of issues about comments and their positioning, but I didn't see this specific example shown. If it's a duplicate, feel free to close.

Basically I want to be able to temporarily transform else if into an else. But prettier is doing something unexpected, it rearranges the position of the comment, which makes it difficult to return back to else if later on.

@suchipi
Copy link
Member

suchipi commented Aug 23, 2018

Thanks for the report. As a workaround, you can change the conditional to true || two instead of using a comment here:

if (one) {
  console.log("one");
} else if (true || two) {
  console.log("another");
}

@suchipi suchipi added area:comments Issues with how Prettier prints comments lang:javascript Issues affecting JS labels Aug 23, 2018
@lydell
Copy link
Member

lydell commented Aug 24, 2018

I often find it easier to duplicate the line when doing such debugging experiments:

if (one) {
  console.log("one");
  // } else if (two) {
} else {
  console.log("another");
}

For me, duplicating lines and commenting out lines are quick editor shortcuts. And I find it easier to edit/delete stuff than adding /* and */. As a bonus, I still have the original line around for reference.

Prettier tends to handle lines consisting of only a // comment better than inline /**/ comments.

Not saying that things can't be improved in Prettier's comment handling (but keep in mind that comment handling is super hard), just sharing another approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:comments Issues with how Prettier prints comments lang:javascript Issues affecting JS
Projects
None yet
Development

No branches or pull requests

3 participants