eslint.config.mjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // 从 URL 和路径模块中导入必要的功能
  2. import fs from 'fs'
  3. import path, { dirname } from 'path'
  4. import { fileURLToPath } from 'url'
  5. // 从 ESLint 插件中导入推荐配置
  6. import pluginJs from '@eslint/js'
  7. import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
  8. import pluginVue from 'eslint-plugin-vue'
  9. import globals from 'globals'
  10. import tseslint from 'typescript-eslint'
  11. // 使用 import.meta.url 获取当前模块的路径
  12. const __filename = fileURLToPath(import.meta.url)
  13. const __dirname = dirname(__filename)
  14. // 读取 .auto-import.json 文件的内容,并将其解析为 JSON 对象
  15. const autoImportConfig = JSON.parse(fs.readFileSync(path.resolve(__dirname, '.auto-import.json'), 'utf-8'))
  16. export default [
  17. // 指定文件匹配规则
  18. {
  19. files: ['**/*.{js,mjs,cjs,ts,vue}']
  20. },
  21. // 指定全局变量和环境
  22. {
  23. languageOptions: {
  24. globals: {
  25. ...globals.browser,
  26. ...globals.node
  27. }
  28. }
  29. },
  30. // 扩展配置
  31. pluginJs.configs.recommended,
  32. ...tseslint.configs.recommended,
  33. ...pluginVue.configs['flat/essential'],
  34. // 自定义规则
  35. {
  36. // 针对所有 JavaScript、TypeScript 和 Vue 文件应用以下配置
  37. files: ['**/*.{js,mjs,cjs,ts,vue}'],
  38. languageOptions: {
  39. globals: {
  40. // 合并从 autoImportConfig 中读取的全局变量配置
  41. ...autoImportConfig.globals,
  42. // TypeScript 全局命名空间
  43. Api: 'readonly',
  44. Form: 'readonly'
  45. }
  46. },
  47. rules: {
  48. quotes: ['error', 'single'], // 使用单引号
  49. semi: ['error', 'never'], // 语句末尾不加分号
  50. 'no-var': 'error', // 要求使用 let 或 const 而不是 var
  51. '@typescript-eslint/no-explicit-any': 'off', // 禁用 any 检查
  52. 'vue/multi-word-component-names': 'off', // 禁用对 Vue 组件名称的多词要求检查
  53. 'no-multiple-empty-lines': ['warn', { max: 1 }], // 不允许多个空行
  54. 'no-unexpected-multiline': 'error' // 禁止空余的多行
  55. }
  56. },
  57. // vue 规则
  58. {
  59. files: ['**/*.vue'],
  60. languageOptions: {
  61. parserOptions: {
  62. parser: tseslint.parser,
  63. ecmaFeatures: {
  64. jsx: true // 启用 JSX 支持
  65. }
  66. }
  67. }
  68. },
  69. // 忽略文件
  70. {
  71. ignores: ['node_modules', 'dist', 'public', '.vscode/**', 'src/assets/**', 'src/utils/console.ts']
  72. },
  73. // prettier 配置
  74. eslintPluginPrettierRecommended
  75. ]