根目录下 添加 .eslintrc.js 文件

在本地分支使用。

  1. module.exports = {
  2. env: {
  3. browser: true,
  4. es2021: true,
  5. },
  6. extends: ['plugin:react/recommended', 'airbnb'],
  7. parser: '@typescript-eslint/parser',
  8. parserOptions: {
  9. ecmaFeatures: {
  10. jsx: true,
  11. },
  12. ecmaVersion: 12,
  13. sourceType: 'module',
  14. },
  15. plugins: ['react', '@typescript-eslint'],
  16. rules: {
  17. // 必须使用单引号
  18. quotes: ['error', 'single'],
  19. // 不可使用未定义的变量
  20. 'no-undef': 2,
  21. // 不可定义未使用的变量
  22. 'no-unused-vars': [
  23. 2,
  24. {
  25. vars: 'all',
  26. args: 'none',
  27. },
  28. ],
  29. // 调用函数不允许使用空格
  30. 'no-spaced-func': 2,
  31. // 禁止修改const
  32. 'no-const-assign': 2,
  33. // 文件结尾要空一行
  34. 'eol-last': 0,
  35. // 必须使用分号
  36. semi: ['error', 'always'],
  37. // 禁止使用 ++/--
  38. 'no-plusplus': 0,
  39. // 禁止有无用的表达式
  40. 'no-unused-expressions': 2,
  41. // 禁止行内备注
  42. 'no-inline-comments': 2,
  43. // 禁止使用var
  44. 'no-var': 2,
  45. // 冒号后必须又个空格
  46. 'comma-spacing': [2, { before: false, after: true }],
  47. // 嵌套层级不能超过4层
  48. 'max-depth': [2, 4],
  49. // 禁止tab和空格锁进混用
  50. 'no-mixed-spaces-and-tabs': [2, false],
  51. // 无需给react组件加上displayName
  52. 'react/display-name': 0,
  53. 'no-use-before-define': 'off',
  54. 'react/function-component-definition': [
  55. 2,
  56. {
  57. // 命名组件允许使用箭头函数, 函数声明
  58. namedComponents: ['function-declaration', 'arrow-function'],
  59. },
  60. ],
  61. },
  62. }

Package.json 文件 dependencies 对象中新增以下

  1. "@typescript-eslint/eslint-plugin": "^5.30.0",
  2. "@typescript-eslint/parser": "^5.30.0",
  3. "babel-eslint": "^10.1.0",
  4. "eslint": "^7.32.0",
  5. "eslint-config-airbnb": "^19.0.4",
  6. "eslint-plugin-import": "^2.26.0",
  7. "eslint-plugin-jsx-a11y": "^6.6.0",
  8. "eslint-plugin-react": "^7.30.1",
  9. "eslint-plugin-react-hooks": "^4.6.0",