@@ -3,37 +3,70 @@ import tseslint from 'typescript-eslint';
33import pluginVue from 'eslint-plugin-vue' ;
44
55export default tseslint . config (
6- { ignores : [ '**/node_modules' , '**/dist' , '**/*.js' ] } , // 忽略 node_modules 和 dist 目录
6+ // 全局忽略配置
7+ { ignores : [ '**/node_modules' , '**/dist' , '**/*.js' ] } ,
8+
9+ // 基础配置
710 eslint . configs . recommended , // 使用 ESLint 的推荐配置
811 tseslint . configs . base , // 使用 TypeScript ESLint 的基础配置
912 ...pluginVue . configs [ 'flat/recommended' ] , // 使用 Vue ESLint 的推荐配置
10- {
11- files : [ '**/*.vue' ] , // 针对所有 .vue 文件
12- languageOptions : {
13- parserOptions : {
14- parser : '@typescript-eslint/parser' , // 使用 TypeScript ESLint 解析器
15- } ,
16- } ,
17- } ,
13+
14+ // 通用规则配置(适用于所有文件)
1815 {
1916 rules : {
20- 'no-debugger' : 'error' , // 禁止使用 debugger 语句
2117 // 'no-console': ['error', { allow: ['warn', 'error', 'info', 'clear'] }], // 禁止使用 console 语句,但允许 warn, error, info 和 clear
18+ 'no-debugger' : 'error' , // 禁止使用 debugger 语句
2219 'prefer-const' : 'error' , // 强制使用 const 而不是 let
2320 'sort-imports' : [ 'error' , { ignoreDeclarationSort : true } ] , // 强制排序导入语句,但忽略声明排序
2421 'no-duplicate-imports' : 'error' , // 禁止重复导入
25- // 该规则强制使用 '@ts-expect-error' 注释在 TypeScript 代码中指示故意的类型错误,提高代码的清晰度和可维护性。
22+ // 禁用对未使用变量的检查(针对类型声明)
23+ 'no-unused-vars' : 'off' ,
24+ // 改用 TypeScript 专属规则(更智能)
25+ '@typescript-eslint/no-unused-vars' : [ 'error' ] ,
26+ } ,
27+ } ,
28+
29+ // TypeScript 文件配置
30+ {
31+ files : [ '**/*.ts' , '**/*.tsx' ] ,
32+ languageOptions : {
33+ parser : tseslint . parser ,
34+ parserOptions : {
35+ createDefaultProgram : false ,
36+ ecmaFeatures : {
37+ jsx : true ,
38+ } ,
39+ ecmaVersion : 'latest' ,
40+ extraFileExtensions : [ '.vue' ] ,
41+ jsxPragma : 'React' ,
42+ project : './tsconfig.*.json' ,
43+ sourceType : 'module' ,
44+ } ,
45+ } ,
46+ rules : {
47+ // TypeScript 特定规则
2648 '@typescript-eslint/prefer-ts-expect-error' : 'error' , // 强制使用 @ts-expect-error 而不是 @ts-ignore
27- // 强制使用 'import type' 进行类型导入
2849 '@typescript-eslint/consistent-type-imports' : [
2950 'error' ,
3051 {
3152 fixStyle : 'inline-type-imports' , // 使用内联类型导入样式
3253 disallowTypeAnnotations : false , // 允许类型注解
3354 } ,
3455 ] ,
35- // 强制在导入仅包含内联类型限定符的规范时使用顶级导入类型限定符
3656 '@typescript-eslint/no-import-type-side-effects' : 'error' , // 禁止导入类型时产生副作用
57+ } ,
58+ } ,
59+
60+ // Vue 文件配置
61+ {
62+ files : [ '**/*.vue' ] ,
63+ languageOptions : {
64+ parserOptions : {
65+ parser : '@typescript-eslint/parser' , // 使用 TypeScript ESLint 解析器解析 Vue 文件中的 TypeScript
66+ } ,
67+ } ,
68+ rules : {
69+ // Vue 特定规则
3770 'vue/max-attributes-per-line' : 'off' , // 关闭每行最多属性数的限制
3871 'vue/singleline-html-element-content-newline' : 'off' , // 关闭单行 HTML 元素内容换行的限制
3972 'vue/multi-word-component-names' : 'off' , // 关闭多单词组件名称的限制
@@ -45,6 +78,17 @@ export default tseslint.config(
4578 svg : 'always' , // 强制 svg 元素始终自闭合
4679 } ,
4780 ] ,
81+
82+ // Vue 文件中的 TypeScript 规则
83+ '@typescript-eslint/prefer-ts-expect-error' : 'error' ,
84+ '@typescript-eslint/consistent-type-imports' : [
85+ 'error' ,
86+ {
87+ fixStyle : 'inline-type-imports' ,
88+ disallowTypeAnnotations : false ,
89+ } ,
90+ ] ,
91+ '@typescript-eslint/no-import-type-side-effects' : 'error' ,
4892 } ,
4993 } ,
5094) ;
0 commit comments