[英]Can someone please explain how all the formatting tools works in VS Code?
背景
我刚开始学习 react.js,发现很多人都在使用 prettier 和 eslint 来格式化他们的代码。 但是在我根据在线指南设置我自己的之后,有线的事情发生了。 当我保存文件时,它可以正确格式化代码,但当我手动触发格式功能(Shift+option+F)时则不能。 它会将文件格式化为 eslint 会给我错误的有线方式。
这是我正在使用的 vscode 设置:
"editor.formatOnSave": true,
"[javascript]": {
"editor.formatOnSave": false
},
"eslint.autoFixOnSave": true,
"eslint.alwaysShowStatus": true,
"prettier.disableLanguages": [
"js"
],
"editor.detectIndentation": true,
"editor.tabSize": 2,
我还有一个 .eslintrc 文件
{
"extends": ["react-app", "plugin:prettier/recommended"],
}
和一个 .prettierrc 文件
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"jsxBracketSameLine": true
}
我在这里假设的是 vscode 键盘快捷键(Shift+option+F)与 autoFixOnSave 使用的配置不同(甚至不是相同的工具)。 但我也不明白这些工具是如何工作和集成在一起的,哪一个优先于哪一个。 有人可以帮忙吗?
为什么要禁用js
以获得更漂亮的效果?
您知道 Prettier 可以与 ESLint 完美集成吗?
看看这篇文章: Prettier:与 ESLint 集成
在您的用户/工作区设置中,只需添加:
"files.autoSave": "off",
"editor.formatOnSave": true,
"eslint.autoFixOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"eslint.options": {
"extensions": [".js", ".jsx"]
},
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"],
此外,建议在您的根文件夹中有一个.editorconfig
:
# http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
max_line_length = off
trim_trailing_whitespace = false
最后,在您的.eslintrc
文件中,添加:
"extends": ["react-app", "plugin:prettier/recommended", "prettier/react"],
查看eslint-plugin-react以验证反应。
我不是在看 VS 代码的 mac 版本,但我认为热键应该是Shift + Option + F
编辑:我通常在 vscode 中禁用默认的 javascript 格式化程序,因为它可能与我的 eslint 规则冲突,这使得 eslint 无法正确格式化我的代码。
ESLint 有它自己的 Fix 命令,它在我的设置中没有热键,但我有eslint.autoFixOnSave: true
。
Prettier 不挂钩内部 vscode 格式命令。 它也有自己的命令设置。 对于大多数可用的漂亮扩展,运行漂亮格式的默认热键是CMD + Shift + P -> Format Document
但如果editor.formatOnSave
为true
则会在保存时触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.