包含ts的编译及js的编译

    1. module.exports = {
    2. // 限制在一个特定的父级别目录内
    3. root: true,
    4. // 将 TypeScript 转换成与 estree 兼容的形式,以便在ESLint中使用
    5. parser: '@typescript-eslint/parser',
    6. // 要在配置文件里指定环境,使用 env 关键字指定你想启用的环境,并设置它们为 true
    7. env: {
    8. browser: true,
    9. es6: true,
    10. node: true,
    11. },
    12. plugins: ["@typescript-eslint"],
    13. // 一个配置文件可以被基础配置中的已启用的规则继承。
    14. extends: ['eslint:recommended', "plugin:@typescript-eslint/recommended"],
    15. // 注意,在使用自定义解析器时,为了让 ESLint 在处理非 ECMAScript 5 特性时正常工作,配置属性 parserOptions 仍然是必须的。解析器会被传入 parserOptions
    16. parserOptions: {
    17. // 指定ECMAScript 版本, 默认是是3,5;
    18. ecmaVersion: 2018,
    19. // 设置为 "script" (默认) 或 "module"(如果你的代码是 ECMAScript 模块)。
    20. sourceType: 'module',
    21. },
    22. /**
    23. * rules
    24. * "off" 或 0 - 关闭规则
    25. * "warn" 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
    26. * "error" 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)
    27. */
    28. rules: {
    29. // 强制使用一致的反勾号、双引号或单引号
    30. quotes: [2, 'single'],
    31. // 强制在 parseInt() 使用基数参数
    32. radix: [2, 'always'],
    33. // 强制使用骆驼拼写法命名约定
    34. camelcase: [2, { properties: 'always' }],
    35. // 强制使用一致的缩进
    36. indent: ['error', 2],
    37. // 'prettier/prettier': 2,
    38. // 要求使用 let 或 const 而不是 var
    39. 'no-var': 2,
    40. // 禁用未声明的变量,除非它们在 /*global */ 注释中被提到
    41. 'no-undef': 2,
    42. // 禁用 alert、confirm 和 prompt
    43. 'no-alert': 2,
    44. // 禁用 console
    45. 'no-console': [2, { allow: ['warn', 'error'] }],
    46. // 禁用 debugger
    47. 'no-debugger': 2,
    48. // 禁用 Object 的构造函数
    49. 'no-new-object': 2,
    50. // 禁止出现未使用过的变量
    51. 'no-unused-vars': 1,
    52. // 禁止对 String,Number 和 Boolean 使用 new 操作符
    53. 'no-new-wrappers': 2,
    54. // 禁止在变量定义之前使用它们
    55. 'no-use-before-define': ['error', { functions: false, classes: false }],
    56. // 禁用 Array 构造函数
    57. 'no-array-constructor': 2,
    58. // 禁用不必要的构造函数
    59. 'no-useless-constructor': 1,
    60. // 禁止对 function 的参数进行重新赋值
    61. 'no-param-reassign': [1, { props: true }],
    62. // 禁止 if 语句中 return 语句之后有 else 块
    63. 'no-else-return': [2, { allowElseIf: false }],
    64. // 禁用一元操作符 ++ 和 --
    65. 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
    66. // 禁用特定的语法
    67. 'no-restricted-syntax': [2, 'BinaryExpression[operator="in"]'],
    68. // 禁用嵌套的三元表达式
    69. 'no-nested-ternary': 2,
    70. // 禁止可以在有更简单的可替代的表达式时使用三元操作符
    71. 'no-unneeded-ternary': [2, { defaultAssignment: false }],
    72. // 禁止混合使用不同的操作符
    73. 'no-mixed-operators': [
    74. 1,
    75. {
    76. groups: [
    77. ['+', '-', '*', '/', '%', '**'],
    78. ['==', '!=', '===', '!==', '>', '>=', '<', '<='],
    79. ],
    80. // 同级别的可以省略括号
    81. allowSamePrecedence: true,
    82. },
    83. ],
    84. // 要求使用模板字面量而非字符串连接
    85. 'prefer-template': 2,
    86. // 要求回调函数使用箭头函数
    87. 'prefer-arrow-callback': 1,
    88. // 优先使用数组和对象解构
    89. 'prefer-destructuring': [1, { object: true, array: false }],
    90. // 要求或禁止对象字面量中方法和属性使用简写语法
    91. 'object-shorthand': [2, 'always', { avoidQuotes: true }],
    92. // 强制函数中的变量要么一起声明要么分开声明
    93. 'one-var': [2, 'never'],
    94. // 要求或禁止模板字符串中的嵌入表达式周围空格的使用
    95. 'template-curly-spacing': 2,
    96. // 强制在 function的左括号之前使用一致的空格
    97. 'space-before-function-paren': ['error', { anonymous: 'never', named: 'never', asyncArrow: 'always' }],
    98. // 强制尽可能地使用点号
    99. 'dot-notation': [1, { allowKeywords: true }],
    100. // 制单个语句的位置
    101. 'nonblock-statement-body-position': [2, 'below'],
    102. // 强制一致的缩进 关闭 ts/indent,base里面已经配置了 indent
    103. '@typescript-eslint/indent': 0,
    104. // 除导入语句外,禁止使用require语句
    105. '@typescript-eslint/no-var-requires': 0,
    106. // 在函数和类方法上需要显式的返回类型
    107. '@typescript-eslint/explicit-function-return-type': 0,
    108. // 在类属性和方法上需要显式的可访问性修饰符
    109. '@typescript-eslint/explicit-member-accessibility': [2, { accessibility: 'no-public' }],
    110. // 禁止使用任何类型
    111. '@typescript-eslint/no-explicit-any': 1,
    112. // 不允许对初始化为数字,字符串或布尔值的变量或参数进行显式类型声明
    113. '@typescript-eslint/no-inferrable-types': [2, { ignoreParameters: true, ignoreProperties: true }],
    114. // 官方没有找到这个配置
    115. '@typescript-eslint/interface-name-prefix': [2, 'always'],
    116. // 需要对数组使用T []或Array <T>
    117. '@typescript-eslint/array-type': 0,
    118. // 强制默认参数为最后
    119. '@typescript-eslint/default-param-last': 2,
    120. // 禁止使用自定义TypeScript模块和名称空间
    121. '@typescript-eslint/no-namespace': 0,
    122. // 在定义变量之前禁止使用变量 此处由 eslint-config-base 的 no-use-before-define 定义
    123. '@typescript-eslint/no-use-before-define': 0,
    124. // 禁止使用特定类型
    125. '@typescript-eslint/ban-types': [
    126. 2,
    127. {
    128. types: {
    129. Object: {
    130. message: 'Use object instead',
    131. fixWith: 'object',
    132. },
    133. '{}': {
    134. message: 'Use object instead',
    135. fixWith: 'object',
    136. },
    137. },
    138. },
    139. ],
    140. },
    141. };