|
| 1 | +// 解决 ts 类型warning |
| 2 | +import type { Linter } from 'eslint'; |
| 3 | +// ts 规则 |
| 4 | +import typescriptEslint from '@typescript-eslint/eslint-plugin'; |
| 5 | +// ts 解析器 |
| 6 | +import tsParser from '@typescript-eslint/parser'; |
| 7 | +import importPlugin from 'eslint-plugin-import'; |
| 8 | +// 解决 eslint 和 prettier 的共存问题 |
| 9 | +import prettier from '@vue/eslint-config-prettier'; |
| 10 | +import vue from 'eslint-plugin-vue'; |
| 11 | +// .vue 解析器 |
| 12 | +import vueParser from 'vue-eslint-parser'; |
| 13 | +// airbnb 规则 |
| 14 | +import { configs as airbnbConfigs } from 'eslint-config-airbnb-extended/legacy'; |
| 15 | +import path from 'node:path'; |
| 16 | + |
| 17 | +export default [ |
| 18 | + // 1. 忽略文件配置 |
| 19 | + { |
| 20 | + ignores: [ |
| 21 | + // 从 .eslintignore 迁移的配置 |
| 22 | + '/*.json', |
| 23 | + '/*.js', |
| 24 | + '**/dist/**', |
| 25 | + '**/node_modules/**', |
| 26 | + // 其他忽略配置 |
| 27 | + '**/dist-ssr/**', |
| 28 | + '**/coverage/**', |
| 29 | + '**/build/**', |
| 30 | + ], |
| 31 | + }, |
| 32 | + // 2. TypeScript 推荐配置 |
| 33 | + { |
| 34 | + files: ['**/*.{ts,tsx,vue}'], |
| 35 | + languageOptions: { |
| 36 | + parser: vueParser, |
| 37 | + parserOptions: { |
| 38 | + parser: tsParser, |
| 39 | + sourceType: 'module', |
| 40 | + ecmaVersion: 2020, |
| 41 | + ecmaFeatures: { |
| 42 | + jsx: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + rules: { |
| 47 | + ...typescriptEslint.configs.recommended.rules, |
| 48 | + }, |
| 49 | + }, |
| 50 | + |
| 51 | + // 3. Vue 3 推荐配置 (基础配置先加载) |
| 52 | + ...vue.configs['flat/recommended'], |
| 53 | + |
| 54 | + // 4. Airbnb 基础配置 |
| 55 | + // 下面的抽象操作时让规则对 vue 文件生效 |
| 56 | + // 原谅我的炫技,不过确实很简洁 |
| 57 | + ...airbnbConfigs.base.recommended.map((rule) => ({ |
| 58 | + ...rule, |
| 59 | + files: rule.files.some((s) => s.includes('ts')) |
| 60 | + ? [...rule.files, '**/*.vue'] |
| 61 | + : rule.files, |
| 62 | + })), |
| 63 | + |
| 64 | + // 5. 自定义配置和规则覆盖 (最后加载以覆盖之前的规则) |
| 65 | + { |
| 66 | + files: ['**/*.{js,mjs,cjs,ts,tsx,vue}'], |
| 67 | + plugins: { |
| 68 | + '@typescript-eslint': typescriptEslint, |
| 69 | + 'import': importPlugin, |
| 70 | + vue, |
| 71 | + }, |
| 72 | + languageOptions: { |
| 73 | + parser: vueParser, |
| 74 | + parserOptions: { |
| 75 | + parser: tsParser, |
| 76 | + sourceType: 'module', |
| 77 | + ecmaVersion: 2020, |
| 78 | + ecmaFeatures: { |
| 79 | + jsx: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + globals: { |
| 83 | + // browser |
| 84 | + window: 'readonly', |
| 85 | + document: 'readonly', |
| 86 | + navigator: 'readonly', |
| 87 | + console: 'readonly', |
| 88 | + // node |
| 89 | + process: 'readonly', |
| 90 | + __dirname: 'readonly', |
| 91 | + __filename: 'readonly', |
| 92 | + module: 'readonly', |
| 93 | + require: 'readonly', |
| 94 | + // vue setup compiler macros |
| 95 | + defineProps: 'readonly', |
| 96 | + defineEmits: 'readonly', |
| 97 | + defineExpose: 'readonly', |
| 98 | + withDefaults: 'readonly', |
| 99 | + defineModel: 'readonly', |
| 100 | + }, |
| 101 | + }, |
| 102 | + settings: { |
| 103 | + 'import/resolver': { |
| 104 | + typescript: { |
| 105 | + project: path.resolve(__dirname, './tsconfig.json'), |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + rules: { |
| 110 | + // 基础规则 |
| 111 | + 'no-console': 1, |
| 112 | + 'no-debugger': 0, // 开发环境可以使用,生产环境需要启用 |
| 113 | + 'no-param-reassign': 0, |
| 114 | + 'no-nested-ternary': 0, |
| 115 | + 'prefer-regex-literals': 0, |
| 116 | + 'camelcase': 0, |
| 117 | + 'no-undef': 0, // 临时解决 defineModel 问题 |
| 118 | + |
| 119 | + // Vue 规则覆盖 |
| 120 | + 'vue/require-default-prop': 0, |
| 121 | + 'vue/singleline-html-element-content-newline': 0, |
| 122 | + 'vue/max-attributes-per-line': 0, |
| 123 | + 'vue/multi-word-component-names': 0, |
| 124 | + 'vue/no-useless-template-attributes': 0, |
| 125 | + 'vue/custom-event-name-casing': [2, 'camelCase'], |
| 126 | + 'vue/no-v-text': 1, |
| 127 | + 'vue/padding-line-between-blocks': 1, |
| 128 | + 'vue/require-direct-export': 1, |
| 129 | + |
| 130 | + // TypeScript 规则覆盖 |
| 131 | + '@typescript-eslint/ban-ts-comment': 0, // 允许 @ts-ignore |
| 132 | + // 解决莫名其妙的,对于 函数参数未使用 的报错 |
| 133 | + 'no-unused-vars': 'off', |
| 134 | + '@typescript-eslint/no-unused-vars': 1, |
| 135 | + '@typescript-eslint/no-empty-function': 1, |
| 136 | + '@typescript-eslint/no-explicit-any': 0, |
| 137 | + 'no-shadow': 0, // 避免 enum 的循环引用 |
| 138 | + '@typescript-eslint/no-shadow': 2, |
| 139 | + |
| 140 | + // Import 规则 |
| 141 | + 'import/extensions': [ |
| 142 | + 2, |
| 143 | + 'ignorePackages', |
| 144 | + { |
| 145 | + js: 'never', |
| 146 | + jsx: 'never', |
| 147 | + ts: 'never', |
| 148 | + tsx: 'never', |
| 149 | + }, |
| 150 | + ], |
| 151 | + 'import/no-extraneous-dependencies': 0, |
| 152 | + 'import/prefer-default-export': 0, |
| 153 | + }, |
| 154 | + }, |
| 155 | + // Prettier 配置 (必须放在最后以禁用所有格式化相关的 ESLint 规则) |
| 156 | + prettier, |
| 157 | +] as Linter.Config[]; |
0 commit comments