eslint.config.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /** @type {import('eslint').Linter.Config[]} */
  17. const config = [
  18. // 基础配置
  19. {
  20. files: ['**/*.{js,ts}'],
  21. ignores: [
  22. 'dist/**',
  23. 'node_modules/**',
  24. '*.config.js',
  25. '*.config.ts',
  26. 'coverage/**',
  27. ],
  28. languageOptions: {
  29. ecmaVersion: 'latest',
  30. sourceType: 'module',
  31. parser: typescriptParser,
  32. parserOptions: {
  33. project: './tsconfig.json',
  34. },
  35. globals: {
  36. ...globals.node,
  37. ...globals.es2021,
  38. },
  39. },
  40. plugins: {
  41. '@typescript-eslint': typescriptEslint,
  42. },
  43. rules: {
  44. // 基础 ESLint 规则
  45. ...js.configs.recommended.rules,
  46. // TypeScript 规则
  47. '@typescript-eslint/no-unused-vars': ['error', {
  48. argsIgnorePattern: '^_',
  49. varsIgnorePattern: '^_',
  50. caughtErrorsIgnorePattern: '^_',
  51. }],
  52. '@typescript-eslint/no-explicit-any': 'error',
  53. '@typescript-eslint/explicit-function-return-type': 'off',
  54. '@typescript-eslint/no-non-null-assertion': 'warn',
  55. // 捕获冗余的 null 检查 (来自 Epic 1 回顾)
  56. 'no-constant-binary-expression': 'error',
  57. // 捕获空 catch 块 (来自 Epic 1 回顾)
  58. 'no-empty': ['error', { allowEmptyCatch: false }],
  59. // 首选 const (来自 Epic 1 回顾)
  60. 'prefer-const': 'error',
  61. // 其他有用的规则
  62. 'no-console': 'off', // 测试工具可能需要 console.debug
  63. 'no-undef': 'off', // TypeScript 已经处理了未定义变量
  64. 'no-unused-vars': 'off', // 使用 TypeScript 的版本
  65. },
  66. },
  67. // 测试环境配置 (Vitest)
  68. {
  69. files: ['tests/**/*.test.ts', 'tests/**/*.spec.ts'],
  70. languageOptions: {
  71. globals: {
  72. ...globals.node,
  73. ...vitestGlobals,
  74. },
  75. },
  76. },
  77. ];
  78. // Vitest 全局变量
  79. const vitestGlobals = {
  80. describe: 'readonly',
  81. it: 'readonly',
  82. test: 'readonly',
  83. expect: 'readonly',
  84. beforeEach: 'readonly',
  85. afterEach: 'readonly',
  86. beforeAll: 'readonly',
  87. afterAll: 'readonly',
  88. vi: 'readonly',
  89. };
  90. export default config;