If there is no unified code specification, there will be a “Canton burning wax, Tianjin dog ignore” all kinds of taste, more unavoidable and “smell concurs”, these situations seriously affect the quality of application.

Therefore, we want to introduce some code scanning tools to detect our code before submission and ensure that the remote warehouse we submit to is uniform and standardized code

tool

Eslint (code scanning)+ Husky + Prettier (formatting code for you)+ Lint-Staged (staging of this modification instead of the entire project document)

The principle of

  • Git add adds the code to the staging area.

  • Git commit;

  • Husky triggers lint-staged hook functions registered in git pre-commit (git hooks) to be called and lint-staged. Such as

      "husky": {   
          "hooks": {      "pre-commit": "lint-staged",
          "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" }
      },
      "lint-staged": {    "*.{js,jsx,ts,tsx}":  "eslint --fix"  }
    Copy the code
  • Lint-passage Takes all submitted files and executes written tasks (ESLint and Prettier); If there is an error (that doesn’t pass ESlint), stop the task, print an error message, and wait for the fix before executing commit; Commit successfully, which can be pushed to remote

emphasis

  • Husky registers git hook functions to ensure that code scans are called when git commits.
  • Eslint completes scanning according to configured rules;
  • Lint-staged projects guarantee that only files currently added to git stage will be scanned. The reason for this is that if entire project files are scanned and code rules have not been detected prior to the project, hundreds or thousands of errors will occur, which will lead to crashes. Therefore, only the current ADD files are detected to achieve timely loss, and the historical code can be cut to the new branch for repair before merging.