eslint.config.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // https://eslint.org/docs/latest/use/configure/configuration-files-new
  2. import eslint from "@eslint/js";
  3. import pluginVue from "eslint-plugin-vue";
  4. import * as typescriptEslint from "typescript-eslint";
  5. import vueParser from "vue-eslint-parser";
  6. import globals from "globals";
  7. import configPrettier from "eslint-config-prettier";
  8. // 解析自动导入配置
  9. import fs from "node:fs";
  10. let autoImportGlobals = {};
  11. try {
  12. autoImportGlobals =
  13. JSON.parse(fs.readFileSync("./.eslintrc-auto-import.json", "utf-8")).globals || {};
  14. } catch (error) {
  15. // 文件不存在或解析错误时使用空对象
  16. console.warn("Could not load auto-import globals", error);
  17. }
  18. // Element Plus组件
  19. const elementPlusComponents = {
  20. // Element Plus 组件添加为全局变量,避免 no-undef 报错
  21. ElInput: "readonly",
  22. ElSelect: "readonly",
  23. ElSwitch: "readonly",
  24. ElCascader: "readonly",
  25. ElInputNumber: "readonly",
  26. ElTimePicker: "readonly",
  27. ElTimeSelect: "readonly",
  28. ElDatePicker: "readonly",
  29. ElTreeSelect: "readonly",
  30. ElText: "readonly",
  31. ElRadioGroup: "readonly",
  32. ElCheckboxGroup: "readonly",
  33. ElOption: "readonly",
  34. ElRadio: "readonly",
  35. ElCheckbox: "readonly",
  36. ElInputTag: "readonly",
  37. ElForm: "readonly",
  38. ElFormItem: "readonly",
  39. ElTable: "readonly",
  40. ElTableColumn: "readonly",
  41. ElButton: "readonly",
  42. ElDialog: "readonly",
  43. ElPagination: "readonly",
  44. ElMessage: "readonly",
  45. ElMessageBox: "readonly",
  46. ElNotification: "readonly",
  47. ElTree: "readonly",
  48. };
  49. export default [
  50. // 忽略文件配置
  51. {
  52. ignores: [
  53. "**/node_modules/**",
  54. "**/dist/**",
  55. "**/*.min.*",
  56. "**/auto-imports.d.ts",
  57. "**/components.d.ts",
  58. ],
  59. },
  60. // 基础 JavaScript 配置
  61. eslint.configs.recommended,
  62. // Vue 推荐配置
  63. ...pluginVue.configs["flat/recommended"],
  64. // TypeScript 推荐配置
  65. ...typescriptEslint.configs.recommended,
  66. // 全局配置
  67. {
  68. // 指定要检查的文件
  69. files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
  70. languageOptions: {
  71. ecmaVersion: "latest",
  72. sourceType: "module",
  73. globals: {
  74. ...globals.browser, // 浏览器环境全局变量
  75. ...globals.node, // Node.js 环境全局变量
  76. ...globals.es2022, // ES2022 全局对象
  77. ...autoImportGlobals, // 自动导入的 API 函数
  78. ...elementPlusComponents, // Element Plus 组件
  79. // 全局类型定义,解决 TypeScript 中定义但 ESLint 不识别的问题
  80. PageQuery: "readonly",
  81. PageResult: "readonly",
  82. OptionType: "readonly",
  83. ApiResponse: "readonly",
  84. ExcelResult: "readonly",
  85. CommonType: "readonly",
  86. updatorType: "readonly",
  87. creatorType: "readonly",
  88. TagView: "readonly",
  89. AppSettings: "readonly",
  90. __APP_INFO__: "readonly",
  91. },
  92. },
  93. plugins: {
  94. vue: pluginVue,
  95. "@typescript-eslint": typescriptEslint.plugin,
  96. },
  97. rules: {
  98. // 基础规则
  99. "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
  100. "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
  101. // ES6+ 规则
  102. "prefer-const": "error",
  103. "no-var": "error",
  104. "object-shorthand": "error",
  105. // 最佳实践
  106. eqeqeq: "off",
  107. "no-multi-spaces": "error",
  108. "no-multiple-empty-lines": ["error", { max: 1, maxBOF: 0, maxEOF: 0 }],
  109. // 禁用与 TypeScript 冲突的规则
  110. "no-unused-vars": "off",
  111. "no-undef": "off",
  112. "no-redeclare": "off",
  113. "@typescript-eslint/ban-ts-comment": "off",
  114. },
  115. },
  116. // Vue 文件特定配置
  117. {
  118. files: ["**/*.vue"],
  119. languageOptions: {
  120. parser: vueParser,
  121. parserOptions: {
  122. ecmaVersion: "latest",
  123. sourceType: "module",
  124. parser: typescriptEslint.parser,
  125. extraFileExtensions: [".vue"],
  126. },
  127. },
  128. rules: {
  129. // Vue 规则
  130. "vue/multi-word-component-names": "off",
  131. "vue/no-v-html": "off",
  132. "vue/require-default-prop": "off",
  133. "vue/require-explicit-emits": "error",
  134. "vue/no-unused-vars": "error",
  135. "vue/no-mutating-props": "off",
  136. "vue/valid-v-for": "warn",
  137. "vue/no-template-shadow": "warn",
  138. "vue/return-in-computed-property": "warn",
  139. "vue/block-order": [
  140. "error",
  141. {
  142. order: ["template", "script", "style"],
  143. },
  144. ],
  145. "vue/html-self-closing": [
  146. "error",
  147. {
  148. html: {
  149. void: "always",
  150. normal: "never",
  151. component: "always",
  152. },
  153. svg: "always",
  154. math: "always",
  155. },
  156. ],
  157. "vue/component-name-in-template-casing": ["error", "PascalCase"],
  158. "@typescript-eslint/no-explicit-any": "off",
  159. },
  160. },
  161. // TypeScript 文件特定配置
  162. {
  163. files: ["**/*.{ts,tsx,mts,cts}"],
  164. languageOptions: {
  165. parser: typescriptEslint.parser,
  166. parserOptions: {
  167. project: true,
  168. tsconfigRootDir: import.meta.dirname,
  169. },
  170. },
  171. rules: {
  172. // TypeScript 规则
  173. "@typescript-eslint/no-explicit-any": "off", // 允许使用any类型,方便开发
  174. "@typescript-eslint/no-empty-function": "off",
  175. "@typescript-eslint/no-empty-object-type": "off",
  176. "@typescript-eslint/ban-ts-comment": "off",
  177. "@typescript-eslint/no-non-null-assertion": "off",
  178. "@typescript-eslint/no-unused-vars": "warn", // 降级为警告
  179. "@typescript-eslint/no-unused-expressions": "warn", // 降级为警告
  180. "@typescript-eslint/consistent-type-imports": "off", // 关闭强制使用type import
  181. "@typescript-eslint/no-import-type-side-effects": "error",
  182. },
  183. },
  184. // .d.ts 文件配置
  185. {
  186. files: ["**/*.d.ts"],
  187. rules: {
  188. "@typescript-eslint/no-explicit-any": "off",
  189. "@typescript-eslint/no-unused-vars": "off",
  190. },
  191. },
  192. // CURD 组件配置
  193. {
  194. files: ["**/components/CURD/**/*.{ts,vue}"],
  195. rules: {
  196. "no-unused-vars": "off",
  197. "@typescript-eslint/no-unused-vars": "off",
  198. "@typescript-eslint/no-explicit-any": "off",
  199. },
  200. },
  201. // Prettier 集成(必须放在最后)
  202. configPrettier,
  203. ];