feat(core-eslint): parseManifestFully — AST-based full manifest extraction
This commit is contained in:
@@ -74,3 +74,94 @@ function extractUseCaseEntry(objExpr) {
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a feature.manifest.ts and return the full manifest shape:
|
||||
* { name, requiredCores: string[], useCases: { [name]: {...} } }
|
||||
*
|
||||
* Same AST walking as parseManifestUseCases but additionally extracts
|
||||
* the top-level name + requiredCores fields. Returns null on parse failure.
|
||||
*/
|
||||
export function parseManifestFully(manifestPath) {
|
||||
// Reuse the AST parser used by parseManifestUseCases by inlining the
|
||||
// file-read + AST walk. We need access to the manifest's top-level
|
||||
// argument object beyond just useCases.
|
||||
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 = findDefineFeatureCallFromBody(ast);
|
||||
if (!defineCall) return null;
|
||||
const arg = unwrapAsConstNode(defineCall.arguments[0]);
|
||||
if (!arg || arg.type !== "ObjectExpression") return null;
|
||||
|
||||
let name = null;
|
||||
let requiredCores = [];
|
||||
let useCases = {};
|
||||
|
||||
for (const prop of arg.properties) {
|
||||
if (prop.type !== "Property" || prop.key.type !== "Identifier") continue;
|
||||
if (prop.key.name === "name" && prop.value.type === "Literal" && typeof prop.value.value === "string") {
|
||||
name = prop.value.value;
|
||||
} else if (prop.key.name === "requiredCores" && prop.value.type === "ArrayExpression") {
|
||||
requiredCores = prop.value.elements
|
||||
.filter((el) => el && el.type === "Literal" && typeof el.value === "string")
|
||||
.map((el) => el.value);
|
||||
} else if (prop.key.name === "useCases" && prop.value.type === "ObjectExpression") {
|
||||
for (const entry of prop.value.properties) {
|
||||
if (entry.type !== "Property" || entry.value.type !== "ObjectExpression") continue;
|
||||
const ucName = entry.key.type === "Identifier" ? entry.key.name : entry.key.value;
|
||||
useCases[ucName] = extractUseCaseEntryFromObj(entry.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (name === null) return null;
|
||||
return { name, requiredCores, useCases };
|
||||
}
|
||||
|
||||
// Helper aliases for the existing private functions — exposed under
|
||||
// different names to avoid touching existing code paths.
|
||||
function findDefineFeatureCallFromBody(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 unwrapAsConstNode(node) {
|
||||
if (node && node.type === "TSAsExpression") return node.expression;
|
||||
return node;
|
||||
}
|
||||
|
||||
function extractUseCaseEntryFromObj(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;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ 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";
|
||||
import { parseManifestUseCases, parseManifestFully } from "./_manifest-ast.js";
|
||||
|
||||
function writeManifest(content) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "manifest-ast-"));
|
||||
@@ -51,3 +51,50 @@ describe("parseManifestUseCases", () => {
|
||||
expect(parseManifestUseCases(fp)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseManifestFully", () => {
|
||||
it("returns name + requiredCores + useCases for a complete manifest", () => {
|
||||
// Use the writeManifest helper that already exists in the test file
|
||||
const fp = writeManifest(`export const authManifest = defineFeature({
|
||||
name: "auth",
|
||||
requiredCores: ["audit", "events"],
|
||||
useCases: {
|
||||
signIn: { mutates: false, audits: [], publishes: [], consumes: [] },
|
||||
signUp: { mutates: true, audits: ["user.created"], publishes: [], consumes: [] },
|
||||
},
|
||||
realtimeChannels: [],
|
||||
jobs: [],
|
||||
} as const);`);
|
||||
expect(parseManifestFully(fp)).toEqual({
|
||||
name: "auth",
|
||||
requiredCores: ["audit", "events"],
|
||||
useCases: {
|
||||
signIn: { mutates: false, audits: [], publishes: [], consumes: [] },
|
||||
signUp: { mutates: true, audits: ["user.created"], publishes: [], consumes: [] },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null when the manifest has no name", () => {
|
||||
const fp = writeManifest(`export const x = 1;`);
|
||||
expect(parseManifestFully(fp)).toBeNull();
|
||||
});
|
||||
|
||||
it("is not fooled by 'name:' inside a JSDoc comment (regex would false-match)", () => {
|
||||
const fp = writeManifest(`/**
|
||||
* Sample comment with name: "fake" embedded in it.
|
||||
*/
|
||||
export const realManifest = defineFeature({
|
||||
name: "real",
|
||||
requiredCores: [],
|
||||
useCases: {},
|
||||
realtimeChannels: [],
|
||||
jobs: [],
|
||||
} as const);`);
|
||||
expect(parseManifestFully(fp)).toEqual({
|
||||
name: "real",
|
||||
requiredCores: [],
|
||||
useCases: {},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user