之前发过一篇ESlint检测自动加逗号的文章,VS Code设置了如果还是未生效,应该怎么办呢?
书写规范是非常重要的,出了统一,还要简洁。
1、VS Code识别编辑规范
在项目根目录新建.editorconfig
文件,加入设置:
# editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
2、ESlint规则
在项目根目录新建.eslintrc.json
文件,加入设置:
{
"env": {
"browser": true,
"es2021": true,
"jquery": true // $不报错
},
"extends": [
"standard"
],
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"rules": {
"space-before-function-paren": ["error", {
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}]
}
}
3、VS Code保存去掉自动加分号、逗号、双引号
在VS Code的是settings.json插入:
{
"prettier.useEditorConfig": true,
"prettier.jsxSingleQuote": true,
"prettier.singleQuote": true,
"prettier.trailingComma": "none",
"prettier.semi": false,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
// For ESLint
"source.fixAll.eslint": true,
// For TSLint
"source.fixAll.tslint": true,
// For Stylelint
"source.fixAll.stylelint": true
},
"[html]": {
"editor.defaultFormatter": "HookyQR.beautify"
},
"files.exclude": {
"node_modules": true
},
}