|
| 1 | +/** |
| 2 | + * @description ESLint 配置文件 - 现代化配置 |
| 3 | + * @author 优化版本 |
| 4 | + */ |
| 5 | +import js from '@eslint/js'; |
| 6 | +import typescript from '@typescript-eslint/eslint-plugin'; |
| 7 | +import typescriptParser from '@typescript-eslint/parser'; |
| 8 | +import vue from 'eslint-plugin-vue'; |
| 9 | +import prettier from 'eslint-plugin-prettier'; |
| 10 | +import vueParser from 'vue-eslint-parser'; |
| 11 | + |
| 12 | +export default [ |
| 13 | + // 基础 JavaScript 配置 |
| 14 | + js.configs.recommended, |
| 15 | + |
| 16 | + // 全局忽略配置 |
| 17 | + { |
| 18 | + ignores: [ |
| 19 | + 'dist/**', |
| 20 | + 'node_modules/**', |
| 21 | + '*.d.ts', |
| 22 | + 'public/**', |
| 23 | + '.vscode/**', |
| 24 | + '.idea/**', |
| 25 | + 'build/**', |
| 26 | + 'mock/**', |
| 27 | + 'packages/**/dist/**', |
| 28 | + 'packages/**/node_modules/**', |
| 29 | + '.eslintrc-auto-import.json' |
| 30 | + ] |
| 31 | + }, |
| 32 | + |
| 33 | + // Vue 文件配置 |
| 34 | + { |
| 35 | + files: ['**/*.vue'], |
| 36 | + languageOptions: { |
| 37 | + parser: vueParser, |
| 38 | + parserOptions: { |
| 39 | + parser: typescriptParser, |
| 40 | + ecmaVersion: 'latest', |
| 41 | + sourceType: 'module', |
| 42 | + extraFileExtensions: ['.vue'], |
| 43 | + project: './tsconfig.json' |
| 44 | + }, |
| 45 | + globals: { |
| 46 | + // Vue 3 全局 API |
| 47 | + defineProps: 'readonly', |
| 48 | + defineEmits: 'readonly', |
| 49 | + defineExpose: 'readonly', |
| 50 | + withDefaults: 'readonly', |
| 51 | + // 自动导入的全局变量 |
| 52 | + ref: 'readonly', |
| 53 | + reactive: 'readonly', |
| 54 | + computed: 'readonly', |
| 55 | + watch: 'readonly', |
| 56 | + watchEffect: 'readonly', |
| 57 | + onMounted: 'readonly', |
| 58 | + onUnmounted: 'readonly', |
| 59 | + nextTick: 'readonly' |
| 60 | + } |
| 61 | + }, |
| 62 | + plugins: { |
| 63 | + vue, |
| 64 | + '@typescript-eslint': typescript, |
| 65 | + prettier |
| 66 | + }, |
| 67 | + rules: { |
| 68 | + // Vue 3 推荐规则 |
| 69 | + ...vue.configs['vue3-recommended'].rules, |
| 70 | + |
| 71 | + // Vue 规则自定义 |
| 72 | + 'vue/multi-word-component-names': 'off', // 允许单词组件名 |
| 73 | + 'vue/no-v-html': 'off', // 允许 v-html |
| 74 | + 'vue/require-default-prop': 'off', // 不强制默认属性 |
| 75 | + 'vue/require-explicit-emits': 'off', // 不强制显式 emits |
| 76 | + 'vue/component-tags-order': ['error', { |
| 77 | + order: ['template', 'script', 'style'] |
| 78 | + }], |
| 79 | + 'vue/html-self-closing': ['error', { |
| 80 | + html: { |
| 81 | + void: 'always', |
| 82 | + normal: 'never', |
| 83 | + component: 'always' |
| 84 | + }, |
| 85 | + svg: 'always', |
| 86 | + math: 'always' |
| 87 | + }], |
| 88 | + 'vue/max-attributes-per-line': ['error', { |
| 89 | + singleline: { max: 3 }, |
| 90 | + multiline: { max: 1 } |
| 91 | + }], |
| 92 | + 'vue/html-indent': ['error', 2], |
| 93 | + 'vue/script-indent': ['error', 2, { baseIndent: 0 }], |
| 94 | + |
| 95 | + // TypeScript 规则 |
| 96 | + '@typescript-eslint/no-unused-vars': ['warn', { |
| 97 | + argsIgnorePattern: '^_', |
| 98 | + varsIgnorePattern: '^_' |
| 99 | + }], |
| 100 | + '@typescript-eslint/no-explicit-any': 'warn', |
| 101 | + '@typescript-eslint/explicit-function-return-type': 'off', |
| 102 | + '@typescript-eslint/explicit-module-boundary-types': 'off', |
| 103 | + '@typescript-eslint/no-empty-function': 'off', |
| 104 | + '@typescript-eslint/no-non-null-assertion': 'warn' |
| 105 | + } |
| 106 | + }, |
| 107 | + |
| 108 | + // TypeScript 文件配置 |
| 109 | + { |
| 110 | + files: ['**/*.ts', '**/*.tsx'], |
| 111 | + languageOptions: { |
| 112 | + parser: typescriptParser, |
| 113 | + parserOptions: { |
| 114 | + ecmaVersion: 'latest', |
| 115 | + sourceType: 'module', |
| 116 | + project: './tsconfig.json' |
| 117 | + } |
| 118 | + }, |
| 119 | + plugins: { |
| 120 | + '@typescript-eslint': typescript, |
| 121 | + prettier |
| 122 | + }, |
| 123 | + rules: { |
| 124 | + // TypeScript 推荐规则 |
| 125 | + ...typescript.configs.recommended.rules, |
| 126 | + |
| 127 | + // TypeScript 规则自定义 |
| 128 | + '@typescript-eslint/no-unused-vars': ['warn', { |
| 129 | + argsIgnorePattern: '^_', |
| 130 | + varsIgnorePattern: '^_', |
| 131 | + caughtErrorsIgnorePattern: '^_' |
| 132 | + }], |
| 133 | + '@typescript-eslint/no-explicit-any': 'warn', |
| 134 | + '@typescript-eslint/explicit-function-return-type': 'off', |
| 135 | + '@typescript-eslint/explicit-module-boundary-types': 'off', |
| 136 | + '@typescript-eslint/no-empty-function': 'off', |
| 137 | + '@typescript-eslint/no-non-null-assertion': 'warn', |
| 138 | + '@typescript-eslint/ban-ts-comment': ['error', { |
| 139 | + 'ts-expect-error': 'allow-with-description', |
| 140 | + 'ts-ignore': 'allow-with-description', |
| 141 | + 'ts-nocheck': 'allow-with-description' |
| 142 | + }], |
| 143 | + '@typescript-eslint/consistent-type-imports': ['error', { |
| 144 | + prefer: 'type-imports', |
| 145 | + disallowTypeAnnotations: false |
| 146 | + }] |
| 147 | + } |
| 148 | + }, |
| 149 | + |
| 150 | + // JavaScript 文件配置 |
| 151 | + { |
| 152 | + files: ['**/*.js', '**/*.jsx'], |
| 153 | + languageOptions: { |
| 154 | + ecmaVersion: 'latest', |
| 155 | + sourceType: 'module', |
| 156 | + globals: { |
| 157 | + process: 'readonly', |
| 158 | + Buffer: 'readonly', |
| 159 | + __dirname: 'readonly', |
| 160 | + __filename: 'readonly', |
| 161 | + module: 'readonly', |
| 162 | + require: 'readonly', |
| 163 | + exports: 'readonly', |
| 164 | + global: 'readonly' |
| 165 | + } |
| 166 | + }, |
| 167 | + plugins: { |
| 168 | + prettier |
| 169 | + }, |
| 170 | + rules: { |
| 171 | + 'no-unused-vars': ['warn', { |
| 172 | + argsIgnorePattern: '^_', |
| 173 | + varsIgnorePattern: '^_' |
| 174 | + }] |
| 175 | + } |
| 176 | + }, |
| 177 | + |
| 178 | + // 全局规则配置 |
| 179 | + { |
| 180 | + files: ['**/*.{js,ts,jsx,tsx,vue}'], |
| 181 | + plugins: { |
| 182 | + prettier |
| 183 | + }, |
| 184 | + rules: { |
| 185 | + // Prettier 集成 |
| 186 | + 'prettier/prettier': 'error', |
| 187 | + |
| 188 | + // 基础规则 |
| 189 | + 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', |
| 190 | + 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', |
| 191 | + 'prefer-const': 'error', |
| 192 | + 'no-var': 'error', |
| 193 | + 'object-shorthand': 'error', |
| 194 | + 'prefer-template': 'error', |
| 195 | + 'no-duplicate-imports': 'error', |
| 196 | + 'no-useless-rename': 'error', |
| 197 | + 'prefer-destructuring': ['error', { |
| 198 | + VariableDeclarator: { array: false, object: true }, |
| 199 | + AssignmentExpression: { array: false, object: false } |
| 200 | + }], |
| 201 | + |
| 202 | + // 代码质量规则 |
| 203 | + 'eqeqeq': ['error', 'always'], |
| 204 | + 'no-eval': 'error', |
| 205 | + 'no-implied-eval': 'error', |
| 206 | + 'no-new-func': 'error', |
| 207 | + 'no-script-url': 'error', |
| 208 | + 'no-self-compare': 'error', |
| 209 | + 'no-sequences': 'error', |
| 210 | + 'no-throw-literal': 'error', |
| 211 | + 'no-unmodified-loop-condition': 'error', |
| 212 | + 'no-unused-expressions': ['error', { |
| 213 | + allowShortCircuit: true, |
| 214 | + allowTernary: true |
| 215 | + }], |
| 216 | + 'no-useless-call': 'error', |
| 217 | + 'no-useless-concat': 'error', |
| 218 | + 'no-useless-return': 'error', |
| 219 | + 'prefer-promise-reject-errors': 'error' |
| 220 | + } |
| 221 | + }, |
| 222 | + |
| 223 | + // 配置文件特殊规则 |
| 224 | + { |
| 225 | + files: [ |
| 226 | + '*.config.{js,ts}', |
| 227 | + '**/.eslintrc.{js,ts}', |
| 228 | + '**/build/**/*.{js,ts}', |
| 229 | + '**/scripts/**/*.{js,ts}', |
| 230 | + 'vite.config.*', |
| 231 | + 'vitest.config.*', |
| 232 | + 'jest.config.*', |
| 233 | + 'postcss.config.*', |
| 234 | + 'tailwind.config.*', |
| 235 | + 'unocss.config.*' |
| 236 | + ], |
| 237 | + rules: { |
| 238 | + 'no-console': 'off', |
| 239 | + '@typescript-eslint/no-var-requires': 'off', |
| 240 | + '@typescript-eslint/no-require-imports': 'off' |
| 241 | + } |
| 242 | + }, |
| 243 | + |
| 244 | + // 测试文件特殊规则 |
| 245 | + { |
| 246 | + files: [ |
| 247 | + '**/*.test.{js,ts,tsx}', |
| 248 | + '**/*.spec.{js,ts,tsx}', |
| 249 | + '**/tests/**/*.{js,ts,tsx}', |
| 250 | + '**/test/**/*.{js,ts,tsx}' |
| 251 | + ], |
| 252 | + rules: { |
| 253 | + '@typescript-eslint/no-explicit-any': 'off', |
| 254 | + '@typescript-eslint/no-non-null-assertion': 'off', |
| 255 | + 'no-console': 'off' |
| 256 | + } |
| 257 | + } |
| 258 | +]; |
0 commit comments