eslint.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 'node_modules/**',
  27. '*/node_modules/**',
  28. 'dist/**',
  29. '*/dist/**',
  30. '*.config.js',
  31. '*.config.ts',
  32. 'scripts/**',
  33. 'coverage/**',
  34. '*/coverage/**',
  35. 'build/**',
  36. '*/build/**',
  37. ],
  38. languageOptions: {
  39. ecmaVersion: 'latest',
  40. sourceType: 'module',
  41. parser: typescriptParser,
  42. parserOptions: {
  43. ecmaFeatures: {
  44. jsx: true,
  45. },
  46. },
  47. globals: {
  48. ...globals.browser,
  49. ...globals.es2021,
  50. RequestInfo: 'readonly',
  51. RequestInit: 'readonly',
  52. URL: 'readonly',
  53. Response: 'readonly',
  54. },
  55. },
  56. plugins: {
  57. '@typescript-eslint': typescriptEslint,
  58. react: reactPlugin,
  59. 'react-hooks': reactHooks,
  60. },
  61. rules: {
  62. // 基础ESLint规则
  63. ...js.configs.recommended.rules,
  64. // TypeScript规则
  65. '@typescript-eslint/no-unused-vars': ['error', {
  66. argsIgnorePattern: '^_',
  67. varsIgnorePattern: '^_',
  68. caughtErrorsIgnorePattern: '^_',
  69. }],
  70. '@typescript-eslint/no-explicit-any': 'warn',
  71. '@typescript-eslint/explicit-function-return-type': 'off',
  72. // React规则
  73. 'react/react-in-jsx-scope': 'off',
  74. 'react/prop-types': 'off',
  75. // 通用规则
  76. 'no-console': 'warn',
  77. 'prefer-const': 'error',
  78. 'no-undef': 'off', // TypeScript已经处理了未定义变量
  79. 'no-unused-vars': 'off', // 使用TypeScript的版本
  80. },
  81. settings: {
  82. react: {
  83. version: 'detect',
  84. },
  85. },
  86. },
  87. // Node.js环境配置
  88. {
  89. files: ['**/src/server/**/*.{js,ts}', '**/src/test/**/*.{js,ts}'],
  90. languageOptions: {
  91. globals: {
  92. ...globals.node,
  93. },
  94. },
  95. },
  96. // 测试环境配置
  97. {
  98. files: ['**/src/**/__tests__/**/*.{js,ts,jsx,tsx}', '**/src/**/__integration_tests__/**/*.{js,ts,jsx,tsx}'],
  99. languageOptions: {
  100. globals: {
  101. ...globals.jest,
  102. vi: 'readonly',
  103. },
  104. },
  105. },
  106. // E2E 测试环境配置 (Playwright)
  107. {
  108. files: ['**/tests/e2e/**/*.{js,ts,jsx,tsx}'],
  109. languageOptions: {
  110. ecmaVersion: 'latest',
  111. sourceType: 'module',
  112. parser: typescriptParser,
  113. globals: {
  114. ...globals.node,
  115. ...playwrightGlobals,
  116. },
  117. },
  118. rules: {
  119. // TypeScript 规则
  120. '@typescript-eslint/no-unused-vars': ['error', {
  121. argsIgnorePattern: '^_',
  122. varsIgnorePattern: '^_',
  123. caughtErrorsIgnorePattern: '^_',
  124. }],
  125. '@typescript-eslint/no-explicit-any': 'warn',
  126. '@typescript-eslint/explicit-function-return-type': 'off',
  127. // 捕获冗余的 null 检查
  128. 'no-constant-binary-expression': 'error',
  129. // 捕获空 catch 块
  130. 'no-empty': ['error', { allowEmptyCatch: false }],
  131. // 首选 const
  132. 'prefer-const': 'error',
  133. // 允许 console.debug 和 console.warn,但不允许 console.log
  134. 'no-console': ['error', { allow: ['debug', 'warn', 'error'] }],
  135. // 其他基础规则
  136. 'no-undef': 'off',
  137. 'no-unused-vars': 'off',
  138. },
  139. },
  140. ];