feat(core-eslint): no-undeclared-event-publish rule
AST-aware ESLint rule that catches bus.publish("X") calls in use-case
files where X is not declared in the matching manifest.useCases[name].publishes array.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
56
packages/core-eslint/rules/no-undeclared-event-publish.js
Normal file
56
packages/core-eslint/rules/no-undeclared-event-publish.js
Normal file
@@ -0,0 +1,56 @@
|
||||
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:
|
||||
"bus.publish(\"X\") inside a use-case factory must declare X in manifest.useCases[name].publishes.",
|
||||
},
|
||||
schema: [
|
||||
{
|
||||
type: "object",
|
||||
properties: { repoRoot: { type: "string" } },
|
||||
additionalProperties: false,
|
||||
},
|
||||
],
|
||||
messages: {
|
||||
undeclared:
|
||||
"{{useCase}} calls bus.publish(\"{{event}}\") but {{event}} is not declared in manifest.useCases.{{useCase}}.publishes. 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].publishes);
|
||||
return {
|
||||
CallExpression(node) {
|
||||
if (
|
||||
node.callee.type === "MemberExpression" &&
|
||||
node.callee.object.type === "Identifier" &&
|
||||
node.callee.object.name === "bus" &&
|
||||
node.callee.property.type === "Identifier" &&
|
||||
node.callee.property.name === "publish" &&
|
||||
node.arguments.length > 0 &&
|
||||
node.arguments[0].type === "Literal" &&
|
||||
typeof node.arguments[0].value === "string"
|
||||
) {
|
||||
const event = node.arguments[0].value;
|
||||
if (!declared.has(event)) {
|
||||
context.report({ node, messageId: "undeclared", data: { event, useCase: useCaseName } });
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user