import js from "@eslint/js"; import globals from "globals"; import pluginVue from "eslint-plugin-vue"; import { defineConfig } from "eslint/config"; // 引入 TypeScript ESLint 插件和解析器 import tsParser from "@typescript-eslint/parser"; import tsPlugin from "@typescript-eslint/eslint-plugin"; export default defineConfig([ // 1. 基础 JS 规则(适用于 JS/Vue 文件) { files: ["**/*.{js,mjs,cjs,vue}"], plugins: { js }, extends: ["js/recommended"] }, // 2. 浏览器环境全局变量(适用于 JS/Vue/TS 文件) { files: ["**/*.{js,mjs,cjs,vue,ts,tsx}"], languageOptions: { globals: globals.browser } }, // 3. Vue 基础规则 pluginVue.configs["flat/essential"], // 4. Auto-import 插件配置(保持不变) { plugins: { 'auto-import': { extensions: ['.js', '.mjs', '.jsx', '.vue', '.ts', '.tsx'], // 增加 TS 扩展名 }, }, }, // 5. TypeScript 专属配置(核心) { // 仅对 TS/TSX 文件生效 files: ["**/*.{ts,tsx}"], // 指定 TS 解析器 languageOptions: { parser: tsParser, // 解析器选项(需与 tsconfig.json 匹配) parserOptions: { ecmaVersion: "latest", // 支持最新 ES 语法 sourceType: "module", // 模块类型(ESM) project: "./tsconfig.json", // 指向项目的 tsconfig.json(启用类型检查) tsconfigRootDir: process.cwd(), // 确保找到 tsconfig.json 的根目录 }, }, // 引入 TS 插件 plugins: { "@typescript-eslint": tsPlugin, }, // 继承 TS 推荐规则(可选,按需调整) extends: [ "plugin:@typescript-eslint/recommended", // 基础推荐规则 "plugin:@typescript-eslint/recommended-requiring-type-checking" // 依赖类型检查的规则(需 project 配置) ], // TS 专属规则(可自定义) rules: { // 示例:禁用某些严格规则(根据需求调整) "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/no-explicit-any": "warn" } }, // 6. 全局规则(覆盖所有文件) { rules: { // 可在此处添加适用于所有文件的规则 // 例如关闭不必要的规则 "no-undef": "off" // TS 已有类型检查,可关闭 JS 的 undef 检查 }, }, // 7. Auto-import 配置(增加 TS 支持) { settings: { 'auto-import': { imports: ['vue', 'vue-router'], // 自动导入的模块 // 增加 TS 扩展名 extensions: ['.js', '.mjs', '.jsx', '.vue', '.ts', '.tsx'], }, }, }, ]);