Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop prettier to keep adding new lines automatically?

I have my formatter installed, which is prettier, and I have enabled the format on save but I come across this problem which is sometimes it adds unnecessary new lines every time I save them, example in an array, like this:

const days = [
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday',
]
const months = [
  'Jan',
  'Feb',
  'Mar',
  'Apr',
  'May',
  'Jun',
  'Jul',
  'Aug',
  'Sep',
  'Oct',
  'Nov',
  'Dec',
]

Whereas before I saved, it just (and should be) look like this:

const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

I was wondering what configuration should I make to prevent this change while still keeping the 'format on save' enabled.

like image 389
codiecub15 Avatar asked Oct 27 '25 15:10

codiecub15


1 Answers

This is one of the biggest problems with prettier and why we have stopped using it on most projects.

There is apparently no way to turn it off because "prettier is opinionated". Unfortunately, some of the choices made by the creators hurt readability more than they help. (As you have found out.) It would be fine if you could customize it per project but certain formatting rules cannot be controlled.

Try using something like js-beautify which is less opinionated and does not attempt to format every aspect of your code.

like image 172
Justin Ohms Avatar answered Oct 30 '25 08:10

Justin Ohms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!