简体   繁体   English

如何停止更漂亮以自动添加新行?

[英]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.我想知道我应该进行什么配置来防止这种更改,同时仍然保持启用“保存格式”。

Try changing Print Width in Extension Settings https://i.stack.imgur.com/aHyif.png尝试在扩展设置https://i.stack.imgur.com/aHyif.png中更改打印宽度

This is one of the biggest problems with prettier and why we have stopped using it on most projects.这是 prettier 的最大问题之一,也是我们在大多数项目中停止使用它的原因。

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.尝试使用js-beautify 之类的东西,它不那么固执己见,并且不会尝试格式化代码的各个方面。

https://prettier.io/docs/en/ignore.html https://prettier.io/docs/en/ignore.html

在此处输入图像描述

Try this, seems to align with what you are looking for.试试这个,似乎与您正在寻找的内容一致。 Posted the link to show how //prettier-ignore can prevent this from happening on save.发布链接以显示//prettier-ignore如何防止在保存时发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM