Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/gate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export interface LayerContext {
export interface Consumed {
readonly artifact: string;
readonly from: string;
/**
* The runner passes the artifact if the producer left it and skips it if not, where a missing input otherwise makes
* the layer `not-run`. The layer says what a missing one means for what it judges (R92): a required law whose
* artifact is missing is `not-evaluated`, which ends the run.
*/
readonly optional?: boolean;
}

export interface Layer {
Expand Down
77 changes: 77 additions & 0 deletions src/laws/layer/catalog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from "vitest";
import type { Catalog, Law } from "../catalog/index.ts";
import { entryOf, yamlCatalog } from "./catalog.ts";

const law = (id: string, extra: Partial<Law> = {}): Law => ({
id,
title: "t",
family: "Types",
severity: "R",
classes: ["ts-library", "node-service"],
reader: { kinds: ["tl"], text: "tl: x" },
rules: [{ tool: "eslint", name: "no-explicit-any" }],
judged: "v0.1",
rationale: "r",
fix: "f",
...extra,
});

describe("G4a the laws layer takes its laws from the catalog's files", () => {
it("a law is its id, severity, first reader kind, classes and rules", () => {
expect(entryOf(law("W-01"))).toEqual({
id: "W-01",
severity: "R",
reader: "tl",
classes: ["ts-library", "node-service"],
judged: true,
rules: [{ tool: "eslint", name: "no-explicit-any" }],
});
expect(
entryOf(law("W-03", { severity: "A", reader: { kinds: ["at", "an"], text: "" } })),
).toMatchObject({
severity: "A",
reader: "at",
});
});

it("a law with no reader kind is not built yet", () => {
expect(entryOf(law("W-03", { reader: { kinds: [], text: "" } })).reader).toBe("tbd");
});

it("a law is judged when the catalog says v0.1 or that M3 gives it a reader, and not when it says no", () => {
expect(
["v0.1", "m3", "no"].map(
(judged) =>
entryOf(law("W-09", { judged: judged === "no" ? "no" : judged === "m3" ? "m3" : "v0.1" }))
.judged,
),
).toEqual([true, true, false]);
});
});

describe("G4a the catalog of the kit", () => {
it("maps every law the loader gives, in order, from the directory it is told", () => {
const seen: string[] = [];
const load = (dir: string): Catalog => {
seen.push(dir);
return { ok: true, laws: [law("W-01"), law("W-02")] };
};
expect(yamlCatalog(load, "somewhere").map((entry) => entry.id)).toEqual(["W-01", "W-02"]);
expect(seen).toEqual(["somewhere"]);
yamlCatalog(load);
expect(seen[1]?.endsWith("/laws")).toBe(true);
});

it("a catalog with a file that is not valid fails with each problem, not an empty list", () => {
const bad: Catalog = {
ok: false,
problems: [
{ file: "W-01.yaml", pointer: "/id", message: "is required" },
{ file: "W-02.yaml", pointer: "", message: "is not a map" },
],
};
expect(() => yamlCatalog(() => bad)).toThrow(
"the law catalog is invalid: W-01.yaml/id: is required; W-02.yaml: is not a map",
);
});
});
29 changes: 29 additions & 0 deletions src/laws/layer/catalog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CATALOG_DIR, loadCatalog } from "../../laws/catalog/index.ts";
import type { Law } from "../../laws/catalog/index.ts";
import type { LawEntry } from "../../laws/readers/types.ts";

/** Where the layer reads its laws: a function, so a test gives it a catalog of its own. */
export type LawCatalog = () => readonly LawEntry[];

/** A law as the layer takes it: what the catalog's file says, with a law that M3 gives a reader judged from now on. */
export const entryOf = (law: Law): LawEntry => ({
id: law.id,
severity: law.severity,
reader: law.reader.kinds[0] ?? "tbd",
classes: law.classes,
judged: law.judged !== "no",
rules: law.rules,
});

/** The kit's laws, from `laws/W-NN.yaml`: a catalog with a file that is not valid fails the layer, never an empty list. */
export function yamlCatalog(
load: typeof loadCatalog = loadCatalog,
dir: string = CATALOG_DIR,
): readonly LawEntry[] {
const loaded = load(dir);
if (!loaded.ok) {
const lines = loaded.problems.map((found) => `${found.file}${found.pointer}: ${found.message}`);
throw new Error(`the law catalog is invalid: ${lines.join("; ")}`);
}
return loaded.laws.map(entryOf);
}
38 changes: 38 additions & 0 deletions src/laws/layer/entries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { isRequired, lawsOfClass, toolsOf } from "./entries.ts";
import type { LawEntry } from "../../laws/readers/types.ts";

const law = (id: string, extra: Partial<LawEntry> = {}): LawEntry => ({
id,
severity: "R",
reader: "tl",
classes: [],
judged: true,
rules: [],
...extra,
});

describe("G4d which laws a class maps, and which it requires", () => {
it("a law with no class list is mapped by every class, and one with a list only by those classes", () => {
const laws = [law("W-01"), law("W-18", { classes: ["ts-library", "ui-components"] })];
expect(lawsOfClass(laws, "ts-library").map((found) => found.id)).toEqual(["W-01", "W-18"]);
expect(lawsOfClass(laws, "node-service").map((found) => found.id)).toEqual(["W-01"]);
});

it("a law is required when it is judged and the class maps it", () => {
expect(isRequired(law("W-01"), "node-service")).toBe(true);
expect(isRequired(law("W-03", { judged: false }), "node-service")).toBe(false);
expect(isRequired(law("W-18", { classes: ["ts-library"] }), "node-service")).toBe(false);
expect(isRequired(law("W-18", { classes: ["ts-library"] }), "ts-library")).toBe(true);
});

it("names the tools a law maps rules of, once each", () => {
const rules = [
{ tool: "eslint", name: "a" },
{ tool: "tsc", name: "b" },
{ tool: "eslint", name: "c" },
];
expect(toolsOf(law("W-13", { rules }))).toEqual(["eslint", "tsc"]);
expect(toolsOf(law("W-40"))).toEqual([]);
});
});
17 changes: 17 additions & 0 deletions src/laws/layer/entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { LawEntry } from "../../laws/readers/types.ts";

/** The laws a class maps: a law with no class list is mapped by every class (section 5.2). */
export const lawsOfClass = (laws: readonly LawEntry[], className: string): LawEntry[] =>
laws.filter((law) => law.classes.length === 0 || law.classes.includes(className));

/**
* A law is required when section 8.2 judges it and the class maps it (section 3.3). A mapped law that is not judged is
* counted and printed, never failing the run.
*/
export const isRequired = (law: LawEntry, className: string): boolean =>
law.judged && lawsOfClass([law], className).length > 0;

/** The tools a law maps rules of, in the order the law lists them, each once. */
export const toolsOf = (law: LawEntry): string[] => [
...new Set(law.rules.map((rule) => rule.tool)),
];
Loading
Loading