Extends the conformance ESLint layer with the consent-check rule:
- `no-undeclared-consent-check` (warn): `consent.isGranted("X")` in a
use-case file must match a category declared in `manifest.requiresConsent`;
also warns when requiresConsent is declared but no isGranted call is found.
- `_manifest-ast.js`: adds `parseManifestFully` which extracts top-level
`name`, `requiredCores`, `requiresConsent`, and per-use-case maps from the
manifest AST; `requiresConsent` extraction tested in `_manifest-ast.test.js`.
- `_rule-context.js` / `_rule-schema.js`: shared helpers extracted from the
existing per-rule files so the new rule can resolve use-case name + feature
root without duplication.
- Existing rules (`no-undeclared-audit`, `no-undeclared-event-publish`,
`no-undeclared-analytics-event`) updated to use the shared helpers.
- `plugin.js` + `base.js` register the rule at warn severity.
- CLAUDE.md + conformance-quickref.md: rule count advanced from 11 → 12.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
import { describe, it } from "vitest";
|
|
import { RuleTester } from "eslint";
|
|
import path from "node:path";
|
|
import os from "node:os";
|
|
import fs from "node:fs";
|
|
import rule from "./no-undeclared-consent-check.js";
|
|
|
|
function makeFixture({ requiresConsent, useCaseBody }) {
|
|
const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), "nucc-"));
|
|
const featureDir = path.join(repoRoot, "packages", "demo");
|
|
fs.mkdirSync(path.join(featureDir, "src", "application", "use-cases"), {
|
|
recursive: true,
|
|
});
|
|
const consentField =
|
|
requiresConsent.length > 0
|
|
? ` requiresConsent: [${requiresConsent.map((c) => `"${c}"`).join(", ")}],\n`
|
|
: "";
|
|
fs.writeFileSync(
|
|
path.join(featureDir, "src", "feature.manifest.ts"),
|
|
`export const demoManifest = defineFeature({
|
|
name: "demo",
|
|
requiredCores: [],
|
|
${consentField} useCases: {
|
|
checkData: { mutates: false, audits: [], publishes: [], consumes: [] },
|
|
},
|
|
realtimeChannels: [],
|
|
jobs: [],
|
|
} as const);`,
|
|
);
|
|
const useCaseFile = path.join(
|
|
featureDir,
|
|
"src",
|
|
"application",
|
|
"use-cases",
|
|
"check-data.use-case.ts",
|
|
);
|
|
fs.writeFileSync(useCaseFile, useCaseBody);
|
|
return { repoRoot, useCaseFile };
|
|
}
|
|
|
|
const tester = new RuleTester({
|
|
languageOptions: {
|
|
parser: await import("@typescript-eslint/parser"),
|
|
ecmaVersion: "latest",
|
|
sourceType: "module",
|
|
},
|
|
});
|
|
|
|
describe("no-undeclared-consent-check", () => {
|
|
it("passes when consent.isGranted category matches manifest requiresConsent", () => {
|
|
const { repoRoot, useCaseFile } = makeFixture({
|
|
requiresConsent: ["analytics"],
|
|
useCaseBody: `export const checkDataUseCase = (consent) => async () => { if (!consent.isGranted("analytics")) throw new Error(); };`,
|
|
});
|
|
tester.run("no-undeclared-consent-check", rule, {
|
|
valid: [
|
|
{
|
|
filename: useCaseFile,
|
|
code: fs.readFileSync(useCaseFile, "utf8"),
|
|
options: [{ repoRoot }],
|
|
},
|
|
],
|
|
invalid: [],
|
|
});
|
|
});
|
|
|
|
it("fires when category is undeclared or manifest declaration is unused", () => {
|
|
const { repoRoot: rootA, useCaseFile: fileA } = makeFixture({
|
|
requiresConsent: ["analytics"],
|
|
useCaseBody: `export const checkDataUseCase = (consent) => async () => { if (!consent.isGranted("marketing")) throw new Error(); };`,
|
|
});
|
|
const { repoRoot: rootB, useCaseFile: fileB } = makeFixture({
|
|
requiresConsent: ["analytics"],
|
|
useCaseBody: `export const checkDataUseCase = () => async () => { return { ok: true }; };`,
|
|
});
|
|
tester.run("no-undeclared-consent-check", rule, {
|
|
valid: [],
|
|
invalid: [
|
|
{
|
|
filename: fileA,
|
|
code: fs.readFileSync(fileA, "utf8"),
|
|
options: [{ repoRoot: rootA }],
|
|
errors: [
|
|
{
|
|
messageId: "undeclared",
|
|
data: { category: "marketing", useCase: "checkData" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
filename: fileB,
|
|
code: fs.readFileSync(fileB, "utf8"),
|
|
options: [{ repoRoot: rootB }],
|
|
errors: [
|
|
{ messageId: "unusedDeclaration", data: { useCase: "checkData" } },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it("is a no-op for non-use-case files", () => {
|
|
const { repoRoot } = makeFixture({
|
|
requiresConsent: ["analytics"],
|
|
useCaseBody: `export const checkDataUseCase = (consent) => async () => { consent.isGranted("unknown"); };`,
|
|
});
|
|
const serviceFile = path.join(
|
|
repoRoot,
|
|
"packages",
|
|
"demo",
|
|
"src",
|
|
"application",
|
|
"services",
|
|
"data.service.ts",
|
|
);
|
|
fs.mkdirSync(path.dirname(serviceFile), { recursive: true });
|
|
fs.writeFileSync(
|
|
serviceFile,
|
|
`export function doWork(consent) { consent.isGranted("unknown"); }`,
|
|
);
|
|
tester.run("no-undeclared-consent-check", rule, {
|
|
valid: [
|
|
{
|
|
filename: serviceFile,
|
|
code: fs.readFileSync(serviceFile, "utf8"),
|
|
options: [{ repoRoot }],
|
|
},
|
|
],
|
|
invalid: [],
|
|
});
|
|
});
|
|
});
|