eslint.config.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // ESLint 9 flat config(monorepo 根,遍历 packages/ + apps/):vue3 + TS strict 配套。
  2. //
  3. // 与 PC 端(单项目)差异:
  4. // ① ignores 补 monorepo 路径(apps/*/dist、packages/shared/dist、各 node_modules、vendor/);
  5. // ② 禁值 import 清单**只含 UMD 全局框架**(vue/vue-router/pinia/axios)——
  6. // Vant 4 走 npm 编译(官方无 UMD 构建,混合方案),`import Vant from 'vant'` 仅允许
  7. // 出现在 apps/*/src/main.ts 的 app.use(Vant) 装配点,不在本清单限内。
  8. import js from '@eslint/js';
  9. import tseslint from 'typescript-eslint';
  10. import pluginVue from 'eslint-plugin-vue';
  11. import prettier from 'eslint-config-prettier';
  12. /**
  13. * 框架运行时模块名(UMD 全局框架;业务代码禁"值 import",见 README 编码规范)。
  14. *
  15. * 精确边界(同 PC 端):
  16. * - `import type { Ref } from 'vue'` 属**类型导入**(编译期擦除、零运行时足迹)——放行;
  17. * - `import { ref } from 'vue'` 属**值导入**——会把框架运行时打包进 dist,必须拦。
  18. *
  19. * 实现用 `no-restricted-syntax`(importKind='value' 只匹配值导入,放行 type 导入)。
  20. */
  21. const FRAMEWORK_MODULE_SELECTOR =
  22. ":is([source.value='vue'],[source.value='vue-router'],[source.value='pinia'],[source.value='axios'])";
  23. export default [
  24. js.configs.recommended,
  25. ...tseslint.configs.recommended,
  26. ...pluginVue.configs['flat/essential'],
  27. prettier,
  28. {
  29. // shims/ 由 scripts/gen-shims.cjs 生成(含逐行 eslint-disable 注释);
  30. // scripts/ 是 Node CJS 工具脚本;vendor/ 是 UMD 源文件——均不 lint。
  31. ignores: [
  32. 'dist/**',
  33. 'vendor/**',
  34. 'node_modules/**',
  35. 'shims/**',
  36. 'scripts/**',
  37. 'packages/*/dist/**',
  38. 'apps/*/dist/**',
  39. '**/node_modules/**',
  40. '**/*.cjs',
  41. ],
  42. },
  43. {
  44. // .vue:SFC 内 <script lang="ts"> 交给 @typescript-eslint/parser,
  45. // 否则 TS 语法(类型注解 / interface / 泛型)在 SFC 内触发 Parsing error。
  46. files: ['**/*.vue'],
  47. languageOptions: {
  48. parserOptions: {
  49. parser: tseslint.parser,
  50. sourceType: 'module',
  51. },
  52. },
  53. },
  54. {
  55. // TS 项目里 no-undef 由 TS 编译器负责(global.d.ts 声明的裸标识符对 JS 规则不可见,会误报)。
  56. files: ['**/*.ts', '**/*.vue', '**/*.tsx'],
  57. rules: {
  58. 'no-undef': 'off',
  59. },
  60. },
  61. {
  62. rules: {
  63. '@typescript-eslint/no-explicit-any': 'error',
  64. 'vue/multi-word-component-names': 'off',
  65. '@typescript-eslint/no-unused-vars': [
  66. 'error',
  67. { argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
  68. ],
  69. // 拦截"值 import UMD 全局框架"(值导入会把框架运行时打包进 dist)。
  70. // 放行 import type(编译期擦除)。
  71. 'no-restricted-syntax': [
  72. 'error',
  73. {
  74. selector: `ImportDeclaration[importKind='value']${FRAMEWORK_MODULE_SELECTOR}`,
  75. message:
  76. 'UMD 全局框架经 index.html 加载(vendor/ 自托管),业务代码禁止值 import;仅允许 `import type`(编译期擦除,零运行时足迹)。',
  77. },
  78. ],
  79. },
  80. },
  81. ];