简体   繁体   English

如何在“codesandbox.io”上配置 ESLint / Prettier 格式规则

[英]How to configure ESLint / Prettier formatting rules on "codesandbox.io"

On "codesandbox.io", how do I have to configure "Prettier" not to change my line breaks any longer (or maybe to deactive "Priettier" completely) ?在“codesandbox.io”上,我如何配置“Prettier”以不再更改我的换行符(或者可能完全停用“Priettier”)?

Also how can I deactivate a certain rule in ESLint there (in my case it's react-hooks/rules-of-hooks that I want to turn off) - a newly created ``eslintrc` file seems to be ignored in my ES201x project?另外,我如何在那里停用 ESLint 中的某个规则(在我的情况下,它是我想关闭的react-hooks/rules-of-hooks ) - 在我的 ES201x 项目中似乎忽略了一个新创建的“eslintrc”文件? Thx.谢谢。

You can easily configure the formatting behaviour of your Sandbox by adding the prettier config file in the following way:您可以通过以下方式添加prettier配置文件来轻松配置 Sandbox 的格式化行为:

  1. Create the file .prettierrc in the root folder of the Sandbox.在 Sandbox 的根文件夹中创建文件.prettierrc
  2. Using the JSON syntax add the formatting rules you want to the file.使用 JSON 语法将您想要的格式规则添加到文件中。

For example to change the the line wrapping which I guess annoys most of the people, set the printWidth value:例如,要更改我猜会惹恼大多数人的换行,请设置printWidth值:

{
  "trailingComma": "es5",
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true,
  "printWidth": 25
}

  1. Save the file and reload the Sandobox page .保存文件并重新加载 Sandobox 页面

  2. Next time you save any of the files the code will be formatted following the rules you had set in the .prettierrc .下次保存任何文件时,代码将按照您在.prettierrc设置的规则进行格式化

Other:其他:

  • It seems to be necessary to reload the Sandbox page for the settings to take place.似乎有必要重新加载沙箱页面才能进行设置。 After re-opening it, the file .prettierrc will be shown as the UI and not file.重新打开后,文件.prettierrc将显示为 UI 而不是文件。

  • To add new formatting rules, open the file .prettierrc showing as the UI and click on Open file in editor and add the rules you need.要添加新的格式规则,请打开显示为 UI 的文件.prettierrc并单击Open file in editor并添加您需要的规则。

  • Here is the list of Prettier config options you can set in .prettierrc .这是您可以在.prettierrc设置的.prettierrc配置选项列表

  • To enable/disable Prettier formatting do following: Cmd + Shift + P --> Select Preferences: Open Settings (UI) --> Search for Editor: Format on Save --> Disable/Enable the option .要启用/禁用 Prettier 格式,请执行以下操作: Cmd + Shift + P --> 选择Preferences: Open Settings (UI) --> 搜索Editor: Format on Save --> 禁用/启用该选项。

Enjoy!享受!

在此处输入图片说明

I couldn't find a way to prevent prettier from removing line-breaks so I just turned off the on-save setting.我找不到防止 prettier 删除换行符的方法,所以我只是关闭了保存设置。 It doesn't come up too often for me, so it's easy enough to pretty up code in an editor.对我来说它不会经常出现,所以在编辑器中修饰代码很容易。

I'm hunting for a way to override the eslint rules too我也在寻找一种方法来覆盖 eslint 规则

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

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