155 lines
6.6 KiB
JavaScript
155 lines
6.6 KiB
JavaScript
// 导入ESLint核心JavaScript规则配置
|
||
import js from '@eslint/js';
|
||
|
||
// 导入TypeScript ESLint插件,提供TypeScript相关的检查规则
|
||
import typescript from '@typescript-eslint/eslint-plugin';
|
||
|
||
// 导入TypeScript ESLint解析器,用于解析TypeScript代码
|
||
import typescriptParser from '@typescript-eslint/parser';
|
||
|
||
// 导入Vue ESLint插件,提供Vue特定的检查规则
|
||
import vueEslintPlugin from 'eslint-plugin-vue';
|
||
|
||
// 导出ESLint配置数组(使用flat配置格式,ESLint v9+推荐)
|
||
export default [
|
||
// 应用JavaScript默认推荐规则
|
||
js.configs.recommended,
|
||
|
||
// 应用Vue推荐规则(flat格式),使用扩展运算符展开数组
|
||
...vueEslintPlugin.configs['flat/recommended'],
|
||
|
||
// 文件忽略配置块
|
||
{
|
||
// ignores属性指定需要ESLint忽略检查的文件路径模式
|
||
ignores: [
|
||
'**/*.test.ts', // 忽略所有测试文件
|
||
'**/*.spec.ts', // 忽略所有规格文件
|
||
'test/**', // 忽略test目录下所有文件
|
||
'dist/**', // 忽略构建输出目录
|
||
'node_modules/**', // 忽略第三方依赖
|
||
'coverage/**', // 忽略测试覆盖率报告目录
|
||
'*.config.js', // 忽略配置文件
|
||
'*.config.ts', // 忽略TypeScript配置文件
|
||
],
|
||
},
|
||
// Vue文件特定规则配置块
|
||
{
|
||
// files属性指定此配置块适用的文件模式
|
||
files: ['**/*.vue'],
|
||
|
||
// languageOptions配置解析器和语言选项
|
||
languageOptions: {
|
||
// 使用Vue ESLint插件提供的解析器来解析.vue文件
|
||
parser: vueEslintPlugin.parser,
|
||
|
||
// parserOptions配置解析器选项
|
||
parserOptions: {
|
||
// 使用最新的ECMAScript版本
|
||
ecmaVersion: 'latest',
|
||
|
||
// 源代码类型为module(ES模块)
|
||
sourceType: 'module',
|
||
|
||
// 为Vue文件中的<script>块指定TypeScript解析器
|
||
parser: typescriptParser,
|
||
|
||
// 禁用projectService以避免与TypeScript编译器重复检查
|
||
projectService: false, // 用tsc检查过了
|
||
|
||
// 添加.vue作为额外的文件扩展名
|
||
extraFileExtensions: ['.vue'],
|
||
},
|
||
},
|
||
|
||
// plugins指定在此配置块中启用的插件
|
||
plugins: {
|
||
'@typescript-eslint': typescript, // 启用TypeScript ESLint插件
|
||
vue: vueEslintPlugin, // 启用Vue ESLint插件
|
||
},
|
||
|
||
// rules配置具体的ESLint规则及其严重级别
|
||
rules: {
|
||
// Vue特定规则
|
||
'vue/multi-word-component-names': 'off', // 关闭组件名称必须为多个单词的规则
|
||
'vue/no-v-model-argument': 'error', // 禁止在v-model中使用参数,违反时报错
|
||
'vue/script-setup-uses-vars': 'error', // 确保script-setup中声明的变量被使用,违反时报错
|
||
|
||
// TypeScript规则
|
||
'@typescript-eslint/no-unused-vars': ['error', { // 未使用的变量报错
|
||
argsIgnorePattern: '^_', // 忽略以下划线开头的参数
|
||
varsIgnorePattern: '^[A-Z_]+$' // 忽略全大写或带下划线的常量
|
||
}],
|
||
|
||
// 通用规则
|
||
'no-unused-vars': 'off', // 关闭JavaScript原生未使用变量规则(使用TypeScript版本替代)
|
||
'no-console': 'warn', // 禁止使用console,违反时警告
|
||
'no-debugger': 'error', // 禁止使用debugger,违反时报错
|
||
'no-var': 'error', // 禁止使用var声明,违反时报错
|
||
'prefer-const': 'error', // 优先使用const声明,违反时报错
|
||
|
||
// 代码风格
|
||
'indent': ['error', 2, { SwitchCase: 1 }], // 强制2空格缩进,switch case额外缩进1级
|
||
'quotes': ['error', 'single', { avoidEscape: true }], // 强制使用单引号,允许转义的例外情况
|
||
'semi': ['error', 'always'], // 语句末尾必须有分号
|
||
'comma-dangle': ['error', 'never'], // 禁止行末逗号
|
||
'eol-last': ['warn', 'always'], // 文件末尾必须有空行
|
||
'no-trailing-spaces': 'warn', // 禁止行尾空格
|
||
},
|
||
},
|
||
// TypeScript文件特定规则配置块(排除测试文件)
|
||
{
|
||
// files属性指定此配置块适用的文件模式
|
||
files: ['**/*.ts', '**/*.tsx'],
|
||
|
||
// ignores在此配置块中进一步排除特定文件
|
||
ignores: ['**/*.test.ts', '**/*.spec.ts', 'test/**/*.ts'],
|
||
|
||
// languageOptions配置解析器和语言选项
|
||
languageOptions: {
|
||
// 使用TypeScript解析器
|
||
parser: typescriptParser,
|
||
|
||
// parserOptions配置解析器选项
|
||
parserOptions: {
|
||
// 使用最新的ECMAScript版本
|
||
ecmaVersion: 'latest',
|
||
|
||
// 源代码类型为module(ES模块)
|
||
sourceType: 'module',
|
||
|
||
// 禁用projectService以提高性能
|
||
projectService: false, // vue-tsc来检查类型安全
|
||
},
|
||
},
|
||
|
||
// plugins指定在此配置块中启用的插件
|
||
plugins: {
|
||
'@typescript-eslint': typescript, // 启用TypeScript ESLint插件
|
||
},
|
||
|
||
// rules配置具体的ESLint规则及其严重级别
|
||
rules: {
|
||
'no-unused-vars': 'off', // 关闭JavaScript原生未使用变量规则(使用TypeScript版本替代)
|
||
'@typescript-eslint/no-unused-vars': ['error', { // 未使用的变量报错
|
||
argsIgnorePattern: '^_', // 忽略以下划线开头的参数(函数参数)
|
||
varsIgnorePattern: '^[A-Z_]+$' // 忽略全大写或带下划线的常量
|
||
}],
|
||
'@typescript-eslint/no-explicit-any': 'warn', // 禁止使用any类型,违反时警告
|
||
'@typescript-eslint/no-non-null-assertion': 'warn', // 禁止使用非空断言(!),违反时警告
|
||
|
||
// 通用规则
|
||
'no-console': 'warn', // 禁止使用console,违反时警告
|
||
'no-debugger': 'error', // 禁止使用debugger,违反时报错
|
||
'no-var': 'error', // 禁止使用var声明,违反时报错
|
||
'prefer-const': 'error', // 优先使用const声明,违反时报错
|
||
|
||
// 代码风格
|
||
'indent': ['error', 2, { SwitchCase: 1 }], // 强制2空格缩进,switch case额外缩进1级
|
||
'quotes': ['error', 'single', { avoidEscape: true }], // 强制使用单引号,允许转义的例外情况
|
||
'semi': ['error', 'always'], // 语句末尾必须有分号
|
||
'comma-dangle': ['error', 'never'], // 禁止行末逗号
|
||
'eol-last': ['warn', 'always'], // 文件末尾必须有空行
|
||
'no-trailing-spaces': 'warn', // 禁止行尾空格
|
||
},
|
||
},
|
||
]; // ESLint配置数组结束
|