Files
agentic-dev/packages/core-eslint/rules/no-undeclared-audit.js
Danijel Martinek 15e90820d2 feat(core-eslint): no-undeclared-audit rule
AST-aware ESLint rule that catches auditLog.record({ type: "X" }) calls
in use-case files where X is not declared in the matching
manifest.useCases[name].audits array.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-12 23:51:42 +02:00

68 lines
2.4 KiB
JavaScript

import { parseManifestUseCases } from "./_manifest-ast.js";
import { useCaseNameFromFile } from "./_usecase-name.js";
import { manifestPathForFeature, featureRootForFile } from "./_manifest-source.js";
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "problem",
docs: {
description:
"auditLog.record({ type: \"X\" }) inside a use-case factory must declare X in manifest.useCases[name].audits.",
},
schema: [
{
type: "object",
properties: { repoRoot: { type: "string" } },
additionalProperties: false,
},
],
messages: {
undeclared:
"{{useCase}} calls auditLog.record with type \"{{event}}\" but {{event}} is not declared in manifest.useCases.{{useCase}}.audits. Add it to the manifest or remove the call.",
},
},
create(context) {
const opts = context.options[0] ?? {};
const repoRoot = opts.repoRoot ?? context.cwd ?? process.cwd();
const filename = context.filename;
const useCaseName = useCaseNameFromFile(filename);
if (!useCaseName) return {};
const featureRoot = featureRootForFile(filename, repoRoot);
if (!featureRoot) return {};
const manifest = parseManifestUseCases(manifestPathForFeature(featureRoot));
if (!manifest || !manifest[useCaseName]) return {};
const declared = new Set(manifest[useCaseName].audits);
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.object.type === "Identifier" &&
node.callee.object.name === "auditLog" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "record" &&
node.arguments.length > 0 &&
node.arguments[0].type === "ObjectExpression"
) {
const typeProp = node.arguments[0].properties.find(
(p) =>
p.type === "Property" &&
p.key.type === "Identifier" &&
p.key.name === "type",
);
if (
typeProp &&
typeProp.value.type === "Literal" &&
typeof typeProp.value.value === "string"
) {
const event = typeProp.value.value;
if (!declared.has(event)) {
context.report({ node, messageId: "undeclared", data: { event, useCase: useCaseName } });
}
}
}
},
};
},
};