.eslintrc.js配置,项目自用版
创建.eslintrc.js文件,内容如下:
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const mode = process.env.NODE_ENV === 'development' ? 1 : 2;
module.exports = {
extends: [require.resolve('@umijs/fabric/dist/eslint')],
plugins: ['simple-import-sort', 'import'],
env: {
browser: true,
node: true,
es6: true,
},
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts'],
},
},
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'],
},
'import/extensions': ['.js', '.mjs', '.jsx', '.ts', '.tsx', '.d.ts'],
'import/external-module-folders': ['node_modules', 'node_modules/@types'],
polyfills: ['fetch', 'Promise', 'URL', 'object-assign'],
react: {
version: 'detect',
},
},
rules: {
'@typescript-eslint/array-type': 1,
'@typescript-eslint/no-empty-function': 1,
'@typescript-eslint/no-empty-interface': 1,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-non-null-asserted-optional-chain': 1,
'@typescript-eslint/no-shadow': 1,
'@typescript-eslint/no-unused-expressions': 0, //启用后,无法使用 boolean && fn()
'@typescript-eslint/no-unused-vars': 1,
'@typescript-eslint/no-use-before-define': 0, //启用后,useEffect中的函数调用得写到useEffect之上
'@typescript-eslint/no-var-requires': 1,
'@typescript-eslint/triple-slash-reference': 0,
'@typescript-eslint/no-redeclare': 0,
'@typescript-eslint/no-inferrable-types': 0,
'@typescript-eslint/no-namespace': 0,
'array-callback-return': 2,
'consistent-return': 0, // 启用后,无法使用 if(xx) return;
'no-async-promise-executor': 2,
'no-case-declarations': 2,
complexity: [1, 15],
'max-params': [2, 5],
//'max-lines-per-function': [1, 500],
'max-depth': [1, 5],
'no-console': [mode, { allow: ['warn', 'error', 'info'] }],
'no-debugger': mode,
'no-nested-ternary': 0,
'no-plusplus': [2, { allowForLoopAfterthoughts: true }],
'no-param-reassign': 1,
'import/first': 1,
'no-undef': 1,
'no-template-curly-in-string': 1,
'react/display-name': 0,
'react-hooks/exhaustive-deps': 0,
'react-hooks/rules-of-hooks': 2,
'jsx-a11y/alt-text': 0,
'no-sequences': 2,
'no-useless-escape': 2,
'no-unused-vars': 0,
'no-unused-expressions': 0,
'no-mixed-operators': 0, //启用后,与默认编辑格式化冲突,+号和*号必须得加一个括号
'no-restricted-globals': 1,
'no-fallthrough': 1,
'default-case': 2,
'prefer-const': 1,
'no-mixed-spaces-and-tabs': 1,
'jsx-quotes': [1, 'prefer-double'],
'import/no-anonymous-default-export': 0, //默认匿名export
'react/prop-types': 0, // defaultProps 有类型即可
'jsx-a11y/anchor-is-valid': 0,
'simple-import-sort/imports': 2,
'no-dupe-keys': 2,
eqeqeq: 1,
},
overrides: [
{
files: ['demo.{ts,tsx}'],
rules: {
'no-console': 0,
'@typescript-eslint/no-unused-vars': 0,
},
},
{
files: ['*.js'],
rules: {
'@typescript-eslint/no-var-requires': 0,
},
},
],
};