更新時間:2020-05-25 23:09:58
公司須要用到uni-app
開發新的項目,關於uni-app
的介紹能夠移步官網介紹。javascript
核心就是用Vue
寫多端應用程序,可是uni-app
的官方編輯器HbuilderX
對代碼校驗這一塊基本是空白。本身對Hbuilderx
配置也是有點模糊。css
用Vue-cli
建立的項目默認啓用了保存代碼檢查,可是uni-app
開發方式有點區別。編譯和打包集成到了工具內部。因此咱們只能在提交代碼以前來作代碼校驗了。html
關於爲何要作代碼校驗,一我的開發還好。要是團隊合做,沒有統一的規則。你是天津狗不理,我是長沙臭豆腐。那個工程寫出來,基本沒有維護性可言。要本身去維護別人寫的代碼就是一個態度,他寫的代碼像💩同樣,看不懂,只能重構。大多數人都是這個樣子,沒有例外。做爲一個程序員,是要想一想爲何那些大佬的代碼一看就能懂,寫代碼就像寫詩,在看看本身寫的,過幾天出了問題,等到親切問候了全家以後再來看是本身寫的。。。😮因而可知規範是很重要的。前端
0202年了, ECMAScript 都已經2020版本了,別再用
var
定義變量、用==
作if
作判斷了。vue
能夠參考約定式提交java
一種用於給提交信息增長人機可讀含義的規範。node
簡單來講就是提交代碼的規範。git
已經詳細記錄到我的的文檔網站,這裏不作詳細介紹 地址程序員
yarn add eslint babel-eslint eslint-plugin-vue husky lint-staged -D
eslint
或stylelint
的命令時,只會檢查咱們經過git add
添加到暫存區的文件,能夠避免咱們每次檢查都把整個項目的代碼都檢查一遍
eslint的配置複製於
PanJiaChen
大佬的項目,
vue-admin-template,關閉了一些實在是太變態的警告。。。(大部分是格式問題)
${app}/.eslintrc.js
es6
/** * @name: .eslintrc.js * @author: SunSeekerX * @Date: 2020-04-18 11:43:35 * @LastEditors: SunSeekerX * @LastEditTime: 2020-05-25 22:01:11 */ module.exports = { root: true, env: { browser: true, es6: true, node: true, }, // 配置js全局變量,由於是uni-app,全局的uni是不須要引入的,還有5+的plus對象 globals: { uni: 'readonly', plus: 'readonly', }, extends: ['plugin:vue/essential', 'eslint:recommended'], parserOptions: { parser: 'babel-eslint', }, rules: { 'no-console': [ 'warn', { allow: ['warn', 'error'], }, ], 'no-eval': 'error', 'no-alert': 'error', 'vue/max-attributes-per-line': [ 0, { singleline: 10, multiline: { max: 1, allowFirstLine: false, }, }, ], 'vue/singleline-html-element-content-newline': 'off', 'vue/multiline-html-element-content-newline': 'off', 'vue/name-property-casing': ['error', 'PascalCase'], 'vue/no-v-html': 'off', 'accessor-pairs': 2, 'arrow-spacing': [ 2, { before: true, after: true, }, ], 'block-spacing': [2, 'always'], 'brace-style': [ 2, '1tbs', { allowSingleLine: true, }, ], camelcase: [ 0, { properties: 'always', }, ], 'comma-dangle': [2, 'only-multiline'], 'comma-style': [2, 'last'], 'constructor-super': 2, curly: [2, 'multi-line'], 'dot-location': [2, 'property'], 'eol-last': 2, eqeqeq: ['error', 'always', { null: 'ignore' }], 'generator-star-spacing': [ 2, { before: true, after: true, }, ], 'handle-callback-err': [2, '^(err|error)$'], 'jsx-quotes': [2, 'prefer-single'], 'new-cap': [ 2, { newIsCap: true, capIsNew: false, }, ], 'new-parens': 2, 'no-array-constructor': 2, 'no-caller': 2, 'no-class-assign': 2, 'no-cond-assign': 2, 'no-const-assign': 2, 'no-control-regex': 0, 'no-delete-var': 2, 'no-dupe-args': 2, 'no-dupe-class-members': 2, 'no-dupe-keys': 2, 'no-duplicate-case': 2, 'no-empty-character-class': 2, 'no-empty-pattern': 2, 'no-ex-assign': 2, 'no-extend-native': 2, 'no-extra-bind': 2, 'no-extra-boolean-cast': 2, 'no-extra-parens': [2, 'functions'], 'no-fallthrough': 2, 'no-floating-decimal': 2, 'no-func-assign': 2, 'no-implied-eval': 2, 'no-inner-declarations': [2, 'functions'], 'no-invalid-regexp': 2, 'no-irregular-whitespace': 2, 'no-iterator': 2, 'no-label-var': 2, 'no-labels': [ 2, { allowLoop: false, allowSwitch: false, }, ], 'no-lone-blocks': 2, 'no-mixed-spaces-and-tabs': 1, 'no-multi-spaces': 2, 'no-multi-str': 2, 'no-native-reassign': 2, 'no-negated-in-lhs': 2, 'no-new-object': 2, 'no-new-require': 2, 'no-new-symbol': 2, 'no-new-wrappers': 2, 'no-obj-calls': 2, 'no-octal': 2, 'no-octal-escape': 2, 'no-path-concat': 2, 'no-proto': 2, 'no-redeclare': 2, 'no-regex-spaces': 2, 'no-return-assign': [2, 'except-parens'], 'no-self-assign': 2, 'no-self-compare': 2, 'no-sequences': 2, 'no-shadow-restricted-names': 2, 'no-spaced-func': 2, 'no-sparse-arrays': 2, 'no-this-before-super': 2, 'no-throw-literal': 2, 'no-trailing-spaces': 0, 'no-undef': 2, 'no-undef-init': 2, 'no-unexpected-multiline': 2, 'no-unmodified-loop-condition': 2, 'no-unneeded-ternary': [ 2, { defaultAssignment: false, }, ], 'no-unreachable': 2, 'no-unsafe-finally': 2, 'no-unused-vars': [ 2, { vars: 'all', args: 'none', }, ], 'no-useless-call': 2, 'no-useless-computed-key': 2, 'no-useless-constructor': 2, 'no-useless-escape': 0, 'no-whitespace-before-property': 2, 'no-with': 2, 'one-var': [ 2, { initialized: 'never', }, ], 'operator-linebreak': [ 2, 'after', { overrides: { '?': 'before', ':': 'before', }, }, ], 'padded-blocks': [2, 'never'], quotes: [ 2, 'single', { avoidEscape: true, allowTemplateLiterals: true, }, ], 'semi-spacing': [ 2, { before: false, after: true, }, ], 'space-before-function-paren': [2, 'never'], 'space-in-parens': [2, 'never'], 'space-infix-ops': 1, 'space-unary-ops': [ 2, { words: true, nonwords: false, }, ], 'spaced-comment': [ 2, 'always', { markers: [ 'global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',', ], }, ], 'template-curly-spacing': [2, 'never'], 'use-isnan': 2, 'valid-typeof': 2, 'wrap-iife': [2, 'any'], 'yield-star-spacing': [2, 'both'], yoda: [2, 'never'], 'prefer-const': 2, 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, 'object-curly-spacing': [ 2, 'always', { objectsInObjects: false, }, ], 'array-bracket-spacing': [2, 'never'], 'no-prototype-builtins': 0, // 自定義開始 'vue/html-indent': 0, 'vue/html-closing-bracket-newline': 0, 'vue/html-self-closing': 0, indent: 0, semi: 0, 'comma-spacing': 0, 'space-before-blocks': 0, 'keyword-spacing': 0, 'key-spacing': 0, 'no-multiple-empty-lines': 0, }, }
刪除了沒必要要的信息
${app}/package.json
{ "name": "ybt-mp", "version": "1.0.0", "scripts": { "gc": "git add -A && git cz && git pull && git push", "lint": "eslint --ext .js,.vue ./" }, "license": "MIT", "devDependencies": { "@commitlint/cli": "^8.3.5", "@commitlint/config-conventional": "^8.3.4", "babel-eslint": "^10.1.0", "cz-conventional-changelog": "3.2.0", "eslint": "^7.1.0", "eslint-plugin-vue": "^6.2.2", "husky": "^4.2.5", "lint-staged": "^10.2.6" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } }, "dependencies": { "uview-ui": "^1.2.8" }, "commitlint": { "extends": [ "@commitlint/config-conventional" ] }, "husky": { "hooks": { "pre-commit": "lint-staged", "commit-msg": "commitlint -e $GIT_PARAMS" } }, "lint-staged": { "*.{js,vue}": [ "yarn lint", "git add" ] } }
本地執行
yarn gc # 實際執行,這是我的本身定義的命令,由於常常須要推送和拉取代碼,核心就是git cz替代git commit -m ''能夠選擇本次提交的類型,很方便 git add -A && git cz && git pull && git push
示例
SunSeekerX@SunSeekerX-NoteBook MINGW64 /w/Coding/uni-app/ybt-mp (master) $ yarn gc yarn run v1.22.4 $ git add -A && git cz && git pull && git push cz-cli@4.1.2, cz-conventional-changelog@3.2.0 ? Select the type of change that you're committing: feat: A new feature ? What is the scope of this change (e.g. component or file name): (press enter to skip) ? Write a short, imperative tense description of the change (max 66 chars): (16) 加入代碼提交eslint校驗配置 ? Provide a longer description of the change: (press enter to skip) ? Are there any breaking changes? No ? Does this change affect any open issues? No husky > pre-commit (node v12.16.3) ‼ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index. [STARTED] Preparing... [SUCCESS] Preparing... [STARTED] Running tasks... [STARTED] Running tasks for *.{js,vue} [STARTED] yarn lint [SUCCESS] yarn lint [STARTED] git add [SUCCESS] git add [SUCCESS] Running tasks for *.{js,vue} [SUCCESS] Running tasks... [STARTED] Applying modifications... [SUCCESS] Applying modifications... [STARTED] Cleaning up... [SUCCESS] Cleaning up... husky > commit-msg (node v12.16.3) [master c050a53] feat: 加入代碼提交eslint校驗配置 11 files changed, 1851 insertions(+), 1076 deletions(-) create mode 100644 .eslintignore rewrite components/ybt-patient-card/ybt-patient-card.vue (97%) rewrite pages/home/home.vue (84%) rewrite pages/patrol/patrol.vue (97%) Already up to date. Enumerating objects: 44, done. Counting objects: 100% (44/44), done. Delta compression using up to 4 threads Compressing objects: 100% (19/19), done. Writing objects: 100% (23/23), 16.87 KiB | 2.11 MiB/s, done. Total 23 (delta 11), reused 0 (delta 0) To http://gitlab.lvyii.com/yibiaotong/ybt-mp.git be3ffc1..c050a53 master -> master Done in 19.97s.
一條命令搞定了代碼提交、校驗、拉取、推送。固然遇到有衝突的狀況仍是須要本身手動解決。
yarn lint # 實際執行,調用eslint檢查後綴是js和vue的文件,暫時沒有加入css,和scss校驗 eslint --ext .js,.vue ./
示例
SunSeekerX@SunSeekerX-NoteBook MINGW64 /w/Coding/uni-app/ybt-mp (master) $ yarn lint yarn run v1.22.4 $ eslint --ext .js,.vue ./ W:\Coding\uni-app\ybt-mp\main.js 31:7 error 's' is assigned a value but never used no-unused-vars W:\Coding\uni-app\ybt-mp\pages\home\home.vue 6:2 warning Mixed spaces and tabs no-mixed-spaces-and-tabs 201:7 warning Unexpected console statement no-console W:\Coding\uni-app\ybt-mp\pages\patrol\patrol-detail\patrol-detail.vue 124:4 warning Unexpected console statement no-console W:\Coding\uni-app\ybt-mp\pages\patrol\patrol-list\patrol-list.vue 38:4 warning Unexpected console statement no-console W:\Coding\uni-app\ybt-mp\utils\app-update.js 40:7 warning Unexpected console statement no-console 45:9 warning Unexpected console statement no-console 48:9 warning Unexpected console statement no-console 60:3 warning Unexpected console statement no-console W:\Coding\uni-app\ybt-mp\utils\index.js 29:4 warning Unexpected console statement no-console 42:4 warning Unexpected console statement no-console 56:4 warning Unexpected console statement no-console ✖ 12 problems (1 error, 11 warnings) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
通過這麼配置以後,代碼的質量和規範相對於沒有配置以前維護性大大提升,同時也能夠避免一些錯誤的發生。但工具終究是工具,寫代碼的終究是人,在編寫代碼的同時,養成好的編碼習慣和優化意識才是最重要的。
在我眼中程序員能夠不穿很時髦的衣服,可是必定要穿的乾淨。多多少少有點潔癖、有點強迫症、😂
參考文章:lint-staged和husky在pre-commit階段作代碼檢查
SunSeekerX,前端開發、Nodejs開發、小程序、uni-app
開發、等等
喜歡探討技術實現方案和細節,完美主義者,見不得bug
。
Github:https://github.com/SunSeekerX
我的博客:https://yoouu.cn/
我的在線筆記:https://sunseekerx.yoouu.cn/