eslint.config.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import js from '@eslint/js';
  2. import pluginTypeScript from '@typescript-eslint/eslint-plugin';
  3. import * as parserTypeScript from '@typescript-eslint/parser';
  4. import configPrettier from 'eslint-config-prettier';
  5. import { defineFlatConfig } from 'eslint-define-config';
  6. import importPlugin from 'eslint-plugin-import';
  7. import pluginPrettier from 'eslint-plugin-prettier';
  8. import pluginVue from 'eslint-plugin-vue';
  9. import * as parserVue from 'vue-eslint-parser';
  10. console.log(
  11. '\x1B[0;37;44m INFO \x1B[0m',
  12. '\x1B[0;;34m ' + `读取了: eslint配置文件` + ' \x1B[0m'
  13. );
  14. export default defineFlatConfig([
  15. {
  16. ...js.configs.recommended,
  17. ...importPlugin.flatConfigs.recommended,
  18. ignores: [
  19. 'node_modules',
  20. 'pnpm-lock.yaml',
  21. 'dist',
  22. 'components.d.ts',
  23. 'auto-imports.d.ts',
  24. 'electron-dist/**/*',
  25. 'deploy/**/*',
  26. '.DS_Store',
  27. '.eslintcache',
  28. ],
  29. languageOptions: {
  30. globals: {},
  31. },
  32. plugins: {
  33. prettier: pluginPrettier,
  34. import: importPlugin,
  35. },
  36. rules: {
  37. ...configPrettier.rules,
  38. ...pluginPrettier.configs?.recommended.rules,
  39. /**
  40. * 0 => off
  41. * 1 => warn
  42. * 2 => error
  43. */
  44. 'no-unused-vars': 'off', // 禁止出现未使用过的变量
  45. 'no-shadow': 'off', // 禁止变量声明与外层作用域的变量同名
  46. 'class-methods-use-this': 'off', // 类方法如果不使用this的话会报错
  47. 'no-console': 'off', // 此规则不允许调用console对象的方法。
  48. 'spaced-comment': ['error', 'always', { exceptions: ['-', '+'] }], // 该规则强制注释中 // 或 /* 后空格的一致性
  49. 'no-var': 'error', // 要求let或const代替var
  50. camelcase: [
  51. 'error',
  52. { properties: 'never' }, // properties默认always,即检查属性名;可以设置为never,即不检查属性名
  53. ], // 强制执行驼峰命名约定
  54. 'no-underscore-dangle': 'error', // 此规则不允许在标识符中使用悬空下划线。
  55. 'no-param-reassign': 'error', // 禁止对 function 的参数进行重新赋值
  56. 'no-nested-ternary': 'error', // 禁止嵌套三元
  57. 'no-plusplus': 'error', // 禁用一元操作符 ++ 和 --
  58. 'vars-on-top': 'error', // 要求所有的 var 声明出现在它们所在的作用域顶部
  59. 'prefer-const': 'error', // 要求使用 const 声明那些声明后不再被修改的变量
  60. 'prefer-template': 'error', // 要求使用模板字符串代替字符串连接
  61. 'new-cap': 'error', // 要求构造函数名称以大写字母开头
  62. 'no-restricted-syntax': [
  63. // 禁用一些语法
  64. 'error',
  65. // 'ForInStatement',
  66. // 'ForOfStatement',
  67. {
  68. selector: 'ForInStatement',
  69. /**
  70. * 用 map() / every() / filter() / find() / findIndex() / reduce() / some() / ... 遍历数组,
  71. * 和使用 Object.keys() / Object.values() / Object.entries() 迭代你的对象生成数组。
  72. * 拥有返回值得纯函数比这个更容易解释
  73. */
  74. message:
  75. 'for in会迭代遍历原型链(__proto__),建议使用map/every/filter等遍历数组,使用Object.{keys,values,entries}等遍历对象',
  76. },
  77. {
  78. selector: 'ForOfStatement',
  79. message:
  80. '建议使用map/every/filter等遍历数组,使用Object.{keys,values,entries}等遍历对象',
  81. },
  82. ], // https://github.com/BingKui/javascript-zh#%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%92%8C%E5%8F%91%E7%94%9F%E5%99%A8
  83. 'no-iterator': 'error', // 禁止使用__iterator__迭代器
  84. 'require-await': 'error', // 禁止使用不带 await 表达式的 async 函数
  85. 'no-empty': 'error', // 禁止空块语句
  86. 'guard-for-in': 'error', // 要求for-in循环包含if语句
  87. 'global-require': 'error', // 此规则要求所有调用require()都在模块的顶层,此规则在 ESLint v7.0.0中已弃用。请使用 中的相应规则eslint-plugin-node:https://github.com/mysticatea/eslint-plugin-node
  88. 'no-unused-expressions': [
  89. 'error',
  90. {
  91. allowShortCircuit: true, // 允许短路
  92. allowTernary: true, // 允许三元
  93. },
  94. ], // 禁止未使用的表达式,即let a = true && console.log(1)允许,但是true && console.log(1)不行
  95. 'object-shorthand': ['error', 'always'], // (默认)希望尽可能使用速记。var foo = {x:x};替换为var foo = {x};
  96. 'no-useless-escape': 'error', // 禁止不必要的转义字符
  97. 'import/order': [
  98. 'error',
  99. {
  100. groups: [
  101. 'builtin', // 如:import fs from 'fs';
  102. 'external', // 如:import _ from 'lodash';
  103. 'internal', // 如:import foo from 'src/foo';
  104. 'parent', // 如:import foo from '../foo';
  105. 'sibling', // 如:import bar from './bar';
  106. // ['sibling', 'parent'],
  107. // ['parent', 'sibling'],
  108. 'index', // 如:import main from './';
  109. 'object', // 如:import log = console.log;
  110. 'type', // 如:import type { Foo } from 'foo';
  111. ],
  112. pathGroups: [
  113. {
  114. pattern: '@/**',
  115. group: 'internal',
  116. },
  117. ],
  118. 'newlines-between': 'always', // 强制或禁止导入组之间的新行
  119. // 根据导入路径以字母顺序排列每个组中的顺序
  120. alphabetize: {
  121. order: 'asc', // 使用asc按升序排序,使用desc按降序排序(默认值:ignore)。
  122. caseInsensitive: true, // 使用true忽略大小写,而false考虑大小写(默认值:false)。
  123. orderImportKind: 'asc', // 使用asc以升序对各种导入类型进行排序,例如以type或typeof为前缀的导入,具有相同的导入路径。使用desc按降序排序(默认值:忽略)
  124. },
  125. },
  126. ],
  127. 'import/newline-after-import': 'error', // 强制在最后一个顶级导入语句或 require 调用之后有一个或多个空行
  128. 'import/no-extraneous-dependencies': 'error', // 禁止导入未在package.json中声明的外部模块。
  129. },
  130. },
  131. {
  132. files: ['**/*.?([cm])ts', '**/*.?([cm])tsx'],
  133. languageOptions: {
  134. parser: parserTypeScript,
  135. parserOptions: {
  136. sourceType: 'module',
  137. },
  138. },
  139. plugins: {
  140. '@typescript-eslint': pluginTypeScript,
  141. },
  142. rules: {
  143. ...pluginTypeScript.configs.strict.rules,
  144. // @typescript-eslint插件
  145. '@typescript-eslint/no-unused-vars': 'error',
  146. // '@typescript-eslint/restrict-template-expressions': [
  147. // 'error',
  148. // {
  149. // allowBoolean: true,
  150. // allowNumber: true,
  151. // },
  152. // ], // 强制模板文字表达式为string类型。即const a = {};console.log(`${a}`);会报错
  153. '@typescript-eslint/no-floating-promises': 'off', // 要求适当处理类似 Promise 的语句。即将await或者return Promise,或者对promise进行.then或者.catch
  154. '@typescript-eslint/no-explicit-any': 'off', // 不允许定义any类型。即let a: any;会报错
  155. '@typescript-eslint/no-non-null-assertion': 'off', // 禁止使用非空断言(后缀运算符!)。即const el = document.querySelector('.app');console.log(el!.tagName);会报错
  156. '@typescript-eslint/ban-ts-comment': 'off', // 禁止使用@ts-<directive>注释
  157. '@typescript-eslint/no-unsafe-assignment': 'off', // 不允许将具有类型的值分配any给变量和属性。即const a: any = {};const b = a;会报错
  158. '@typescript-eslint/no-unsafe-argument': 'off', // 不允许用any类型的值调用一个函数。即let a: any;Object.keys(a);会报错
  159. '@typescript-eslint/no-unsafe-member-access': 'off', // 不允许对类型为any的值进行成员访问。即const a: any = [];console.log(a[0]);会报错
  160. '@typescript-eslint/no-unsafe-return': 'off', // 不允许从一个函数中返回一个类型为any的值
  161. '@typescript-eslint/no-unsafe-call': 'off', // 不允许调用any类型的值
  162. '@typescript-eslint/no-var-requires': 'off', // 即不允许var foo = require('foo');。但是允许import foo = require('foo');
  163. '@typescript-eslint/restrict-plus-operands': 'off', // 要求加法的两个操作数是相同的类型并且是bigint, number, 或string。即const a = '1';console.log(a + 1);会报错
  164. '@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
  165. },
  166. },
  167. {
  168. files: ['**/*.vue'],
  169. languageOptions: {
  170. globals: {},
  171. parser: parserVue,
  172. parserOptions: {
  173. ecmaFeatures: {
  174. jsx: true,
  175. },
  176. extraFileExtensions: ['.vue'],
  177. parser: '@typescript-eslint/parser',
  178. sourceType: 'module',
  179. },
  180. },
  181. plugins: {
  182. vue: pluginVue,
  183. },
  184. processor: pluginVue.processors['.vue'],
  185. rules: {
  186. ...pluginVue.configs.base.rules,
  187. ...pluginVue.configs['vue3-essential'].rules,
  188. ...pluginVue.configs['vue3-recommended'].rules,
  189. 'vue/multi-word-component-names': 'off',
  190. },
  191. },
  192. ]);