eslint.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import js from '@eslint/js';
  2. import typescriptEslint from '@typescript-eslint/eslint-plugin';
  3. import typescriptParser from '@typescript-eslint/parser';
  4. import reactPlugin from 'eslint-plugin-react';
  5. import reactHooks from 'eslint-plugin-react-hooks';
  6. import globals from 'globals';
  7. // Playwright 全局变量
  8. const playwrightGlobals = {
  9. test: 'readonly',
  10. expect: 'readonly',
  11. describe: 'readonly',
  12. beforeAll: 'readonly',
  13. afterAll: 'readonly',
  14. beforeEach: 'readonly',
  15. afterEach: 'readonly',
  16. page: 'readonly',
  17. browser: 'readonly',
  18. context: 'readonly',
  19. request: 'readonly',
  20. };
  21. export default [
  22. // 基础配置
  23. {
  24. files: ['**/*.{js,jsx,ts,tsx}'],
  25. ignores: [
  26. 'dist/**',
  27. 'node_modules/**',
  28. '*.config.js',
  29. '*.config.ts',
  30. 'scripts/**',
  31. 'server.js',
  32. 'vitest.config.components.ts',
  33. 'coverage/**',
  34. ],
  35. languageOptions: {
  36. ecmaVersion: 'latest',
  37. sourceType: 'module',
  38. parser: typescriptParser,
  39. parserOptions: {
  40. ecmaFeatures: {
  41. jsx: true,
  42. },
  43. },
  44. globals: {
  45. ...globals.browser,
  46. ...globals.es2021,
  47. RequestInfo: 'readonly',
  48. RequestInit: 'readonly',
  49. URL: 'readonly',
  50. Response: 'readonly',
  51. },
  52. },
  53. plugins: {
  54. '@typescript-eslint': typescriptEslint,
  55. react: reactPlugin,
  56. 'react-hooks': reactHooks,
  57. },
  58. rules: {
  59. // 基础ESLint规则
  60. ...js.configs.recommended.rules,
  61. // TypeScript规则
  62. '@typescript-eslint/no-unused-vars': ['error', {
  63. argsIgnorePattern: '^_',
  64. varsIgnorePattern: '^_',
  65. caughtErrorsIgnorePattern: '^_',
  66. }],
  67. '@typescript-eslint/no-explicit-any': 'warn',
  68. '@typescript-eslint/explicit-function-return-type': 'off',
  69. // React规则
  70. 'react/react-in-jsx-scope': 'off',
  71. 'react/prop-types': 'off',
  72. // 通用规则
  73. 'no-console': 'warn',
  74. 'prefer-const': 'error',
  75. 'no-undef': 'off', // TypeScript已经处理了未定义变量
  76. 'no-unused-vars': 'off', // 使用TypeScript的版本
  77. },
  78. settings: {
  79. react: {
  80. version: 'detect',
  81. },
  82. },
  83. },
  84. // Node.js环境配置
  85. {
  86. files: ['src/server/**/*.{js,ts}', 'src/test/**/*.{js,ts}'],
  87. languageOptions: {
  88. globals: {
  89. ...globals.node,
  90. },
  91. },
  92. },
  93. // 测试环境配置
  94. {
  95. files: ['src/**/__tests__/**/*.{js,ts,jsx,tsx}', 'src/**/__integration_tests__/**/*.{js,ts,jsx,tsx}'],
  96. languageOptions: {
  97. globals: {
  98. ...globals.jest,
  99. vi: 'readonly',
  100. },
  101. },
  102. },
  103. // E2E 测试环境配置 (Playwright)
  104. // 来自 Epic 2-3 回顾的 ESLint 配置
  105. // 捕获常见问题:console.log、硬编码超时、未使用变量等
  106. {
  107. files: ['tests/e2e/**/*.{js,ts,jsx,tsx}'],
  108. languageOptions: {
  109. ecmaVersion: 'latest',
  110. sourceType: 'module',
  111. parser: typescriptParser,
  112. globals: {
  113. ...globals.node,
  114. ...playwrightGlobals,
  115. },
  116. },
  117. rules: {
  118. // TypeScript 规则
  119. '@typescript-eslint/no-unused-vars': ['error', {
  120. argsIgnorePattern: '^_',
  121. varsIgnorePattern: '^_',
  122. caughtErrorsIgnorePattern: '^_',
  123. }],
  124. '@typescript-eslint/no-explicit-any': 'warn',
  125. '@typescript-eslint/explicit-function-return-type': 'off',
  126. // 捕获冗余的 null 检查 (来自 Epic 1 回顾)
  127. 'no-constant-binary-expression': 'error',
  128. // 捕获空 catch 块 (来自 Epic 1 回顾)
  129. 'no-empty': ['error', { allowEmptyCatch: false }],
  130. // 首选 const (来自 Epic 1 回顾)
  131. 'prefer-const': 'error',
  132. // 允许 console.debug 和 console.warn,但不允许 console.log
  133. 'no-console': ['error', { allow: ['debug', 'warn', 'error'] }],
  134. // 捕获硬编码超时 (Epic 3 回顾)
  135. // 注意:page.waitForTimeout 在 Playwright 中有时是必要的
  136. // 这里只是警告,鼓励使用 Playwright 的 auto-waiting
  137. 'no-restricted-globals': ['warn', {
  138. name: 'setTimeout',
  139. message: '避免使用 setTimeout,优先使用 Playwright 的 auto-waiting 机制',
  140. }],
  141. // 其他基础规则
  142. 'no-undef': 'off',
  143. 'no-unused-vars': 'off',
  144. },
  145. },
  146. ];