eslint prettier husky lint-staged typescript eslint-config-alloy
为构建项目代码质量保驾护航一、新建一个文件夹,打开命令行,npm init -y
建立package.json
二、安装依赖npm install --save-dev eslint babel-eslint eslint-config-alloy
三、在项目根目录下建立一个.eslintrc.js
或 .eslintrc.json
的配置文件:javascript
// .eslintrc.js module.exports = { extends: [ 'alloy', ], };
四、在项目根目录下建立一个index.js
,复制下面内容:css
var myName = 'Tom'; console.log(`My name is ${myNane}`);
五、在命令行输入npx eslint index.js
html
// eslint 报错信息: ✖ 2 problems (2 errors, 0 warnings) error Unexpected var, use let or const instead no-var error 'myNane' is not defined no-undef
六、使用npx eslint index.js --fix
自动修复某些规则前端
// 这时 var 变成了 let // 还剩下一个没法自动修复的错误 ✖ 1 problem (1 error, 0 warnings) error 'myNane' is not defined no-undef
一、因为 ESLint 默认使用 Espree 进行语法解析,没法识别 TypeScript 的一些语法,故咱们须要安装 @typescript-eslint/parser,替代掉默认的解析器,别忘了同时安装 typescript:vue
npm install --save-dev typescript @typescript-eslint/parser
二、接下来须要安装对应的插件 @typescript-eslint/eslint-plugin 它做为 eslint 默认规则的补充,提供了一些额外的适用于 ts 语法的规则。java
npm install --save-dev @typescript-eslint/eslint-plugin
三、修改配置文件node
module.exports = { extends: [ 'alloy', ], parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], rules: { // 禁止使用 var 'no-var': "error", // 优先使用 interface 而不是 type '@typescript-eslint/consistent-type-definitions': [ "error", "interface" ] } }
no-var
是 ESLint 原生的规则(咱们刚刚已经用到了这个规则,它被包含在alloy
中,此处会覆盖),@typescript-eslint/consistent-type-definitions 是 @typescript-eslint/eslint-plugin 新增的规则关闭、警告和报错的含义以下:
四、新建index.ts
文件:react
var myName = 'Tom'; console.log(`My name is ${myNane}`); console.log(`My name is ${myName.toStrng()}`); type Foo = {};
五、在命令行输入npx eslint index.ts
,以下能够看到报错信息以及可修复项jquery
1:1 error Unexpected var, use let or const instead no-var 2:27 error 'myNane' is not defined no-undef 4:6 error Use an `interface` instead of a `type` @typescript-eslint/consistent-type-definitions ✖ 3 problems (3 errors, 0 warnings) 2 errors and 0 warnings potentially fixable with the `--fix` option.
一、根目录新建一个src文件夹,将咱们的index.js
和index.ts
放进去
二、在package.json
中的scripts
新增:git
{ "scripts": { // 由于eslint不是全局安装的,因此要使用npx "eslint": "npx eslint src --ext .js,.ts,tsx" // eslint 默认不会检查 .ts 后缀的文件,因此须要加上参数 --ext .ts } }
三、而后npm run lint
就能够看到src下全部指定后缀文件的报错信息
ESLint
的配置过程AlloyTeam
实现可自定义拓展的ESLint
规则一、安装技术栈相关依赖
// Eslint npm install --save-dev eslint babel-eslint eslint-config-alloy // React npm install --save-dev eslint babel-eslint eslint-plugin-react eslint-config-alloy // Vue npm install --save-dev eslint babel-eslint vue-eslint-parser eslint-plugin-vue eslint-config-alloy // TypeScript npm install --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-alloy // TypeScript React npm install --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-config-alloy
二、配置.eslintrc.js
文件
/* .eslintrc.js */ module.exports = { extends: [ 'alloy', // 都须要 'alloy/vue', //vue项目须要 'alloy/react', //react项目须要 'alloy/typescript', //ts项目须要 ], env: { // 你的环境变量(包含多个预约义的全局变量) // // browser: true, // node: true, // mocha: true, // jest: true, // jquery: true }, globals: { // 你的全局变量(设置为 false 表示它不容许被从新赋值) // // myGlobal: false }, rules: { // 自定义你的规则 } };
三、接下来就能够直接用eslint
命令检查文件了
四、这样就引入了alloy团队的lint规则了,而后能够用rules覆盖你不爽的规则,直接采用开源规则是为了不重复造轮子,你也能够选择别的团队,或者本身定义一套
在编辑器中集成 ESLint 检查,能够在开发过程当中就发现错误,甚至能够在保存时自动修复错误,极大的增长了开发效率
一、先安装 ESLint 插件,打开 VSCode 点击「扩展」按钮,搜索 ESLint,而后安装便可
二、在「文件 => 首选项 => 设置 => 工做区」中(也能够在项目根目录下建立一个配置文件 .vscode/settings.json
),添加如下配置:
{ // VSCode 中的 ESLint 插件默认是不会检查 `.vue`、`.ts` 或 `.tsx` 后缀的 "eslint.validate": [ "javascript", "javascriptreact", "vue", "typescript", "typescriptreact" ], // 开启保存时自动修复 "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, // 指定VSCode用于IntelliSense(智能感知)的ts版本,将内置版本更换为工做区版本 "typescript.tsdk": "node_modules/typescript/lib" }
Prettier 是一个代码格式化工具,相比于 ESLint 中的代码格式规则,它提供了更少的选项,可是却更加专业。
AlloyTeam 推荐用 Prettier 管理格式化相关的规则,用 ESLint 来检查它更擅长的逻辑错误。
一、安装 Prettier
npm install --save-dev prettier
二、配置 .prettierrc.js
仅供参考:
// .prettierrc.js module.exports = { // 一行最多 100 字符 printWidth: 100, // 使用 4 个空格缩进 tabWidth: 4, // 不使用缩进符,而使用空格 useTabs: false, // 行尾须要有分号 semi: true, // 使用单引号 singleQuote: true, // 对象的 key 仅在必要时用引号 quoteProps: 'as-needed', // jsx 不使用单引号,而使用双引号 jsxSingleQuote: false, // 末尾不须要逗号 trailingComma: 'none', // 大括号内的首尾须要空格 bracketSpacing: true, // jsx 标签的反尖括号须要换行 jsxBracketSameLine: false, // 箭头函数,只有一个参数的时候,也须要括号 arrowParens: 'always', // 每一个文件格式化的范围是文件的所有内容 rangeStart: 0, rangeEnd: Infinity, // 不须要写文件开头的 @prettier requirePragma: false, // 不须要自动在文件开头插入 @prettier insertPragma: false, // 使用默认的折行标准 proseWrap: 'preserve', // 根据显示样式决定 html 要不要折行 htmlWhitespaceSensitivity: 'css', // 换行符使用 lf endOfLine: 'lf' };
一、在.vscode/settings.json
中添加配置:
{ // 保存时自动格式化全部支持文件 javascript/javascriptreact/typescript/typescriptreact/json/graphql "editor.formatOnSave": true, "editor.defaultFormatter": "esbenp.prettier-vscode", }
二、这时咱们保存文件的时候,已经能够自动格式化了
三、也能够指定格式化文件类型:
{ // Set the default "editor.formatOnSave": false, // Enable per-language "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true } }
ESLint、Prettier
集成了VSCode插件,实现了错误提示和保存自动修复git hook
就是.git文件夹的hooks下的一些钩子函数,特定时机他们将被调用cd .git/hooks ls -l // 打印以下: total 96 -rwxr-xr-x 1 zzc staff 478 10 21 2019 applypatch-msg.sample -rwxr-xr-x 1 zzc staff 896 10 21 2019 commit-msg.sample -rwxr-xr-x 1 zzc staff 3327 10 21 2019 fsmonitor-watchman.sample -rwxr-xr-x 1 zzc staff 189 10 21 2019 post-update.sample -rwxr-xr-x 1 zzc staff 424 10 21 2019 pre-applypatch.sample -rwxr-xr-x 1 zzc staff 1638 10 21 2019 pre-commit.sample -rwxr-xr-x 1 zzc staff 1348 10 21 2019 pre-push.sample -rwxr-xr-x 1 zzc staff 4898 10 21 2019 pre-rebase.sample -rwxr-xr-x 1 zzc staff 544 10 21 2019 pre-receive.sample -rwxr-xr-x 1 zzc staff 1492 10 21 2019 prepare-commit-msg.sample -rwxr-xr-x 1 zzc staff 3610 10 21 2019 update.sample
.sample
为各个钩子的案例脚本,能够把sample去掉,直接编写shell脚原本执行。Requires Node >= 10 and Git >= 2.13.0.
husky
新老版本的配置方式和使用变化较大,老版本请自行升级,详见 husky 一、安装 husky
npm install husky --save-dev
二、编辑 package.json
文件:
{ "husky": { "hooks": { "pre-commit": "eslint src/**/*.js" } }, }
三、尝试 git commit
提交,就会先执行eslint src/**/*.js
,代码没有问题才会被真正提交
四、这样每次提交代码,eslint
都会检查全部文件,若是报错过多,必定会崩溃
lint-staged requires Node.js version 10.13.0 or later.
一、安装 lint-staged
npm install lint-staged --save-dev
二、新增 package.json
配置:
{ "lint-staged": { "src/**/*.js": "eslint" } }
三、如此husky
只负责注册git hook
,后续操做交给lint-staged
,只对改动的文件执行任务,并且能够很方便
地配置多条命令:
{ "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "src/**/*.js": ["eslint --fix", "prettier --write"] } }
四、如上,咱们提交代码以前,程序会自动修复eslint
配置,格式化prettier
配置
eslint、prettier
配置,并且大部分还须要手动修复才行git commit -m -n "跳过代码代码预检"
跳过检查,慎用--save-dev
安装在项目内部husky lint-staged
配置都放在package.json
中,如今eslint prettier husky lint-staged
都支持多种后缀配置文件,建议采用.js
统一格式,也方便拓展:// .eslintrc.js module.exports = { extends: [ 'alloy', ], }; // .prettierrc.js module.exports = { // 一行最多 100 字符 printWidth: 100, // 使用 4 个空格缩进 tabWidth: 4, // ... }; // .huskyrc.js module.exports = { 'hooks': { 'pre-commit': "lint-staged" } } // .lintstagedrc.js module.exports = { "src/**/*.{js,ts}": "eslint" }
.huskyrc.js
// 数组方式配置多条命令 const tasks = arr => arr.join(' && ') module.exports = { 'hooks': { 'pre-commit': tasks([ 'npm run lint', 'npm test' ]) } }
.lintstagedrc.js
module.exports = { // 若是超过10个暂存文件,则在整个存储库上运行eslint '**/*.js?(x)': (filenames) => filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}`, "*.css": "stylelint", "*.scss": "stylelint --syntax=scss", // 对ts文件运行tsc,但不传递任何参数 '**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit' }