简介

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

代码编辑器

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可以有效的规范代码,以后还是会采用,培养自己的规范的编码习惯
https://cn.eslint.org/

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,那么一直都无法生效.学的教训。good lucky for me


吴玉宏
16 声望1 粉丝

萌新