如何衡量代码质量的好坏
衡量代码质量的唯一有效标准:WTF/min —— Robert C. Martin
代码规范
yarn global add eslint
{
"extends": "eslint:recommended",
"rules": {
// enable additional rules
"indent": ["error", 4],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double"],
"semi": ["error", "always"],
// override default options for rules from base configurations
"comma-dangle": ["error", "always"],
"no-cond-assign": ["error", "always"],
// disable rules from base configurations
"no-console": "off",
}
}
ESLint对框架最佳实践的帮助
.eslintrc.js
module.exports = {
"env": {
"browser": true,
"es6": true
},
"extends": [
"standard"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"no-console" : ["error"]
}
};
//src/index.js
function helloWorld (user) {
console.log(`hello,${user}`)
}
export default helloWorld
husky + lint-staged
//.git/hooks/pre-commit
. "$(dirname "$0")/husky.sh"
"scripts": {
"precommit": "lint-staged"
},
"lint-staged": {
"src/**/*.js": [
"eslint --fix --ext .js",
"prettier --write",
"git add"
]
}
自动检查
格式
流程化
如何优雅地提交代码
{
"name": "75start-project",
"version": "1.1.0",
"description": "code maintain",
"main": "index.js",
"scripts": {
"commit": "npx git-cz",
"release": "standard-version"
},
"author": "sunlei",
"license": "MIT",
"devDependencies": {
"cz-conventional-changelog": "3.1.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
yarn global add commitizen cz-conventional-changelog standard-version
commitizen init cz-conventional-changelog --yarn --dev --exact
Comments规范
合并提交
有的时候,我们会遇到多次Commit才能完成一个feature
这时git log就会出现多次记录,如下图所示,污染提交历史\
git rebase -i $GIT_LOG_VERSION$
git commit --fixup HEAD
git rebase -i $GIT_LOG_VERSION$ --autosquash
git push -f origin master