Files
agentic-dev/packages/core-eslint/rules/no-undeclared-rate-limit.js
Danijel Martinek 18cef2f889 feat(core-eslint): add no-undeclared-rate-limit conformance rule
Adds the `no-undeclared-rate-limit` ESLint rule (warn severity) that
enforces rate-limit drift at lint time:

- Warns when rateLimit.consume("X", _) is called inside a use-case but
  "X" is absent from manifest.useCases[name].rateLimit
- Warns when a declared rateLimit budget has no matching consume call
  in the use-case body (unusedDeclaration)
- Is a no-op outside use-case files

Extends _manifest-ast.js to extract the rateLimit[] field from both
parseManifestUseCases and parseManifestFully. Updates _manifest-ast
tests to include the new field in expected shapes. Registers the rule
at warn severity in plugin.js and base.js. Adds RuleTester fixtures
for all four cases (declared+matching, undeclared, unused, non-use-case).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-20 08:43:30 +00:00

68 lines
2.4 KiB
JavaScript

import { parseManifestUseCases } from "./_manifest-ast.js";
import { manifestPathForFeature } from "./_manifest-source.js";
import { repoRootSchema } from "./_rule-schema.js";
import { resolveRuleContext } from "./_rule-context.js";
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "suggestion",
docs: {
description:
'rateLimit.consume("X", _) inside a use-case file must match a budget declared in manifest.useCases[name].rateLimit.',
},
schema: repoRootSchema,
messages: {
undeclared:
'{{useCase}} calls rateLimit.consume("{{budget}}") but "{{budget}}" is not declared in manifest.useCases.{{useCase}}.rateLimit. Add it to the manifest or remove the call.',
unusedDeclaration:
'{{useCase}} declares rateLimit budget "{{budget}}" in the manifest but rateLimit.consume("{{budget}}") is never called in this file. Add the consume call or remove the manifest declaration.',
},
},
create(context) {
const rc = resolveRuleContext(context);
if (!rc) return {};
const { useCaseName, featureRoot } = rc;
const manifest = parseManifestUseCases(manifestPathForFeature(featureRoot));
if (!manifest || !manifest[useCaseName]) return {};
const declared = new Set(manifest[useCaseName].rateLimit ?? []);
const consumed = new Set();
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.object.type === "Identifier" &&
node.callee.object.name === "rateLimit" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "consume" &&
node.arguments.length > 0 &&
node.arguments[0].type === "Literal" &&
typeof node.arguments[0].value === "string"
) {
const budget = node.arguments[0].value;
consumed.add(budget);
if (!declared.has(budget)) {
context.report({
node,
messageId: "undeclared",
data: { budget, useCase: useCaseName },
});
}
}
},
"Program:exit"(node) {
for (const budget of declared) {
if (!consumed.has(budget)) {
context.report({
node,
messageId: "unusedDeclaration",
data: { budget, useCase: useCaseName },
});
}
}
},
};
},
};