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
module.exports = {
env: {
node: true,
mocha: true,
jest: true,
es6: true,
browser: true
},
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended'
],
parser: 'babel-eslint',
parserOptions: {
ecmaFeatures: {
jsx: true
},
ecmaVersion: 6,
sourceType: 'module'
},
plugins: ['react', 'jsx-a11y', 'react-hooks', 'prettier'],
settings: {
react: {
version: 'detect'
}
},
globals: {
JSX: true,
React: true,
NodeJS: true,
Promise: true
},
rules: {
'no-console': 'off',
'consistent-return': 2,
curly: [2, 'multi-or-nest'],
'dot-location': 0,
eqeqeq: 2,
'no-alert': 2,
'no-eq-null': 2,
'no-lone-blocks': 2,
'no-return-await': 2,
'no-unused-expressions': 2,
'no-label-var': 1,
'array-bracket-spacing': 2,
'brace-style': 0,
'comma-spacing': 1,
'consistent-this': 1,
'eol-last': 0,
'multiline-ternary': [1, 'always-multiline'],
'new-cap': [
2,
{
capIsNew: false
}
],
'no-trailing-spaces': 0,
semi: ['error', 'never'],
'space-before-blocks': 2,
'space-in-parens': 2,
'spaced-comment': 2,
'switch-colon-spacing': [
'error',
{
after: true,
before: false
}
],
'arrow-spacing': 2,
quotes: [0, 'single'],
'key-spacing': 2,
'comma-dangle': ['error', 'never'],
'react-hooks/exhaustive-deps': 0,
'no-empty-function': 0,
'react-native/no-inline-styles': 0,
'react/forbid-prop-types': 0,
'react/prop-types': 0,
'react/display-name': 0,
'prefer-promise-reject-errors': 0,
'react/no-array-index-key': 2,
'react/no-unused-state': 2,
'react/jsx-indent-props': 2,
'react/jsx-no-comment-textnodes': 1,
'react/jsx-no-duplicate-props': 2,
'react/jsx-no-target-blank': [
1,
{
enforceDynamicLinks: 'always'
}
],
'react/jsx-no-undef': 2,
'react/jsx-props-no-multi-spaces': 1,
'react/jsx-tag-spacing': 1,
'react/jsx-uses-vars': 2,
'react/jsx-wrap-multilines': 2,
'react-hooks/rules-of-hooks': 2
}
}
pre-commit
{
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js",
"lint": "eslint . --fix" //新增,用来修改代码
},
"pre-commit": [
"lint" // 新增验证
],
}
.eslintignore
.eslintnore
node_modules
dist/
test
build/
public/
scripts/
static/
config/
*.min.js // 忽略压缩的 js