77 lines
3.2 KiB
JavaScript
77 lines
3.2 KiB
JavaScript
|
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";
|
|||
|
|
|||
|
export default defineConfig([
|
|||
|
{
|
|||
|
ignores: [
|
|||
|
"**/dist/**", // 忽略所有 dist 文件夹
|
|||
|
"**/node_modules/**" // 忽略所有 node_modules
|
|||
|
]
|
|||
|
},
|
|||
|
// 1. 使用 ESLint 官方推荐规则
|
|||
|
js.configs.recommended,
|
|||
|
|
|||
|
// 2. 设置全局变量(浏览器、Node.js、ES2021、Jest)
|
|||
|
{
|
|||
|
files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
|
|||
|
languageOptions: {
|
|||
|
globals: {
|
|||
|
...globals.browser, // 浏览器全局变量(如 `window`, `document`)
|
|||
|
...globals.es2021, // ES2021 全局变量(如 `Promise`, `Map`)
|
|||
|
...globals.node, // Node.js 全局变量(如 `require`, `process`)
|
|||
|
...{ jest: "readonly" } // Jest 测试框架全局变量(如 `describe`, `test`)
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
|
|||
|
// 3. 添加 TypeScript 支持(使用 `@typescript-eslint` 推荐规则)
|
|||
|
...tseslint.configs.recommended,
|
|||
|
|
|||
|
// 4. 添加 Vue 3 支持(使用 `eslint-plugin-vue` 基础规则)
|
|||
|
...pluginVue.configs["flat/essential"],
|
|||
|
|
|||
|
// 5. 针对 `.vue` 文件的解析配置
|
|||
|
{
|
|||
|
files: ["**/*.vue"],
|
|||
|
languageOptions: {
|
|||
|
parserOptions: {
|
|||
|
ecmaVersion: "latest", // 使用最新的 ECMAScript 版本
|
|||
|
sourceType: "module", // 使用 ES Module 语法
|
|||
|
jsxPragma: "React", // 如果用了 JSX,指定 React 作为全局变量
|
|||
|
ecmaFeatures: {
|
|||
|
jsx: true // 启用 JSX 支持
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
|
|||
|
// 6. 自定义规则(覆盖或扩展默认规则)
|
|||
|
{
|
|||
|
rules: {
|
|||
|
// ESLint 基础规则
|
|||
|
"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", // 允许不必要的转义字符(如 `\/`)
|
|||
|
|
|||
|
// TypeScript 规则
|
|||
|
"@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", // 不强制分号
|
|||
|
|
|||
|
// Vue 规则
|
|||
|
"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`)
|
|||
|
}
|
|||
|
}
|
|||
|
]);
|