| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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',
- },
- },
- ];
|