67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
import js from "@eslint/js";
|
||
import globals from "globals";
|
||
import tseslint from "typescript-eslint";
|
||
import { defineConfig } from "eslint/config";
|
||
import unusedImports from "eslint-plugin-unused-imports";
|
||
import unicorn from "eslint-plugin-unicorn";
|
||
|
||
export default defineConfig([
|
||
// 全局忽略声明文件
|
||
{ ignores: ["**/*.d.ts"] },
|
||
{
|
||
files: ["**/*.ts"],
|
||
plugins: { js, ...(unusedImports ? { "unused-imports": unusedImports } : {}), unicorn },
|
||
extends: ["js/recommended"],
|
||
languageOptions: { globals: globals.browser },
|
||
rules: {
|
||
// 当插件可用时,启用自动清理未使用的 import;否则回退到 TS 的未使用变量规则
|
||
...(unusedImports
|
||
? {
|
||
"unused-imports/no-unused-imports": "error",
|
||
"unused-imports/no-unused-vars": [
|
||
"warn",
|
||
{
|
||
vars: "all",
|
||
varsIgnorePattern: "^_",
|
||
args: "after-used",
|
||
argsIgnorePattern: "^_",
|
||
ignoreRestSiblings: true,
|
||
},
|
||
],
|
||
"@typescript-eslint/no-unused-vars": "off",
|
||
}
|
||
: {
|
||
"@typescript-eslint/no-unused-vars": [
|
||
"warn",
|
||
{
|
||
vars: "all",
|
||
varsIgnorePattern: "^_",
|
||
args: "after-used",
|
||
argsIgnorePattern: "^_",
|
||
ignoreRestSiblings: true,
|
||
},
|
||
],
|
||
}),
|
||
// 统一 try/catch 的错误变量名为 err
|
||
"unicorn/catch-error-name": ["error", { name: "err" }],
|
||
// 未重新赋值的变量使用 const
|
||
"prefer-const": [
|
||
"error",
|
||
{
|
||
destructuring: "all",
|
||
},
|
||
],
|
||
// 禁用 var 声明
|
||
"no-var": "error",
|
||
},
|
||
},
|
||
...tseslint.configs.recommended.map((cfg) => ({ ...cfg, files: ["**/*.ts"] })),
|
||
// 在所有 TS 推荐配置之后,显式关闭冲突的未使用变量规则
|
||
{
|
||
files: ["**/*.ts"],
|
||
rules: {
|
||
"@typescript-eslint/no-unused-vars": "off",
|
||
},
|
||
},
|
||
]);
|