2025-07-10 11:58:13 +08:00
|
|
|
|
import js from '@eslint/js'
|
|
|
|
|
import globals from 'globals'
|
|
|
|
|
import tseslint from 'typescript-eslint'
|
|
|
|
|
import pluginVue from 'eslint-plugin-vue'
|
|
|
|
|
import { defineConfig } from 'eslint/config'
|
2025-07-10 11:24:06 +08:00
|
|
|
|
|
|
|
|
|
export default defineConfig([
|
|
|
|
|
{
|
|
|
|
|
ignores: [
|
2025-07-10 11:58:13 +08:00
|
|
|
|
'**/dist/**', // 忽略所有 dist 文件夹
|
|
|
|
|
'**/node_modules/**', // 忽略所有 node_modules
|
|
|
|
|
],
|
2025-07-10 11:24:06 +08:00
|
|
|
|
},
|
|
|
|
|
// 1. 使用 ESLint 官方推荐规则
|
|
|
|
|
js.configs.recommended,
|
2025-07-10 11:58:13 +08:00
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// 2. 设置全局变量(浏览器、Node.js、ES2021、Jest)
|
|
|
|
|
{
|
2025-07-10 11:58:13 +08:00
|
|
|
|
files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
|
2025-07-10 11:24:06 +08:00
|
|
|
|
languageOptions: {
|
|
|
|
|
globals: {
|
2025-07-10 11:58:13 +08:00
|
|
|
|
...globals.browser, // 浏览器全局变量(如 `window`, `document`)
|
|
|
|
|
...globals.es2021, // ES2021 全局变量(如 `Promise`, `Map`)
|
|
|
|
|
...globals.node, // Node.js 全局变量(如 `require`, `process`)
|
|
|
|
|
...{ jest: 'readonly' }, // Jest 测试框架全局变量(如 `describe`, `test`)
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-07-10 11:24:06 +08:00
|
|
|
|
},
|
2025-07-10 11:58:13 +08:00
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// 3. 添加 TypeScript 支持(使用 `@typescript-eslint` 推荐规则)
|
|
|
|
|
...tseslint.configs.recommended,
|
2025-07-10 11:58:13 +08:00
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// 4. 添加 Vue 3 支持(使用 `eslint-plugin-vue` 基础规则)
|
2025-07-10 11:58:13 +08:00
|
|
|
|
...pluginVue.configs['flat/essential'],
|
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// 5. 针对 `.vue` 文件的解析配置
|
|
|
|
|
{
|
2025-07-10 11:58:13 +08:00
|
|
|
|
files: ['**/*.vue'],
|
2025-07-10 11:24:06 +08:00
|
|
|
|
languageOptions: {
|
|
|
|
|
parserOptions: {
|
2025-07-10 11:58:13 +08:00
|
|
|
|
ecmaVersion: 'latest', // 使用最新的 ECMAScript 版本
|
|
|
|
|
sourceType: 'module', // 使用 ES Module 语法
|
|
|
|
|
jsxPragma: 'React', // 如果用了 JSX,指定 React 作为全局变量
|
2025-07-10 11:24:06 +08:00
|
|
|
|
ecmaFeatures: {
|
2025-07-10 11:58:13 +08:00
|
|
|
|
jsx: true, // 启用 JSX 支持
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-07-10 11:24:06 +08:00
|
|
|
|
},
|
2025-07-10 11:58:13 +08:00
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// 6. 自定义规则(覆盖或扩展默认规则)
|
|
|
|
|
{
|
|
|
|
|
rules: {
|
|
|
|
|
// ESLint 基础规则
|
2025-07-10 11:58:13 +08:00
|
|
|
|
'no-var': 'error', // 禁止使用 `var`,必须用 `let` 或 `const`
|
|
|
|
|
'no-multiple-empty-lines': ['warn', { max: 1 }], // 最多允许 1 个空行
|
|
|
|
|
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', // 生产环境禁止 `console`
|
|
|
|
|
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', // 生产环境禁止 `debugger`
|
|
|
|
|
'no-unexpected-multiline': 'error', // 禁止意外的多行代码
|
|
|
|
|
'no-useless-escape': 'off', // 允许不必要的转义字符(如 `\/`)
|
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// TypeScript 规则
|
2025-07-10 11:58:13 +08:00
|
|
|
|
'@typescript-eslint/no-unused-vars': 'error', // 禁止未使用的变量
|
|
|
|
|
'@typescript-eslint/prefer-ts-expect-error': 'error', // 禁止 `@ts-ignore`,推荐用 `@ts-expect-error`
|
|
|
|
|
'@typescript-eslint/no-explicit-any': 'off', // 允许使用 `any` 类型
|
|
|
|
|
'@typescript-eslint/no-non-null-assertion': 'off', // 允许 `!` 非空断言
|
|
|
|
|
'@typescript-eslint/no-namespace': 'off', // 允许使用 `namespace`
|
|
|
|
|
'@typescript-eslint/semi': 'off', // 不强制分号
|
|
|
|
|
|
2025-07-10 11:24:06 +08:00
|
|
|
|
// Vue 规则
|
2025-07-10 11:58:13 +08:00
|
|
|
|
'vue/multi-word-component-names': 'off', // 允许单单词组件名(如 `Home.vue`)
|
|
|
|
|
'vue/script-setup-uses-vars': 'error', // 确保 `<script setup>` 的变量在 `<template>` 里可用
|
|
|
|
|
'vue/no-mutating-props': 'off', // 允许直接修改 `props`(不推荐,但有时需要)
|
|
|
|
|
'vue/attribute-hyphenation': 'off', // 允许非连字符式属性名(如 `:customProp`)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
])
|