export const noEmptyFunction = { meta: { type: "problem", docs: { description: "禁止空函数体。修复方式:在函数体内添加注释说明为何为空实现(如接口契约、测试桩、noop)。", }, messages: { unexpected: "空函数体禁止使用。请在 {} 内添加注释说明原因,例如:/* 实现 Logger 接口契约,有意静默丢弃 */。", }, schema: [], }, create(context) { const sourceCode = context.sourceCode ?? context.getSourceCode(); const allowedFunctionTypes = new Set(["ArrowFunctionExpression", "FunctionDeclaration", "FunctionExpression"]); function isEmptyBody(body) { return ( body.type === "BlockStatement" && body.body.length === 0 && sourceCode.getCommentsInside(body).length === 0 ); } function hasDecorator(node) { return Array.isArray(node.decorators) && node.decorators.length > 0; } function isPrivateOrProtectedConstructor(node) { if (node.parent?.type !== "MethodDefinition") return false; if (node.parent.kind !== "constructor") return false; const accessibility = node.parent.accessibility; return accessibility === "private" || accessibility === "protected"; } function isOverrideMethod(node) { if (node.parent?.type !== "MethodDefinition") return false; return node.parent.override === true; } function check(node) { if (!allowedFunctionTypes.has(node.type)) return; if (!isEmptyBody(node.body)) return; if (hasDecorator(node)) return; if (isPrivateOrProtectedConstructor(node)) return; if (isOverrideMethod(node)) return; context.report({ node, messageId: "unexpected", }); } return { ArrowFunctionExpression: check, FunctionDeclaration: check, FunctionExpression: check, }; }, };