360前端星计划--前端代码的自我修养

如何衡量代码质量的好坏

衡量代码质量的唯一有效标准: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"
    ]
  }

自动检查

格式

流程化

如何优雅地提交代码

  • git commit message规范
{
  "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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值