Files
Alfred/eslint-rules/enforce-catch-type.js
lanyuanxiaoyao ab7b7fb189 fix: 质量修复 — ESLint 规则 TS6 兼容 + catch 注解 + 空函数体注释化 + 后端架构对齐 + 前端红线修复
- enforce-catch-type: 增加 TSUnknownKeyword 判断,消除28个 TS6 假阳性
- no-empty-function: 统一为注释方案,移除测试/生产分支和 eslint-disable 引导
- logger.ts: 空函数体改为注释说明,删除无用 eslint-disable 指令
- 补充15处 catch 子句 : unknown 类型注解
- 清理7个测试文件失效 eslint-disable 指令
- chat/send.ts: 提取 getModelWithProvider DAO,消除直接 Drizzle 操作
- projects/update.ts: 修复死代码+条件逻辑 bug
- providers/update.ts: 补充至少一个字段校验
- 前端: inline style → CSS className, ProviderFormModal whitespace 校验
- 开发文档: 更新 Zod 使用说明(AI SDK 框架级约束)
2026-06-01 23:11:42 +08:00

61 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const enforceCatchType = {
meta: {
type: "problem",
docs: {
description:
"强制 catch 子句使用 e: unknown并用 instanceof Error 提取错误信息;空的 catch 块应添加注释说明原因",
},
messages: {
missingTypeAnnotation:
"catch 子句缺少类型注解。请使用 catch (e: unknown),然后用 e instanceof Error ? e.message : String(e) 提取错误信息。",
nonUnknownType:
"catch 的类型注解应为 unknown切勿使用 any。请改为 catch (e: unknown),然后用 e instanceof Error ? e.message : String(e) 提取错误信息。",
emptyCatchNoComment:
"空的 catch 块应添加注释说明为什么忽略此异常。如果确有理由静默吞掉错误,请在该 catch body 内添加注释。",
},
schema: [],
},
create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
function isUnknownType(typeAnnotation) {
if (!typeAnnotation) return false;
const type = typeAnnotation.typeAnnotation;
if (type?.type === "TSTypeReference") {
return type.typeName?.name === "unknown";
}
if (type?.type === "TSUnknownKeyword") {
return true;
}
return false;
}
function hasCommentsInBody(body) {
if (!body) return false;
return sourceCode.getCommentsInside(body).length > 0;
}
function check(node) {
const { param, body } = node;
if (param) {
const typeAnnotation = param.typeAnnotation;
if (!typeAnnotation) {
context.report({ node: param, messageId: "missingTypeAnnotation" });
} else if (!isUnknownType(typeAnnotation)) {
context.report({ node: typeAnnotation, messageId: "nonUnknownType" });
}
}
if (body && body.type === "BlockStatement" && body.body.length === 0 && !hasCommentsInBody(body)) {
context.report({ node: body, messageId: "emptyCatchNoComment" });
}
}
return {
CatchClause: check,
};
},
};