eslint常见错误

  • no-plusplus, i++, i—
  • no-unused-vars,
  • no-unused-expressions,
  • no-undef,
  • no-restricted-globals

等错误,可以在 .eslintrc 配置中 rules进行关闭

Missing radix parameter.(radix)

原因:ESLint检查 javascript代码语法时,压缩工具对语法的严谨性要求比较高
radix 是 2-36 之间的整数,表示被解析字符串的基数

  • parseInt的第二个参数默认是十进制
  • 第二参数有四种: 2、8、10、16,分别对应二进制、八进制、十进制、十六进制
  • 在parseInt语法进行转换时,还是要求明确加个第二个参数
  • parseInt(‘86400’, 10) 即可, parseInt(string, radix)

严谨的做eslint规范,增加了 git comit时的验证
package.json新增,pre-commit

  1. module.exports = {
  2. env: {
  3. node: true,
  4. mocha: true,
  5. jest: true,
  6. es6: true,
  7. browser: true
  8. },
  9. extends: [
  10. 'eslint:recommended',
  11. 'plugin:react/recommended',
  12. 'plugin:react-hooks/recommended'
  13. ],
  14. parser: 'babel-eslint',
  15. parserOptions: {
  16. ecmaFeatures: {
  17. jsx: true
  18. },
  19. ecmaVersion: 6,
  20. sourceType: 'module'
  21. },
  22. plugins: ['react', 'jsx-a11y', 'react-hooks', 'prettier'],
  23. settings: {
  24. react: {
  25. version: 'detect'
  26. }
  27. },
  28. globals: {
  29. JSX: true,
  30. React: true,
  31. NodeJS: true,
  32. Promise: true
  33. },
  34. rules: {
  35. 'no-console': 'off',
  36. 'consistent-return': 2,
  37. curly: [2, 'multi-or-nest'],
  38. 'dot-location': 0,
  39. eqeqeq: 2,
  40. 'no-alert': 2,
  41. 'no-eq-null': 2,
  42. 'no-lone-blocks': 2,
  43. 'no-return-await': 2,
  44. 'no-unused-expressions': 2,
  45. 'no-label-var': 1,
  46. 'array-bracket-spacing': 2,
  47. 'brace-style': 0,
  48. 'comma-spacing': 1,
  49. 'consistent-this': 1,
  50. 'eol-last': 0,
  51. 'multiline-ternary': [1, 'always-multiline'],
  52. 'new-cap': [
  53. 2,
  54. {
  55. capIsNew: false
  56. }
  57. ],
  58. 'no-trailing-spaces': 0,
  59. semi: ['error', 'never'],
  60. 'space-before-blocks': 2,
  61. 'space-in-parens': 2,
  62. 'spaced-comment': 2,
  63. 'switch-colon-spacing': [
  64. 'error',
  65. {
  66. after: true,
  67. before: false
  68. }
  69. ],
  70. 'arrow-spacing': 2,
  71. quotes: [0, 'single'],
  72. 'key-spacing': 2,
  73. 'comma-dangle': ['error', 'never'],
  74. 'react-hooks/exhaustive-deps': 0,
  75. 'no-empty-function': 0,
  76. 'react-native/no-inline-styles': 0,
  77. 'react/forbid-prop-types': 0,
  78. 'react/prop-types': 0,
  79. 'react/display-name': 0,
  80. 'prefer-promise-reject-errors': 0,
  81. 'react/no-array-index-key': 2,
  82. 'react/no-unused-state': 2,
  83. 'react/jsx-indent-props': 2,
  84. 'react/jsx-no-comment-textnodes': 1,
  85. 'react/jsx-no-duplicate-props': 2,
  86. 'react/jsx-no-target-blank': [
  87. 1,
  88. {
  89. enforceDynamicLinks: 'always'
  90. }
  91. ],
  92. 'react/jsx-no-undef': 2,
  93. 'react/jsx-props-no-multi-spaces': 1,
  94. 'react/jsx-tag-spacing': 1,
  95. 'react/jsx-uses-vars': 2,
  96. 'react/jsx-wrap-multilines': 2,
  97. 'react-hooks/rules-of-hooks': 2
  98. }
  99. }

pre-commit

  1. {
  2. "scripts": {
  3. "start": "node scripts/start.js",
  4. "build": "node scripts/build.js",
  5. "test": "node scripts/test.js",
  6. "lint": "eslint . --fix" //新增,用来修改代码
  7. },
  8. "pre-commit": [
  9. "lint" // 新增验证
  10. ],
  11. }

.eslintignore

.eslintnore

  1. node_modules
  2. dist/
  3. test
  4. build/
  5. public/
  6. scripts/
  7. static/
  8. config/
  9. *.min.js // 忽略压缩的 js