.eslintrc.cjs

34 阅读5分钟
```const { defineConfig } = require('eslint-define-config');
/// <reference types="@eslint-types/import" />
/// <reference types="@eslint-types/typescript-eslint" />

module.exports = defineConfig({
  env: {
    // browser: true,
    commonjs: true,
    es2023: true,
    node: true, // 告诉eslint代码是在Node.js环境下执行, 或者在文件首部添加一个特殊的注释 /* eslint-env node */
  },
  extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
  overrides: [],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
  },
  plugins: ['@typescript-eslint', 'import', 'promise'],
  rules: {
    '@typescript-eslint/no-redeclare': 2, // 禁止重复申明变量
    '@typescript-eslint/no-unused-vars': 2, // 禁止未使用的变量
    '@typescript-eslint/no-explicit-any': 0, // 允许使用any类型
    '@typescript-eslint/no-empty-object-type': 2, // 在容易混淆的地方禁止使用内置类型{}
    '@typescript-eslint/no-unsafe-function-type': 2, // 禁止内置Function
    '@typescript-eslint/no-wrapper-object-types': 2, // 禁止Object和内置类包装器,例如Number
    '@typescript-eslint/no-require-imports': 0, // 禁止使用require
    '@typescript-eslint/no-unused-expressions': 0, // 禁止不必要的表达式
    '@typescript-eslint/no-empty-function': 2, // 禁止空函数
    // '@typescript-eslint/no-use-before-define': 2, // 在定义变量之前禁止使用变量
    '@typescript-eslint/no-non-null-assertion': 2, // !禁止使用后缀运算符进行非空断言
    '@typescript-eslint/no-inferrable-types': 2, // 对初始化为数字、字符串或布尔值的变量或参数进行显式类型声明。
    '@typescript-eslint/consistent-type-imports': 2, // 使用 import type
    // 一个函数的复杂性不超过 10,所有分支、循环、回调加在一起,在一个函数里不超过 10 个
    complexity: [2, 10],
    // 一个函数的嵌套不能超过 4 层,多个 for 循环,深层的 if-else,这些都是罪恶之源
    'max-depth': [2, 4],
    // 一个函数最多有 3 层 callback,使用 async/await
    'max-nested-callbacks': [2, 3],
    // 一个函数最多 5 个参数。参数太多的函数,意味着函数功能过于复杂,请拆分
    'max-params': [2, 5],
    // 一个函数最多有 50 行代码,如果超过了,请拆分之,或者精简之
    'max-statements': [2, 50],
    'no-var': 2,
    'no-cond-assign': 2, //禁止在条件表达式中使用赋值语句
    'no-console': [2, { allow: ['log'] }], //禁止使用console
    'no-const-assign': 2, //禁止修改const声明的变量
    'no-constant-condition': 2, //禁止在条件中使用常量表达式 if(true) if(1)
    'no-control-regex': 2, //禁止在正则表达式中使用控制字符
    'no-debugger': 2, //禁止使用debugger
    'no-div-regex': 2, //不能使用看起来像除法的正则表达式/=foo/
    'no-dupe-keys': 2, //在创建对象字面量时不允许键重复 {a:1,a:1}
    'no-dupe-args': 2, //函数参数不能重复
    'no-duplicate-case': 2, //switch中的case标签不能重复
    'no-else-return': 2, //如果if语句里面有return,后面不能跟else语句
    'no-empty': 2, //块语句中的内容不能为空
    'no-empty-character-class': 2, //正则表达式中的[]内容不能为空
    'no-eq-null': 2, //禁止对null使用==或!=运算符
    'no-eval': 2, //禁止使用eval
    'no-ex-assign': 2, //禁止给catch语句中的异常参数赋值
    'no-extend-native': 2, //禁止扩展native对象
    'no-extra-bind': 2, //禁止不必要的函数绑定
    'no-extra-boolean-cast': 2, //禁止不必要的bool转换
    'no-extra-semi': 2, //禁止多余的冒号
    'no-fallthrough': 1, //禁止switch穿透
    'no-floating-decimal': 2, //禁止省略浮点数中的0 .5 3.
    'no-func-assign': 2, //禁止重复的函数声明
    'no-implicit-coercion': 2, //禁止隐式转换
    'no-implied-eval': 2, //禁止使用隐式eval
    'no-inner-declarations': [2, 'functions'], //禁止在块语句中使用声明(变量或函数)
    'no-label-var': 2, //label名不能与var声明的变量名相同
    'no-labels': 2, //禁止标签声明
    'no-lone-blocks': 2, //禁止不必要的嵌套块
    'no-lonely-if': 2, //禁止else语句内只有if语句
    'no-loop-func': 2, //禁止在循环中使用函数(如果没有引用外部变量不形成闭包就可以)
    'no-mixed-requires': 2, //声明时不能混用声明类型
    'no-multiple-empty-lines': [1, { max: 1 }], //空行最多不能超过1行
    'no-nested-ternary': 2, //禁止使用嵌套的三目运算
    'no-param-reassign': 2, //禁止给参数重新赋值
    'no-shadow': 2, //外部作用域中的变量不能与它所包含的作用域中的变量或参数同名
    'no-unexpected-multiline': 2, //避免多行表达式
    'no-underscore-dangle': 2, //    'no-unneeded-ternary': 2, //禁止不必要的嵌套 var isYes = answer === 1 ? true : false;
    'no-unreachable': 2, //不能有无法执行的代码
    'callback-return': 2, //避免多次调用回调什么的
    // camelcase: [2, { ignoreDestructuring: true }], //强制驼峰法命名
    'default-case': 2, //switch语句最后必须有default
    eqeqeq: 2, //必须使用全等
    'handle-callback-err': 2, //nodejs 处理错误
    'new-parens': 2, //new时必须加小括号
    'object-shorthand': 2, //强制对象字面量缩写语法
    'prefer-template': 2, // 要求使用模板字面量而非字符串连接
    'one-var': [2, { var: 'never', let: 'never', const: 'never' }], //连续声明
    'prefer-const': 2, //首选const
    'prefer-spread': 2, //首选展开运算
    'id-match': 2, //命名检测
    'sort-vars': 2, //变量声明时排序
    'require-await': 2, // 禁止使用不带await表达式的async函数
    'no-unused-expressions': [2, { allowShortCircuit: true, allowTernary: true }], // 禁止对程序状态没有影响的未使用的表达式
    'no-unneeded-ternary': 2, //禁止不必要的嵌套 var isYes = answer === 1 ? true : false;
    'sort-imports': [
      2,
      {
        ignoreCase: false,
        ignoreDeclarationSort: true,
        ignoreMemberSort: false,
        memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
        allowSeparatedGroups: false,
      },
    ], // 导入排序
    // 'no-duplicate-imports': 2, // 禁止重复导入
    'import/no-duplicates': 2, // 禁止重复导入
    'import/first': 2, // import放在最上面
    'import/order': 2, // 导入按照一定的顺序
    'import/newline-after-import': 2, // 导入语句后必须有一个空行
    'promise/no-return-wrap': 2, // 禁止在不需要时包裹值在 Promise.resolve 或 Promise.reject 中
    'promise/prefer-await-to-callbacks': 2, // 使用异步/等待而不是回调模式
    'promise/prefer-await-to-then': 2, // 使用 await 代替 then()/catch()/finally() 来读取 Promise 的值
  },
});