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

如何衡量代码质量的好坏

衡量代码质量的唯一有效标准:WTF/min —— Robert C. Martin

360前端星计划--前端代码的自我修养_第1张图片

代码规范

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",
    }
}

360前端星计划--前端代码的自我修养_第2张图片

ESLint对框架最佳实践的帮助

360前端星计划--前端代码的自我修养_第3张图片

.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"
    ]
  }

自动检查

360前端星计划--前端代码的自我修养_第4张图片

格式

360前端星计划--前端代码的自我修养_第5张图片

流程化

360前端星计划--前端代码的自我修养_第6张图片

如何优雅地提交代码

  • 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规范

360前端星计划--前端代码的自我修养_第7张图片

 

合并提交

有的时候,我们会遇到多次Commit才能完成一个feature

这时git log就会出现多次记录,如下图所示,污染提交历史\

360前端星计划--前端代码的自我修养_第8张图片

  • 合并提交

 git rebase -i $GIT_LOG_VERSION$

360前端星计划--前端代码的自我修养_第9张图片

git commit --fixup HEAD

git rebase -i $GIT_LOG_VERSION$ --autosquash

git push -f origin master

360前端星计划--前端代码的自我修养_第10张图片

360前端星计划--前端代码的自我修养_第11张图片

你可能感兴趣的:(360前端星计划)