当前位置: 移动技术网 > IT编程>开发语言>其他编程 > vscode代码格式化和eslint的使用

vscode代码格式化和eslint的使用

2020年05月12日  | 移动技术网IT编程  | 我要评论

简介

今天看着写的代码越来越多后,发现自己读起都有点吃力了,哈哈,自己看着眼睛痛,就准备整顿一下,毕竟这个项目还要维护很久的,找解决方案和测试解决方案就用了一个半小时,严重开始怀疑自己的智商了。下面的目标让代码看起来很公正

代码编辑器

vscode
version:版本 1.35.1 (1.35.1) 2019-06-12t14:19:05.197z更新的

vscode代码格式化

因为目前公司就我一个后端,项目也不大,所以就采用这种方案,简单快捷粗暴。

一.点击code->preferences->settings 点击右上角{}
二.用户自定义设置(/user/settings.json)
添加代码

"editor.formatontype": true,
"editor.formatonsave": true

eslint配置

eslint不仅有代码规范而且还有一部分语法检查的功能,ex:命令规范(驼峰) a==b警告提示a===b...
eslint可以有效的规范代码,以后还是会采用,培养自己的规范的编码习惯

1.vscode安装eslint

这里以配置eslint-config-aribnb的例子
vscode在extensions中安装eslint

2.npm安装

npm install -g eslint

3.创建.eslintrc文件

softwaredemacbook-pro:koa-pro software$ "eslint --init"
? how would you like to configure eslint? "use a popular style guide"
? which style guide do you want to follow? "airbnb" (https://github.com/airbnb/javascript)
? do you use react? "no"
? what format do you want your config file to be in? "json"
checking peerdependencies of eslint-config-airbnb-base@latest
the config that you have selected requires the following dependencies:

eslint-config-airbnb-base@latest eslint@^4.19.1 || ^5.3.0 eslint-plugin-import@^2.14.0
? would you like to install them now with npm? "yes"
installing eslint-config-airbnb-base@latest, eslint@^4.19.1 || ^5.3.0, eslint-plugin-import@^2.14.0
npm warn koa-pro@1.0.0 no repository field.

+ eslint@5.16.0
+ eslint-plugin-import@2.17.3
+ eslint-config-airbnb-base@13.1.0
updated 3 packages and audited 7469 packages in 23.559s
found 370 vulnerabilities (1 low, 367 moderate, 2 high)
 run `npm audit fix` to fix them, or `npm audit` for details
successfully created .eslintrc.json file in /users/software/workspace/me/huafu/koa-pro

项目目录下将会生成一个eslintrc.json的文件

{
 "extends": "airbnb-base"
}
添加自己想要的设置,我这里node环境
{"env": {
 "node": true,
 "es6": true
 },
 "parseroptions": {#解决import export eslint报错
 "ecmafeatures": {
  "legacydecorators": true
 }
 },
 "extends": "airbnb-base"
}

4.关联eslint与vscode

1.code->preferences->settings 进入user的seetings
2.添加以下代码

"eslint.autofixonsave": true,//保存自动修复eslint错误
 "eslint.validate": [
 "javascript",
 "javascriptreact",
 {
  "language": "vue",
  "autofix": true
 }
 ],
 "eslint.options": {//指定eslint配置文件位置i
 "configfile": ".eslintrc.json" //指定项目根目录中的eslint配置文件
 }

这样vscode和eslint关联配置完成了,不出意外会报一大堆错。good lucky

总结:

主要是卡在eslint.options上,没看vscode的extensions的eslint的readme,而去相信了百度,没有添加eslint.options,那么一直都无法生效.学的教训。

到此这篇关于vscode代码格式化和eslint的使用的文章就介绍到这了,更多相关vscode代码格式化内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网