From 7cfb78b99e16a696756c81fa746ad35ca70fa98c Mon Sep 17 00:00:00 2001 From: Danijel Martinek Date: Tue, 12 May 2026 23:49:12 +0200 Subject: [PATCH] feat(core-eslint): manifest AST parser for per-use-case attributes --- packages/core-eslint/rules/_manifest-ast.js | 76 +++++++++++++++++++ .../core-eslint/rules/_manifest-ast.test.js | 53 +++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 packages/core-eslint/rules/_manifest-ast.js create mode 100644 packages/core-eslint/rules/_manifest-ast.test.js diff --git a/packages/core-eslint/rules/_manifest-ast.js b/packages/core-eslint/rules/_manifest-ast.js new file mode 100644 index 0000000..54d4962 --- /dev/null +++ b/packages/core-eslint/rules/_manifest-ast.js @@ -0,0 +1,76 @@ +import fs from "node:fs"; +import { parse } from "@typescript-eslint/parser"; + +/** + * Parse a feature.manifest.ts file and extract per-use-case attributes. + * Walks the AST to find the `defineFeature({...} as const)` call expression + * and reads literal values from its argument object. + * + * Returns: { [useCaseName]: { mutates, audits[], publishes[], consumes[] } } + * Returns null if the file is missing or doesn't match the expected shape. + */ +export function parseManifestUseCases(manifestPath) { + let src; + try { + src = fs.readFileSync(manifestPath, "utf8"); + } catch { + return null; + } + let ast; + try { + ast = parse(src, { sourceType: "module", ecmaVersion: "latest", loc: false, range: false }); + } catch { + return null; + } + const defineCall = findDefineFeatureCall(ast); + if (!defineCall) return null; + const arg = unwrapAsConst(defineCall.arguments[0]); + if (!arg || arg.type !== "ObjectExpression") return null; + const useCasesProp = arg.properties.find( + (p) => p.type === "Property" && p.key.type === "Identifier" && p.key.name === "useCases", + ); + if (!useCasesProp || useCasesProp.value.type !== "ObjectExpression") return {}; + const result = {}; + for (const entry of useCasesProp.value.properties) { + if (entry.type !== "Property" || entry.value.type !== "ObjectExpression") continue; + const name = entry.key.type === "Identifier" ? entry.key.name : entry.key.value; + result[name] = extractUseCaseEntry(entry.value); + } + return result; +} + +function findDefineFeatureCall(ast) { + for (const node of ast.body) { + if (node.type !== "ExportNamedDeclaration" || !node.declaration) continue; + if (node.declaration.type !== "VariableDeclaration") continue; + for (const decl of node.declaration.declarations) { + const init = decl.init; + if (!init) continue; + if (init.type === "CallExpression" && init.callee.type === "Identifier" && init.callee.name === "defineFeature") { + return init; + } + } + } + return null; +} + +function unwrapAsConst(node) { + if (node && node.type === "TSAsExpression") return node.expression; + return node; +} + +function extractUseCaseEntry(objExpr) { + const entry = { mutates: false, audits: [], publishes: [], consumes: [] }; + for (const prop of objExpr.properties) { + if (prop.type !== "Property" || prop.key.type !== "Identifier") continue; + const key = prop.key.name; + if (key === "mutates" && prop.value.type === "Literal") { + entry.mutates = prop.value.value === true; + } else if ((key === "audits" || key === "publishes" || key === "consumes") && prop.value.type === "ArrayExpression") { + entry[key] = prop.value.elements + .filter((el) => el && el.type === "Literal" && typeof el.value === "string") + .map((el) => el.value); + } + } + return entry; +} diff --git a/packages/core-eslint/rules/_manifest-ast.test.js b/packages/core-eslint/rules/_manifest-ast.test.js new file mode 100644 index 0000000..f33227c --- /dev/null +++ b/packages/core-eslint/rules/_manifest-ast.test.js @@ -0,0 +1,53 @@ +import { describe, it, expect } from "vitest"; +import path from "node:path"; +import os from "node:os"; +import fs from "node:fs"; +import { parseManifestUseCases } from "./_manifest-ast.js"; + +function writeManifest(content) { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "manifest-ast-")); + const fp = path.join(dir, "feature.manifest.ts"); + fs.writeFileSync(fp, content); + return fp; +} + +describe("parseManifestUseCases", () => { + it("returns an empty object for a manifest with no useCases", () => { + const fp = writeManifest(`export const xManifest = defineFeature({ + name: "x", + requiredCores: [], + useCases: {}, + realtimeChannels: [], + jobs: [], +} as const);`); + expect(parseManifestUseCases(fp)).toEqual({}); + }); + + it("extracts per-use-case publishes/audits/consumes arrays", () => { + const fp = writeManifest(`export const authManifest = defineFeature({ + name: "auth", + requiredCores: [], + useCases: { + signIn: { mutates: false, audits: [], publishes: [], consumes: [] }, + signUp: { mutates: true, audits: ["user.created"], publishes: ["auth.signed-up"], consumes: [] }, + signOut: { mutates: true, audits: ["session.ended"], publishes: [], consumes: [] }, + }, + realtimeChannels: [], + jobs: [], +} as const);`); + expect(parseManifestUseCases(fp)).toEqual({ + signIn: { mutates: false, audits: [], publishes: [], consumes: [] }, + signUp: { mutates: true, audits: ["user.created"], publishes: ["auth.signed-up"], consumes: [] }, + signOut: { mutates: true, audits: ["session.ended"], publishes: [], consumes: [] }, + }); + }); + + it("returns null when file does not exist", () => { + expect(parseManifestUseCases("/nonexistent/manifest.ts")).toBeNull(); + }); + + it("returns null when the file has no defineFeature call", () => { + const fp = writeManifest(`export const x = 1;`); + expect(parseManifestUseCases(fp)).toBeNull(); + }); +});