eslint.config.ts 6.4 KB

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