Skip to content

Commit c9e3889

Browse files
authored
Merge pull request #87 from dsgler/eslint_update
feat: eslint 更新
2 parents a482c3a + b847836 commit c9e3889

File tree

10 files changed

+637
-531
lines changed

10 files changed

+637
-531
lines changed

.eslintignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.eslintrc.cjs

Lines changed: 0 additions & 85 deletions
This file was deleted.

eslint.config.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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[];

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,23 @@
4444
"@arco-plugins/vite-vue": "^1.4.6",
4545
"@types/lodash": "^4.17.20",
4646
"@types/mockjs": "^1.0.10",
47+
"@types/node": "^24.9.1",
4748
"@types/qs": "^6.14.0",
48-
"@typescript-eslint/eslint-plugin": "^6.21.0",
49-
"@typescript-eslint/parser": "^6.21.0",
49+
"@typescript-eslint/parser": "^8.46.2",
5050
"@vitejs/plugin-vue": "^6.0.1",
5151
"@vitejs/plugin-vue-jsx": "^5.1.1",
52+
"@vue/eslint-config-prettier": "^10.2.0",
53+
"@vue/eslint-config-typescript": "^14.6.0",
5254
"autoprefixer": "^10.4.21",
5355
"browserslist": "^4.26.3",
5456
"caniuse-lite": "^1.0.30001750",
55-
"eslint": "^8.57.1",
56-
"eslint-config-airbnb-base": "^15.0.0",
57-
"eslint-config-prettier": "^9.1.2",
58-
"eslint-import-resolver-typescript": "^3.10.1",
57+
"eslint": "^9.38.0",
58+
"eslint-config-airbnb-extended": "^2.3.2",
59+
"eslint-import-resolver-typescript": "^4.4.4",
5960
"eslint-plugin-import": "^2.32.0",
60-
"eslint-plugin-prettier": "^5.5.4",
61-
"eslint-plugin-vue": "^9.33.0",
61+
"eslint-plugin-vue": "~10.4.0",
6262
"husky": "^9.1.7",
63+
"jiti": "^2.6.1",
6364
"less": "^4.4.2",
6465
"lint-staged": "^16.2.4",
6566
"postcss": "^8.5.6",

0 commit comments

Comments
 (0)