vs Code + Eslint + Prettier 代码格式化(vue开发)
一、什么是Eslint
通过查看eslint官网(官网传送门),我们就可以知道,eslint就是一个用来识别 ECMAScript/JavaScript 并且按照规则给出报告的代码检测工具,主要用来检测代码风格是不是符合指定的规则/规范,这样有利于团队开发的时候代码风格统一,.毕竟每个人的代码风格不一致,使用eslint代码校验工具就保证了代码风格的统一性。
二、什么Prettier
通过查看prettier官网(官网传送门),我们就知道,prettier是一个代码格式化工具,包括JavaScript (including experimental features)、JSX、Angular、Vue、Flow、TypeScript、CSS, Less, and SCSS、HTML、JSON、GraphQL、Markdown, including GFM and MDX、YAML各种语言,简而言之,这个工具能够使输出代码保持风格一致。
三、vs Code安装相关插件
在这里我默认你已经安装好了vs Code,如果还没有安装后者不知道怎么安装也没关系,可以查看安装vs Code的其他博客,我这里不在赘述。vs Code安装好之后,这里要介绍几个常用的插件,可以灵活使用,以方便使用提高效率为原则。
1、Vetur: vue语法高亮插件;
2、ESlint
语法错误检测工具;
3、HTML Snippets
回车或按下Tab键生成标签;
4、Auto Rename Tag
自动修改匹配的标签;
5、prettier
代码格式化工具;
四、vs Code格式化相关配置
vs Code格式化插件有很多,这里首推prettier,可以根据自己的需求进行配置,并且设置format on save,就会在代码格式化的时候自动保存
{
"vetur.format.defaultFormatter.html": "js-beautify-html",
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"wrap_line_length": 200,
"wrap_attributes": "auto",
"end_with_newline": false
}
},
//分号
"prettier.semi": false,
"prettier.singleQuote": true, //使用单引号替代双引号
"prettier.eslintIntegration": true,
//函数前加空格
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"vetur.format.defaultFormatter.js": "vscode-typescript",
"eslint.alwaysShowStatus": true,
"eslint.autoFixOnSave": true,
"eslint.workingDirectories": [
".eslintrc.js",
{
"mode": "auto"
}
],
"eslint.validate": [
"javascript",
"javascriptreact",
{ "language": "vue", "autoFix": true }
],
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
//默认JavaScript格式化程序(为了更美观)
"javascript.format.enable": true,
"prettier.arrowParens": "avoid",
}
五、eslint相关配置
eslint先关配置跟prettier结合起来,设置prettier的eslintIntegration属性为true,就可以根据eslint相关配置规则格式化代码,相关配置如下,更多配置规则请查看官网
// https://eslint.org/docs/user-guide/configuring module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint'
},
env: {
browser: true
// "jest":true
},
// required to lint *.vue files
plugins: ['vue'],
extends: ['plugin:vue/essential', 'standard'],
// add your custom rules here
rules: {
// allow async-await
camelcase: 'off',
'no-console': 'error',
'standard/no-callback-literal': 'off',
'generator-star-spacing': 'off',
'space-before-function-paren': [
'error',
{
anonymous: 'always',
named: 'never',
asyncArrow: 'never'
}
],
'func-call-spacing': ['error', 'never'],
'vue/no-mutating-props': 'nerve',
// allow debugger during development
// 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': 'error',
'template-curly-spacing': 'off',
quotes: ['error', 'single'],
// 强制不使用分号结尾
semi: ['error', 'never'],
indent: [
'error',
2,
{
ignoredNodes: ['TemplateLiteral', 'ConditionalExpression'],
SwitchCase: 1
}
]
// end 解决eslint报错问题
}
}
在setting.json中设置一下配置,可在格式化的时候自动安装eslint规则修复不符合规范的代码。
"eslint.validate": [
"javascript",
"javascriptreact",
{ "language": "vue", "autoFix": true }
],
vs Code + Eslint + Prettier 代码格式化(vue开发)的更多相关文章
- Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南
Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南 因为平时都是使用 VSCode ESLint + Prettier 检测格式化不规范代码,但是随着接手的项目越来越多 ...
- vscode如何配置ts的lint,如何配置才能让eslint和prettier不冲突一键格式化代码(vue开发使用)
最近在使用ts,发觉tslint在vscode上使用很不方便,不如eslint一键格式化高效,就想着能不能配置下vscode让其像写js一样爽 这篇文章主要解决2个问题,第一个是如何让vscode使用 ...
- vs code 设置 保存自动格式化vue代码
配置 ESLint (需安装 Prettier - Code formatter 插件) command + shift + p 打开用户设置 // vscode默认启用了根据文件类型自动设置tab ...
- 在IntelliJ IDEA中配置Google Java Code Style及代码格式化快捷键
google-java-format plugin should intercept the “Reformat Code” action in IDEA (Ctrl+Alt+L) and apply ...
- eslint prettier vetur eslint
VScode (版本 1.47.3)安装 eslint prettier vetur 插件 .vue 文件使用 vetur 进行格式化 在文件 .prettierrc 里写 属于你的 pettier ...
- ESLint 检查代码质量
利用 ESLint 检查代码质量 其实很早的时候就想尝试 ESLint 了,但是很多次都是玩了一下就觉得这东西巨复杂,一执行检查就是满屏的error,简直是不堪入目,遂放弃.直到某天终于下定决心深入看 ...
- 利用 ESLint 检查代码质量
原文发表于作者的个人博客:http://morning.work/page/maintainable-nodejs/getting-started-with-eslint.html 其实很早的时候就想 ...
- 在vscode中使用eslint+prettier格式化vue项目代码 (转载)
ESlint:javascript代码检测工具,可以配置每次保存时格式化js,但每次保存只格式化一点点,你得连续按住Ctrl+S好几次,才格式化好,自行体会~~ vetur:可以格式化html.标准c ...
- VS Code 自动修改和保存 代码风格 == eslint+prettier
最近因为用到VS Code,需要统一所有人的代码风格(前端语言js/html/css等,或者后端语言 go/python等也可以这么用). 所以参考了一些网络资料,记录下设置步骤,以便后续查阅. St ...
随机推荐
- 蓝牙鼠标 & 罗技 M337
蓝牙鼠标 & 罗技 M337 蓝牙鼠标,有哪些不需要适配器的 https://www.logitech.com.cn/zh-cn/product/bluetooth-mouse-m337 ht ...
- sketch 导出 svg
sketch 导出 svg refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- XML & XPath & XQuery
XML & XPath & XQuery full XPath demo https://www.w3.org/TR/xpath-full-text-30/ https://www.w ...
- zsh all in one
zsh all in one zsh https://ohmyz.sh/ # install $ sh -c "$(curl -fsSL https://raw.github.com/ohm ...
- Flutter: OrientationBuilder 根据方向更新UI
文档 api class _HomePageState extends State<HomePage> { @override Widget build(BuildContext cont ...
- 类关系与uml图示表示
1. 关联(Association).聚合(Aggregation).组合(Composition)区别 association: 两者之间存在某种关联即可,很弱的关系,如student and co ...
- 读懂框架设计的灵魂—Java反射机制
尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 CS-Wiki(Gitee 官 ...
- elasticsearch如何设计集群
本文为博客园作者所写: 一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 在写本文时就在想,如果让你负责一个elasticsearch集群,从零开始,你会从哪些方面 ...
- 永恒之蓝(MS17-010)检测与利用
目录 利用Nmap检测 MSF反弹SHELL 注意 乱码 参考 利用Nmap检测 命令: nmap -p445 --script smb-vuln-ms17-010 [IP] # 如果运行报错,可以加 ...
- POJ-1502(基本dijikstra算法)
MPI Maelstrom POJ-1502 这题是求最短路,但是因为一开始看错题目,导致我去使用prime算法求最小生成树 题意是指一台机器发出信息后,还可以向其他的机器发送信息,所以不能使用pri ...