2
0

eslint.config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import js from '@eslint/js';
  2. import typescriptEslint from '@typescript-eslint/eslint-plugin';
  3. import typescriptParser from '@typescript-eslint/parser';
  4. import globals from 'globals';
  5. /**
  6. * ESLint 配置 for @d8d/e2e-test-utils
  7. *
  8. * 此配置捕获常见代码问题,包括:
  9. * - 冗余的 null 检查
  10. * - 未使用的变量
  11. * - 空 catch 块
  12. * - 首选 const 而非 let
  13. *
  14. * 参见: _bmad-output/implementation-artifacts/epic-1-retrospective.md
  15. */
  16. // Vitest 全局变量
  17. const vitestGlobals = {
  18. describe: 'readonly',
  19. it: 'readonly',
  20. test: 'readonly',
  21. expect: 'readonly',
  22. beforeEach: 'readonly',
  23. afterEach: 'readonly',
  24. beforeAll: 'readonly',
  25. afterAll: 'readonly',
  26. vi: 'readonly',
  27. };
  28. /** @type {import('eslint').Linter.Config[]} */
  29. const config = [
  30. // 全局忽略
  31. {
  32. ignores: [
  33. 'dist/**',
  34. 'node_modules/**',
  35. '*.config.js',
  36. '*.config.ts',
  37. 'coverage/**',
  38. ],
  39. },
  40. // 基础配置
  41. {
  42. files: ['src/**/*.ts'],
  43. ignores: [
  44. 'dist/**',
  45. 'node_modules/**',
  46. '*.config.js',
  47. '*.config.ts',
  48. 'coverage/**',
  49. ],
  50. languageOptions: {
  51. ecmaVersion: 'latest',
  52. sourceType: 'module',
  53. parser: typescriptParser,
  54. parserOptions: {
  55. project: './tsconfig.json',
  56. },
  57. globals: {
  58. ...globals.node,
  59. ...globals.es2021,
  60. },
  61. },
  62. plugins: {
  63. '@typescript-eslint': typescriptEslint,
  64. },
  65. rules: {
  66. // 基础 ESLint 规则
  67. ...js.configs.recommended.rules,
  68. // TypeScript 规则
  69. '@typescript-eslint/no-unused-vars': ['error', {
  70. argsIgnorePattern: '^_',
  71. varsIgnorePattern: '^_',
  72. caughtErrorsIgnorePattern: '^_',
  73. }],
  74. '@typescript-eslint/no-explicit-any': 'error',
  75. '@typescript-eslint/explicit-function-return-type': 'off',
  76. '@typescript-eslint/no-non-null-assertion': 'warn',
  77. // 捕获冗余的 null 检查 (来自 Epic 1 回顾)
  78. 'no-constant-binary-expression': 'error',
  79. // 捕获空 catch 块 (来自 Epic 1 回顾)
  80. 'no-empty': ['error', { allowEmptyCatch: false }],
  81. // 首选 const (来自 Epic 1 回顾)
  82. 'prefer-const': 'error',
  83. // 其他有用的规则
  84. 'no-console': 'off', // 测试工具可能需要 console.debug
  85. 'no-undef': 'off', // TypeScript 已经处理了未定义变量
  86. 'no-unused-vars': 'off', // 使用 TypeScript 的版本
  87. },
  88. },
  89. // 测试环境配置 (Vitest) - 不使用 tsconfig project
  90. {
  91. files: ['tests/**/*.test.ts', 'tests/**/*.spec.ts'],
  92. ignores: ['coverage/**'],
  93. languageOptions: {
  94. ecmaVersion: 'latest',
  95. sourceType: 'module',
  96. parser: typescriptParser,
  97. globals: {
  98. ...globals.node,
  99. ...vitestGlobals,
  100. },
  101. },
  102. rules: {
  103. '@typescript-eslint/no-explicit-any': 'off',
  104. 'no-console': 'off',
  105. },
  106. },
  107. ];
  108. export default config;