1
- import globals from "globals" ; // 全局变量配置
2
- import pluginJs from "@eslint/js" ; // JavaScript 的推荐配置
3
- import tseslint from "typescript-eslint" ; // TypeScript 配置
4
- import pluginVue from "eslint-plugin-vue" ; // Vue 配置
1
+ import globals from "globals" ;
2
+ import js from "@eslint/js" ;
5
3
6
- import { readFileSync } from "fs" ;
7
- import { fileURLToPath } from "url " ;
8
- import { dirname , resolve } from "path " ;
4
+ // ESLint 核心插件
5
+ import pluginVue from "eslint-plugin-vue " ;
6
+ import pluginTypeScript from "@typescript-eslint/eslint-plugin " ;
9
7
10
- // 动态读取 .eslintrc-auto-import.json 文件内容
11
- const autoImportConfig = JSON . parse (
12
- readFileSync (
13
- resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , ".eslintrc-auto-import.json" ) ,
14
- "utf-8" ,
15
- ) ,
16
- ) ;
8
+ // Prettier 插件及配置
9
+ import configPrettier from "eslint-config-prettier" ;
10
+ import pluginPrettier from "eslint-plugin-prettier" ;
17
11
18
- export default [
19
- { files : [ "**/*.{js,mjs,cjs,ts,vue}" ] } , // 校验的文件类型
20
- {
21
- // 语言选项配置,定义全局变量
22
- languageOptions : {
23
- globals : {
24
- ...globals . browser , // 浏览器变量 (window, document 等)
25
- ...globals . node , // Node.js 变量 (process, require 等)
26
- ...autoImportConfig . globals , // 自动导入的全局变量
27
- ...{
28
- uni : "readonly" , // uni-app 全局对象
29
- } ,
30
- } ,
31
- } ,
32
- } ,
33
- pluginJs . configs . recommended , // JavaScript 推荐配置
34
- ...tseslint . configs . recommended , // TypeScript 推荐配置
35
- ...pluginVue . configs [ "flat/essential" ] , // Vue 推荐配置
36
- { files : [ "**/*.vue" ] , languageOptions : { parserOptions : { parser : tseslint . parser } } } , // 对 .vue 文件使用 TypeScript 解析器
12
+ // 解析器
13
+ import * as parserVue from "vue-eslint-parser" ;
14
+ import * as parserTypeScript from "@typescript-eslint/parser" ;
37
15
38
- // 添加忽略的文件或目录
16
+ // 定义 ESLint 配置
17
+ export default [
18
+ // 通用 JavaScript 配置
39
19
{
20
+ ...js . configs . recommended ,
40
21
ignores : [
41
22
"/dist" ,
42
23
"/public" ,
@@ -46,28 +27,123 @@ export default [
46
27
"**/*.tsbuildinfo" ,
47
28
"/src/manifest.json" ,
48
29
] ,
30
+ languageOptions : {
31
+ globals : {
32
+ ...globals . browser , // 浏览器变量 (window, document 等)
33
+ ...globals . node , // Node.js 变量 (process, require 等)
34
+ } ,
35
+ } ,
36
+ plugins : {
37
+ prettier : pluginPrettier ,
38
+ } ,
39
+ rules : {
40
+ ...configPrettier . rules ,
41
+ ...pluginPrettier . configs . recommended . rules ,
42
+ "no-debug" : "off" , // 禁止 debugger
43
+ "no-unused-vars" : [
44
+ "error" ,
45
+ {
46
+ argsIgnorePattern : "^_" ,
47
+ varsIgnorePattern : "^_" ,
48
+ } ,
49
+ ] , // 允许未使用的变量,以 _ 开头的变量不检查
50
+ "prettier/prettier" : [
51
+ "error" ,
52
+ {
53
+ endOfLine : "auto" , // 自动识别换行符
54
+ } ,
55
+ ] ,
56
+ } ,
49
57
} ,
50
58
51
- // 自定义规则
59
+ // TypeScript 配置
52
60
{
61
+ files : [ "**/*.?([cm])ts" ] ,
62
+ languageOptions : {
63
+ parser : parserTypeScript ,
64
+ parserOptions : {
65
+ sourceType : "module" ,
66
+ } ,
67
+ } ,
68
+ plugins : {
69
+ "@typescript-eslint" : pluginTypeScript ,
70
+ } ,
53
71
rules : {
54
- quotes : [ "error" , "double" ] , // 强制使用双引号
55
- "quote-props" : [ "error" , "always" ] , // 强制对象的属性名使用引号
56
- semi : [ "error" , "always" ] , // 要求使用分号
57
- indent : [ "error" , 2 ] , // 使用两个空格进行缩进
58
- "no-multiple-empty-lines" : [ "error" , { max : 1 } ] , // 不允许多个空行
59
- "no-trailing-spaces" : "error" , // 不允许行尾有空格
72
+ ...pluginTypeScript . configs . strict . rules ,
73
+ "@typescript-eslint/ban-types" : "off" , // 禁止特定类型
74
+ "@typescript-eslint/no-redeclare" : "error" , // 禁止重复声明
75
+ "@typescript-eslint/ban-ts-comment" : "off" , // 禁止特定注释
76
+ "@typescript-eslint/no-explicit-any" : "off" , // 禁止使用 any
77
+ "@typescript-eslint/prefer-as-const" : "warn" , // 使用 as const 替代 as 'const'
78
+ "@typescript-eslint/no-empty-function" : "off" , // 禁止空函数
79
+ "@typescript-eslint/no-non-null-assertion" : "off" , // 禁止非空断言
80
+ "@typescript-eslint/no-import-type-side-effects" : "error" , // 禁止导入类型产生副作用
81
+ "@typescript-eslint/explicit-module-boundary-types" : "off" , // 显式函数返回类型
82
+ "@typescript-eslint/consistent-type-imports" : [
83
+ "error" ,
84
+ { disallowTypeAnnotations : false , fixStyle : "inline-type-imports" } ,
85
+ ] , // 一致的类型导入
86
+ "@typescript-eslint/prefer-literal-enum-member" : [ "error" , { allowBitwiseExpressions : true } ] , // 使用字面量枚举成员
87
+ "@typescript-eslint/no-empty-object-type" : "off" , // 禁止空对象类型
88
+ } ,
89
+ } ,
90
+
91
+ // TypeScript 声明文件的特殊配置
92
+ {
93
+ files : [ "**/*.d.ts" ] ,
94
+ rules : {
95
+ "eslint-comments/no-unlimited-disable" : "off" ,
96
+ "unused-imports/no-unused-vars" : "off" ,
97
+ } ,
98
+ } ,
60
99
61
- // TypeScript 规则
62
- "@typescript-eslint/no-explicit-any" : "off" , // 禁用 no-explicit-any 规则,允许使用 any 类型
63
- "@typescript-eslint/explicit-function-return-type" : "off" , // 不强制要求函数必须明确返回类型
64
- "@typescript-eslint/no-empty-interface" : "off" , // 禁用 no-empty-interface 规则,允许空接口声明
65
- "@typescript-eslint/no-empty-object-type" : "off" , // 允许空对象类型
100
+ // JavaScript 配置(包含 commonjs)
101
+ {
102
+ files : [ "**/*.?([cm])js" ] ,
103
+ rules : {
104
+ "@typescript-eslint/no-require-imports" : "off" , // 禁止 require
105
+ "@typescript-eslint/no-var-requires" : "off" , // 禁止 require
106
+ } ,
107
+ } ,
66
108
67
- // Vue 规则
68
- "vue/multi-word-component-names" : "off" , // 关闭多单词组件名称的限制
69
- "vue/html-indent" : [ "error" , 2 ] , // Vue 模板中的 HTML 缩进使用两个空格
70
- "vue/no-v-html" : "off" , // 允许使用 v-html (根据实际项目需要)
109
+ // Vue 文件配置
110
+ {
111
+ files : [ "**/*.vue" ] ,
112
+ languageOptions : {
113
+ parser : parserVue ,
114
+ parserOptions : {
115
+ extraFileExtensions : [ ".vue" ] ,
116
+ parser : "@typescript-eslint/parser" ,
117
+ sourceType : "module" ,
118
+ } ,
119
+ } ,
120
+ plugins : {
121
+ vue : pluginVue ,
122
+ } ,
123
+ processor : pluginVue . processors [ ".vue" ] ,
124
+ rules : {
125
+ ...pluginVue . configs . base . rules , // Vue 基础配置
126
+ ...pluginVue . configs [ "vue3-essential" ] . rules , // Vue3 基础配置
127
+ ...pluginVue . configs [ "vue3-recommended" ] . rules , // Vue3 推荐配置
128
+ "no-undef" : "off" ,
129
+ "no-unused-vars" : "off" ,
130
+ "vue/no-v-html" : "off" ,
131
+ "vue/require-default-prop" : "off" ,
132
+ "vue/require-explicit-emits" : "off" ,
133
+ "vue/multi-word-component-names" : "off" ,
134
+ "vue/no-setup-props-reactivity-loss" : "off" ,
135
+ "vue/html-self-closing" : [
136
+ "error" ,
137
+ {
138
+ html : {
139
+ void : "always" ,
140
+ normal : "always" ,
141
+ component : "always" ,
142
+ } ,
143
+ svg : "always" ,
144
+ math : "always" ,
145
+ } ,
146
+ ] , // 自闭合标签
71
147
} ,
72
148
} ,
73
149
] ;
0 commit comments