How to prevent Prettier from moving comments to new line

Viewed 1503

I have a typescript project formatted with prettier. My issue is when I save code like this:

const thing = await call({ // comment
  a: 1
});

Prettier moves the comment to a new line which is not what I want.

const thing = await call({
  // comment
  a: 1
});

How do I prevent this from happening while retaining all other format settings?

EDIT: I do not want to use another formatter or // prettier-ignore. I need this done from the config file .prettierrc.json

2 Answers

In my experience, you can specify certain areas of your code for prettier to ignore in the .prettierignore which uses the same syntax as a .gitignore. You can also ignore specific comments as this section of their documentation.

I'm pretty sure there is no way to configure this in .prettierrc, as prettier is opinionated. Though this kind of moving of comments to new line only happens when it appears beside opening brackets. If it really annoys you, you could change to a different linter, such as eslint, which to my knowledge does not do the above.

Related