EsLint

  1. npm install --save-dev eslint eslint-loader babel-loader babel-eslint eslint-plugin-react
  • eslint is the core JavaScript linter.
  • eslint-loader tells webpack that you want to use eslint in our build
  • babel-loader transpiles our code with webpack
  • babel-eslint provides linting for valid ES6 code
  • eslint-plugin-react extends ESLint rules to cover React

添加 eslint 配置文件,如果有不希望被限制的语法可以添加到“rule”字段

  1. touch .eslintrc
  1. {
  2. "parser": "babel-eslint",
  3. "env": {
  4. "browser": true,
  5. "commonjs": true,
  6. "es6": true,
  7. "node": true
  8. },
  9. "plugins": [
  10. "react"
  11. ],
  12. "extends": ["plugin:prettier/recommended", "plugin:react/recommended", "eslint:recommended"],
  13. "rules": {
  14. "quotes": [1, "double", "avoid-escape"],
  15. "react/prop-types": 0,
  16. "no-console": 0,
  17. "no-unused-vars": 1,
  18. "no-func-assign": 2,
  19. "no-const-assign": 2,
  20. "no-var": 2,
  21. "react/react-in-jsx-scope": 0
  22. }
  23. }

prettier

  1. npm install --save-dev prettier eslint-config-prettier eslint-plugin-prettier
  • prettier — the core prettier library. Duh.
  • eslint-plugin-prettier — this plugin allows you to format code using Prettier when you run —fix
  • eslint-config-prettier — This library solves the conflicts between eslint and prettier. It turns off conflicting rules. ESLint’s rules, not Prettier’s. Obviously.

vscode settings

配置 vscode 编辑器,可以自动提示并修复错误

.vscode/settings.json

  1. {
  2. "editor.formatOnSave": true,
  3. "[javascript]": {
  4. "editor.formatOnSave": false
  5. },
  6. "eslint.autoFixOnSave": true,
  7. "eslint.alwaysShowStatus": true,
  8. "files.associations": {
  9. "*.js": "javascriptreact"
  10. }
  11. }

git hook

  1. npm install --save-dev pretty-quick husky lint-staged

配置文件,使得自动修复错误

package.json

  1. "husky": {
  2. "hooks": {
  3. "pre-commit": "pretty-quick --staged"
  4. }
  5. },