import js from '@eslint/js'; import typescriptEslint from '@typescript-eslint/eslint-plugin'; import typescriptParser from '@typescript-eslint/parser'; import reactPlugin from 'eslint-plugin-react'; import reactHooks from 'eslint-plugin-react-hooks'; import globals from 'globals'; // Playwright 全局变量 const playwrightGlobals = { test: 'readonly', expect: 'readonly', describe: 'readonly', beforeAll: 'readonly', afterAll: 'readonly', beforeEach: 'readonly', afterEach: 'readonly', page: 'readonly', browser: 'readonly', context: 'readonly', request: 'readonly', }; export default [ // 基础配置 { files: ['**/*.{js,jsx,ts,tsx}'], ignores: [ 'dist/**', 'node_modules/**', '*.config.js', '*.config.ts', 'scripts/**', 'server.js', 'vitest.config.components.ts', 'coverage/**', ], languageOptions: { ecmaVersion: 'latest', sourceType: 'module', parser: typescriptParser, parserOptions: { ecmaFeatures: { jsx: true, }, }, globals: { ...globals.browser, ...globals.es2021, RequestInfo: 'readonly', RequestInit: 'readonly', URL: 'readonly', Response: 'readonly', }, }, plugins: { '@typescript-eslint': typescriptEslint, react: reactPlugin, 'react-hooks': reactHooks, }, rules: { // 基础ESLint规则 ...js.configs.recommended.rules, // TypeScript规则 '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_', }], '@typescript-eslint/no-explicit-any': 'warn', '@typescript-eslint/explicit-function-return-type': 'off', // React规则 'react/react-in-jsx-scope': 'off', 'react/prop-types': 'off', // 通用规则 'no-console': 'warn', 'prefer-const': 'error', 'no-undef': 'off', // TypeScript已经处理了未定义变量 'no-unused-vars': 'off', // 使用TypeScript的版本 }, settings: { react: { version: 'detect', }, }, }, // Node.js环境配置 { files: ['src/server/**/*.{js,ts}', 'src/test/**/*.{js,ts}'], languageOptions: { globals: { ...globals.node, }, }, }, // 测试环境配置 { files: ['src/**/__tests__/**/*.{js,ts,jsx,tsx}', 'src/**/__integration_tests__/**/*.{js,ts,jsx,tsx}'], languageOptions: { globals: { ...globals.jest, vi: 'readonly', }, }, }, // E2E 测试环境配置 (Playwright) // 来自 Epic 2-3 回顾的 ESLint 配置 // 捕获常见问题:console.log、硬编码超时、未使用变量等 { files: ['tests/e2e/**/*.{js,ts,jsx,tsx}'], languageOptions: { ecmaVersion: 'latest', sourceType: 'module', parser: typescriptParser, globals: { ...globals.node, ...playwrightGlobals, }, }, rules: { // TypeScript 规则 '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_', }], '@typescript-eslint/no-explicit-any': 'warn', '@typescript-eslint/explicit-function-return-type': 'off', // 捕获冗余的 null 检查 (来自 Epic 1 回顾) 'no-constant-binary-expression': 'error', // 捕获空 catch 块 (来自 Epic 1 回顾) 'no-empty': ['error', { allowEmptyCatch: false }], // 首选 const (来自 Epic 1 回顾) 'prefer-const': 'error', // 允许 console.debug 和 console.warn,但不允许 console.log 'no-console': ['error', { allow: ['debug', 'warn', 'error'] }], // 捕获硬编码超时 (Epic 3 回顾) // 注意:page.waitForTimeout 在 Playwright 中有时是必要的 // 这里只是警告,鼓励使用 Playwright 的 auto-waiting 'no-restricted-globals': ['warn', { name: 'setTimeout', message: '避免使用 setTimeout,优先使用 Playwright 的 auto-waiting 机制', }], // 其他基础规则 'no-undef': 'off', 'no-unused-vars': 'off', }, }, ];