- 编辑器
- 代码规范
- http://editorconfig.org">http://editorconfig.org
- 告诉 EditorConfig 插件,这是根文件,不用继续往上查找
- 匹配全部文件
- 缩进风格,可选 space、tab
- 结尾换行符,可选 lf、cr、crlf
- 设置字符集
- 删除一行中的前后空格
- 在文件结尾插入新行
- 匹配md结尾的文件
- editorconfig-tools is unable to ignore longs strings or urls
- Git Hooks
- husky(git hooks)">husky(git hooks)
- commitlint(用于检查提交信息)">commitlint(用于检查提交信息)
编辑器
首选 Visual Studio Code ,其次 WebStorm
代码规范
ESLint
安装并初始化 ESLint
# 全局安装npm install -g eslint# cd到项目根目录下# 强制初始化package.jsonnpm init -force# 初始化ESLinteslint --init
使用方式
注释
例如在当前文件中禁止使用 console 关键字
/* eslint no-console: "error" */
文件
ESLint 支持 eslint.js , eslintrc.yaml , eslintrc.json 类型的配置文件。
例如 eslint.js 配置文件
module.exports = {root: true,parser: "@typescript-eslint/parser", // 解析器,本解析器支持TsparserOptions: {ecmaVersion: 2018, // 指定es版本sourceType: "module", // 代码支持es6,使用module},env: {// 环境es6: true,node: true,browser: true,},plugins: [// 插件"@typescript-eslint",],extends: [// 拓展"eslint:recommended","plugin:@typescript-eslint/recommended",],globals: {__WEEX__: true,WXEnvironment: true,},rules: {// 规则"no-console": process.env.NODE_ENV !== "production" ? 0 : 2,"no-useless-escape": 0,"no-empty": 0,},};
配置项
- parser - 解析器
- parserOptions - 解析器选项
- env 和 globals - 环境和全局变量
- rules - 规则
- off或0,关闭规则
- warn或1,开启规则
- error或2,开启规则,并会出错阻止代码运行
- plugins - 插件
- extends - 拓展
配置优先级
规则是使用离要检测的文件最近的 .eslintrc文件作为最高优先级。
- 行内配置
- 命令行选项
- 项目级配置
- IDE环境配置
EditorConfig
```bashhttp://editorconfig.org
告诉 EditorConfig 插件,这是根文件,不用继续往上查找
root = true
匹配全部文件
[*]
缩进风格,可选 space、tab
indent_style = space indent_size = 2
结尾换行符,可选 lf、cr、crlf
end_of_line = lf
设置字符集
charset = utf-8
删除一行中的前后空格
trim_trailing_whitespace = true
在文件结尾插入新行
insert_final_newline = true
匹配md结尾的文件
[*.md] trim_trailing_whitespace = false
[Makefile] indent_style = tab
editorconfig-tools is unable to ignore longs strings or urls
max_line_length = null
<a name="RQs0Y"></a>## prettier> [https://prettier.io/docs/en/install.html](https://prettier.io/docs/en/install.html)<a name="lGKey"></a>### 安装```bash$ yarn add --dev --exact prettier
新建文件
$ echo {}> .prettierrc# 新建不需要格式化文件$ echo {}> .prettierignore
// .prettierrc// https://prettier.io/docs/en/options.html{"printWidth": 120, // 单行宽度限制"tabWidth": 2, // tab 使用两个空格"useTabs": false, // 不使用制表符缩进,使用空格缩进"semi": false, // 代码需要分号结尾"singleQuote": true, // 使用单引号"jsxSingleQuote": true, // 在JSX中使用单引号而不是双引号"bracketSpacing": true, // 对象左右两侧需要空格"jsxBracketSameLine": false, // html 关闭标签换行"arrowParens": "avoid", // 单参数的箭头函数参数不需要括号"proseWrap": "never", // 参考 https://prettier.io/docs/en/options.html#prose-wrap"trailingComma": "none" // 参考 https://prettier.io/docs/en/options.html#trailing-commas};
自动化格式
$ npx mrm lint-staged# or$ npx mrm@2 lint-staged
// package.json{// ..."config": {"commitizen": {"path": "node_modules/cz-customizable"}},"gitHooks": {"pre-commit": "lint-staged"},"lint-staged": {"src/**/*.{js,css,md,ts,tsx,vue}": ["eslint --fix","git add"]}}
Git Hooks
详细的 HOOKS介绍 可点击这里查看 整体的 hooks 非常多,当时我们其中用的比较多的其实只有两个:
简单来说这两个钩子:
- commit-msg:可以用来规范化标准格式,并且可以按需指定是否要拒绝本次提交
- pre-commit:会在提交前被调用,并且可以按需指定是否要拒绝本次提交
| Git Hook | 调用时机 | 说明 |
| —- | —- | —- |
| pre-commit | git commit执行前
它不接受任何参数,并且在获取提交日志消息并进行提交之前被调用。脚本git commit以非零状态退出会导致命令在创建提交之前中止。 | 可以用git commit —no-verify绕过 | | commit-msg | git commit执行前
可用于将消息规范化为某种项目标准格式。
还可用于在检查消息文件后拒绝提交。 | 可以用git commit —no-verify绕过 |
husky(git hooks)
1. 安装依赖
$ yarn add husky --dev
2. 启动 hooks , 生成 .husky 文件夹
$ npx husky install
3. 配置 script 命令并且执行
$ npm set-script prepare "husky install"
4. 添加 commitlint 的 hook 到 husky中
$ npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'# win上的终端有时候不能正确的识别,可以分成两个步骤来操作:$ npx husky add .husky/commit-msg$ npx --no-install commitlint --edit $1
commitlint(用于检查提交信息)
1. 安装依赖
$ yarn add @commitlint/cli @commitlint/config-conventional
2. 创建 commitlint.config.js
增加配置项( config-conventional 默认配置点击可查看 ) 注:确保保存为
UTF-8的编码格式
$ echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js# 完整的文件# module.exports = {# // 继承的规则# extends: ['@commitlint/config-conventional'],# // 定义规则类型# rules: {# // type 类型定义,表示 git 提交的 type 必须在以下类型范围内# 'type-enum': [# 2,# 'always',# [# 'feat', // 新功能 feature# 'fix', // 修复 bug# 'docs', // 文档注释# 'style', // 代码格式(不影响代码运行的变动)# 'refactor', // 重构(既不增加新功能,也不是修复bug)# 'perf', // 性能优化# 'test', // 增加测试# 'chore', // 构建过程或辅助工具的变动# 'revert', // 回退# 'build' // 打包# ]# ],# // subject 大小写不做校验# 'subject-case': [0]# }# }
