architecture-harness 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli/check.mjs +168 -6
- package/src/cli/registry-history.mjs +53 -0
- package/src/cli/registry-history.test.mjs +88 -0
- package/src/config.mjs +55 -0
- package/src/descriptions.mjs +7 -0
- package/src/gates/closed-set-writers.mjs +36 -0
- package/src/gates/closed-set-writers.test.mjs +46 -0
- package/src/gates/doc-claims.mjs +45 -0
- package/src/gates/doc-claims.test.mjs +33 -0
- package/src/gates/integration-imports.mjs +49 -0
- package/src/gates/integration-imports.test.mjs +35 -0
- package/src/gates/migration-numbers.mjs +33 -0
- package/src/gates/migration-numbers.test.mjs +35 -0
- package/src/gates/parity.mjs +163 -0
- package/src/gates/parity.test.mjs +108 -0
- package/src/gates/registry-literal.mjs +137 -0
- package/src/gates/registry-literal.test.mjs +68 -0
- package/src/gates/sql-identifiers.mjs +72 -9
- package/src/gates/sql-identifiers.test.mjs +70 -1
- package/src/gates/tenant-predicate.mjs +108 -58
- package/src/gates/tenant-predicate.test.mjs +61 -39
- package/src/gates/threshold-ratchet.mjs +46 -0
- package/src/gates/threshold-ratchet.test.mjs +60 -0
- package/templates/quality-thresholds.json +2 -2
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkDocClaims, lineCount, parseDocClaims } from "./doc-claims.mjs";
|
|
4
|
+
|
|
5
|
+
const doc = "# State\n\n```state-claim\nfile: src/big.ts\nlines: 3\n```\n\n```state-claim\r\nfile: src/gone.ts\r\nlines: 9\r\n```\n";
|
|
6
|
+
|
|
7
|
+
test("each fenced block yields its file and line count, and a block missing one is skipped", () => {
|
|
8
|
+
assert.deepEqual(parseDocClaims(doc), [{ file: "src/big.ts", lines: 3 }, { file: "src/gone.ts", lines: 9 }]);
|
|
9
|
+
assert.deepEqual(parseDocClaims("```state-claim\nfile: a.ts\n```\n"), []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("lines are counted the way wc -l counts them, for LF and CRLF alike", () => {
|
|
13
|
+
assert.equal(lineCount("a\nb\nc\n"), 3);
|
|
14
|
+
assert.equal(lineCount("a\r\nb\r\nc\r\n"), 3);
|
|
15
|
+
assert.equal(lineCount("a\nb\nc"), 3);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("a claim that is still true passes", () => {
|
|
19
|
+
const found = checkDocClaims("STATE.md", [{ file: "src/big.ts", lines: 3 }], new Map([["src/big.ts", "1\n2\n3\n"]]));
|
|
20
|
+
assert.deepEqual(found, []);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a claim whose count changed, or whose file is gone, fails", () => {
|
|
24
|
+
const found = checkDocClaims(
|
|
25
|
+
"STATE.md",
|
|
26
|
+
parseDocClaims(doc),
|
|
27
|
+
new Map([["src/big.ts", "1\n2\n3\n4\n"], ["src/gone.ts", null]]),
|
|
28
|
+
);
|
|
29
|
+
assert.deepEqual(found.map((problem) => problem.rule), ["stale-doc-claim", "stale-doc-claim"]);
|
|
30
|
+
assert.equal(found[0].path, "src/big.ts");
|
|
31
|
+
assert.equal(found[0].detail, "STATE.md claims 3 lines; the file now has 4");
|
|
32
|
+
assert.equal(found[1].detail, "STATE.md claims 9 lines; the file no longer exists");
|
|
33
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A test named for integration has to reach the thing it integrates with. One that imports
|
|
2
|
+
// only a test runner and the kernel builds its own subject and asserts about it, so it passes
|
|
3
|
+
// for every failure a user would meet.
|
|
4
|
+
|
|
5
|
+
/** Each `import ... from "<specifier>"`, so a name in a comment or a string is not one. */
|
|
6
|
+
const IMPORT_SPECIFIER = /^\s*import\s[\s\S]*?from\s+"([^"]+)"/gm;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A test inside a product root reaches its neighbours with `./`, so any relative import counts.
|
|
10
|
+
* One outside has to name the product root it climbs into, or import a product package.
|
|
11
|
+
* @param {string} source
|
|
12
|
+
* @param {string} file the repository-relative path
|
|
13
|
+
* @param {{productRoots: string[], productPackages: string[]}} options
|
|
14
|
+
* @returns {number}
|
|
15
|
+
*/
|
|
16
|
+
export function productImportCount(source, file, { productRoots, productPackages }) {
|
|
17
|
+
const inProduct = productRoots.some((root) => file.startsWith(`${root}/`));
|
|
18
|
+
const productPackage = productPackages.length > 0 ? new RegExp(productPackages.join("|")) : null;
|
|
19
|
+
let count = 0;
|
|
20
|
+
for (const [, specifier] of source.matchAll(IMPORT_SPECIFIER)) {
|
|
21
|
+
if (productPackage?.test(specifier)) count += 1;
|
|
22
|
+
else if (!specifier.startsWith(".")) continue;
|
|
23
|
+
else if (inProduct) count += 1;
|
|
24
|
+
else if (productRoots.some((root) => specifier.split("/").includes(root))) count += 1;
|
|
25
|
+
}
|
|
26
|
+
return count;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {{path: string, contents: string}[]} subjects every file the subject pattern matched
|
|
31
|
+
* @param {{productRoots: string[], productPackages: string[]}} options
|
|
32
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
33
|
+
*/
|
|
34
|
+
export function checkIntegrationImports(subjects, options) {
|
|
35
|
+
if (subjects.length === 0) {
|
|
36
|
+
return [{
|
|
37
|
+
path: "qc.config.json",
|
|
38
|
+
rule: "no-integration-tests",
|
|
39
|
+
detail: "no file matches integrationImports.subject, so this gate is checking nothing",
|
|
40
|
+
}];
|
|
41
|
+
}
|
|
42
|
+
return subjects
|
|
43
|
+
.filter(({ path, contents }) => productImportCount(contents, path, options) === 0)
|
|
44
|
+
.map(({ path }) => ({
|
|
45
|
+
path,
|
|
46
|
+
rule: "integration-without-product",
|
|
47
|
+
detail: "imports no product module, so it can only assert about subjects it declares itself",
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkIntegrationImports, productImportCount } from "./integration-imports.mjs";
|
|
4
|
+
|
|
5
|
+
const options = { productRoots: ["backend", "frontend"], productPackages: ["^@app/(?:domain|contracts)(?:/|$)"] };
|
|
6
|
+
|
|
7
|
+
test("an integration test that imports only the runner and the kernel fails", () => {
|
|
8
|
+
const contents = 'import { test } from "vitest";\nimport { ok } from "@app/kernel";\ntest("x", () => {});\n';
|
|
9
|
+
const problems = checkIntegrationImports([{ path: "packages/journeys/src/flow.interop.test.ts", contents }], options);
|
|
10
|
+
assert.equal(problems.length, 1);
|
|
11
|
+
assert.equal(problems[0].rule, "integration-without-product");
|
|
12
|
+
assert.equal(problems[0].path, "packages/journeys/src/flow.interop.test.ts");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("a product package, a climb into a product root, or any relative import inside one counts", () => {
|
|
16
|
+
assert.equal(productImportCount('import { Pin } from "@app/domain/pin";', "packages/x/a.live.test.ts", options), 1);
|
|
17
|
+
assert.equal(productImportCount('import { app } from "../../backend/src/main.js";', "packages/x/a.live.test.ts", options), 1);
|
|
18
|
+
assert.equal(productImportCount('import { route } from "./route.js";', "backend/src/pins/a.live.test.ts", options), 1);
|
|
19
|
+
assert.equal(productImportCount('import { helper } from "./helper.js";', "packages/x/a.live.test.ts", options), 0);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("a product root named in a comment or a string is not an import", () => {
|
|
23
|
+
const contents = '// see backend/src/main.ts\nimport { test } from "vitest";\nconst path = "@app/domain";\n';
|
|
24
|
+
assert.equal(checkIntegrationImports([{ path: "packages/x/a.interop.test.ts", contents }], options).length, 1);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("with no integration test at all the gate says it checks nothing", () => {
|
|
28
|
+
const problems = checkIntegrationImports([], options);
|
|
29
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["no-integration-tests"]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a test that reaches the product passes", () => {
|
|
33
|
+
const contents = 'import { test } from "vitest";\nimport { createPin } from "@app/domain";\n';
|
|
34
|
+
assert.deepEqual(checkIntegrationImports([{ path: "packages/x/a.interop.test.ts", contents }], options), []);
|
|
35
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Migration numbers are handed out by hand, so two branches can take the same one, or a rebase can
|
|
2
|
+
// drop one. Each numeric prefix is unique, and the run from the lowest to the highest has no gap.
|
|
3
|
+
|
|
4
|
+
const MIGRATION = /^(\d+)_.*\.sql$/;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {string[]} names the names in the migrations folder
|
|
8
|
+
* @param {string} dir the folder, as a problem names it
|
|
9
|
+
*/
|
|
10
|
+
export function checkMigrationNumbers(names, dir) {
|
|
11
|
+
const byNumber = new Map();
|
|
12
|
+
for (const name of names) {
|
|
13
|
+
const found = MIGRATION.exec(name);
|
|
14
|
+
if (found === null) continue;
|
|
15
|
+
const number = Number(found[1]);
|
|
16
|
+
byNumber.set(number, [...(byNumber.get(number) ?? []), name]);
|
|
17
|
+
}
|
|
18
|
+
const problems = [];
|
|
19
|
+
for (const [number, files] of byNumber) {
|
|
20
|
+
if (files.length > 1) {
|
|
21
|
+
problems.push({ path: dir, rule: "duplicate-migration-number", detail: `${files.sort().join(", ")} share number ${number}; give each migration its own` });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const numbers = [...byNumber.keys()].sort((a, b) => a - b);
|
|
25
|
+
const missing = [];
|
|
26
|
+
for (let number = numbers[0]; number < numbers.at(-1); number += 1) {
|
|
27
|
+
if (!byNumber.has(number)) missing.push(number);
|
|
28
|
+
}
|
|
29
|
+
if (missing.length > 0) {
|
|
30
|
+
problems.push({ path: dir, rule: "migration-number-gap", detail: `no migration holds number ${missing.join(", ")}; the numbers run from ${numbers[0]} to ${numbers.at(-1)} with no gap` });
|
|
31
|
+
}
|
|
32
|
+
return problems;
|
|
33
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkMigrationNumbers } from "./migration-numbers.mjs";
|
|
4
|
+
|
|
5
|
+
const DIR = "backend/src/drizzle";
|
|
6
|
+
|
|
7
|
+
test("contiguous prefixes pass, and a name that is not a migration is ignored", () => {
|
|
8
|
+
const names = ["0001_identity.sql", "0002_projects.sql", "0003_sites.sql", "meta", "README.md"];
|
|
9
|
+
assert.deepEqual(checkMigrationNumbers(names, DIR), []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("the numbers may start above one, as after a squash", () => {
|
|
13
|
+
assert.deepEqual(checkMigrationNumbers(["0040_base.sql", "0041_photos.sql"], DIR), []);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("a duplicate prefix fails and names both files", () => {
|
|
17
|
+
const problems = checkMigrationNumbers(["0001_identity.sql", "0002_projects.sql", "0002_sites.sql"], DIR);
|
|
18
|
+
assert.equal(problems.length, 1);
|
|
19
|
+
assert.equal(problems[0].rule, "duplicate-migration-number");
|
|
20
|
+
assert.equal(problems[0].path, DIR);
|
|
21
|
+
assert.match(problems[0].detail, /0002_projects\.sql/);
|
|
22
|
+
assert.match(problems[0].detail, /0002_sites\.sql/);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a gap fails and names the missing numbers", () => {
|
|
26
|
+
const problems = checkMigrationNumbers(["0001_identity.sql", "0002_projects.sql", "0005_sites.sql"], DIR);
|
|
27
|
+
assert.equal(problems.length, 1);
|
|
28
|
+
assert.equal(problems[0].rule, "migration-number-gap");
|
|
29
|
+
assert.match(problems[0].detail, /\b3\b.*\b4\b/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a different width is the same number", () => {
|
|
33
|
+
const problems = checkMigrationNumbers(["0001_identity.sql", "01_projects.sql"], DIR);
|
|
34
|
+
assert.equal(problems[0].rule, "duplicate-migration-number");
|
|
35
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// The two surfaces carry one architecture: a feature exists on both under one name, and a
|
|
2
|
+
// server slice has a client slice of the same stem. A divergence is legal only when the
|
|
3
|
+
// registry names it with its reason, and an entry that stops being true fails like a gap.
|
|
4
|
+
// A slice that only exists to satisfy this pairing is refused too, or the pairing proves nothing.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {{
|
|
8
|
+
* server: {name: string, slices: string[], routes: string[]}[],
|
|
9
|
+
* client: {name: string, slices: string[]}[],
|
|
10
|
+
* registry: {backendOnlyFeatures?: {name: string}[], frontendOnlyFeatures?: {name: string}[], slices?: Record<string, {stem: string, why?: string}[]>},
|
|
11
|
+
* }} surfaces
|
|
12
|
+
* @param {{server: string, client: string, registry: string, trigger?: string}} roots
|
|
13
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
14
|
+
*/
|
|
15
|
+
export function checkParity({ server, client, registry }, roots) {
|
|
16
|
+
const where = roots.registry;
|
|
17
|
+
const trigger = roots.trigger ?? "trigger.ts";
|
|
18
|
+
const backend = server.map((feature) => feature.name);
|
|
19
|
+
const frontend = client.map((feature) => feature.name);
|
|
20
|
+
const backendOnly = new Set((registry.backendOnlyFeatures ?? []).map((entry) => entry.name));
|
|
21
|
+
const frontendOnly = new Set((registry.frontendOnlyFeatures ?? []).map((entry) => entry.name));
|
|
22
|
+
const problems = [];
|
|
23
|
+
const push = (path, rule, detail) => problems.push({ path, rule, detail });
|
|
24
|
+
|
|
25
|
+
for (const name of backend) {
|
|
26
|
+
if (frontend.includes(name) || backendOnly.has(name)) continue;
|
|
27
|
+
push(`${roots.server}/${name}`, "one-sided-feature", `${name}: served by the api with no client feature, and not named in ${where}`);
|
|
28
|
+
}
|
|
29
|
+
for (const name of frontend) {
|
|
30
|
+
if (backend.includes(name) || frontendOnly.has(name)) continue;
|
|
31
|
+
push(`${roots.client}/${name}`, "one-sided-feature", `${name}: a client feature the api does not serve, and not named in ${where}`);
|
|
32
|
+
}
|
|
33
|
+
for (const name of [...backendOnly, ...frontendOnly]) {
|
|
34
|
+
if (backend.includes(name) && frontend.includes(name)) {
|
|
35
|
+
push(where, "stale-divergence", `${name}: listed as one-sided in ${where}, but both surfaces have it — delete the entry`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const drawnBy = new Map(client.map((feature) => [feature.name, new Set(feature.slices)]));
|
|
40
|
+
for (const feature of server.filter(({ name }) => drawnBy.has(name))) {
|
|
41
|
+
const { name } = feature;
|
|
42
|
+
const served = new Set(feature.slices);
|
|
43
|
+
const drawn = drawnBy.get(name);
|
|
44
|
+
const routes = [...new Set(feature.routes)].sort();
|
|
45
|
+
const excuses = new Map((registry.slices?.[name] ?? []).map((entry) => [entry.stem, entry.why ?? ""]));
|
|
46
|
+
const serverPath = `${roots.server}/${name}`;
|
|
47
|
+
const clientPath = `${roots.client}/${name}`;
|
|
48
|
+
|
|
49
|
+
for (const stem of routes) {
|
|
50
|
+
if (!served.has(stem)) push(serverPath, "one-sided-slice", `${name}/${stem}: route in ${trigger} has no backend slice implementation`);
|
|
51
|
+
}
|
|
52
|
+
for (const stem of routes) {
|
|
53
|
+
if (!drawn.has(stem) && !excuses.has(stem)) push(clientPath, "one-sided-slice", `${name}/${stem}: backend route has no frontend slice`);
|
|
54
|
+
}
|
|
55
|
+
for (const stem of [...served].sort()) {
|
|
56
|
+
if (drawn.has(stem) || excuses.has(stem)) continue;
|
|
57
|
+
push(clientPath, "one-sided-slice", `${name}/${stem}: a backend slice with no frontend slice of that name`);
|
|
58
|
+
}
|
|
59
|
+
for (const stem of [...drawn].sort()) {
|
|
60
|
+
if (served.has(stem) || excuses.has(stem)) continue;
|
|
61
|
+
push(serverPath, "one-sided-slice", `${name}/${stem}: a frontend slice with no backend slice/route of that name`);
|
|
62
|
+
}
|
|
63
|
+
for (const [stem, why] of excuses) {
|
|
64
|
+
const stale = (detail) => push(where, "stale-divergence", `${name}/${stem}: ${detail}`);
|
|
65
|
+
if (served.has(stem) && drawn.has(stem)) stale(`excused in ${where}, but both surfaces have it — delete the entry`);
|
|
66
|
+
else if (!served.has(stem) && !drawn.has(stem)) stale(`excused in ${where}, but neither surface has a slice of that name — delete the entry`);
|
|
67
|
+
else if (why.startsWith("Client-only") && !drawn.has(stem)) stale(`excused as client-only in ${where}, but no frontend slice exists`);
|
|
68
|
+
else if (why.startsWith("Backend-only") && !served.has(stem)) stale(`excused as backend-only in ${where}, but no backend slice exists`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return problems;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A string can hold "//" or a brace, so a comment is removed only outside a string.
|
|
75
|
+
const COMMENT_OR_STRING = /("(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*'|`(?:\\.|[^`\\])*`)|\/\*[\s\S]*?\*\/|\/\/[^\n]*/g;
|
|
76
|
+
const NO_OP_STATEMENT = /^(?:void 0|return|return undefined|return void 0)$/;
|
|
77
|
+
|
|
78
|
+
/** The index just past the bracket that closes the one at `open`, skipping strings. */
|
|
79
|
+
function closing(source, open) {
|
|
80
|
+
const pair = { "(": ")", "{": "}", "<": ">" }[source[open]];
|
|
81
|
+
let depth = 0;
|
|
82
|
+
for (let i = open; i < source.length; i += 1) {
|
|
83
|
+
const ch = source[i];
|
|
84
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
85
|
+
const end = source.indexOf(ch, i + 1);
|
|
86
|
+
if (end === -1) return source.length;
|
|
87
|
+
i = end;
|
|
88
|
+
} else if (ch === source[open]) depth += 1;
|
|
89
|
+
else if (ch === pair && (depth -= 1) === 0) return i + 1;
|
|
90
|
+
}
|
|
91
|
+
return source.length;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isNoOpBody(body) {
|
|
95
|
+
return body
|
|
96
|
+
.split(/[;\n]/)
|
|
97
|
+
.map((statement) => statement.trim())
|
|
98
|
+
.filter(Boolean)
|
|
99
|
+
.every((statement) => NO_OP_STATEMENT.test(statement));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** True for a function whose parameters open at `open` and whose body does nothing. */
|
|
103
|
+
function noOpFunctionAt(source, open, { arrow }) {
|
|
104
|
+
const after = source.slice(closing(source, open));
|
|
105
|
+
if (arrow) {
|
|
106
|
+
const head = /^\s*(?::[^=]*?)?=>\s*/.exec(after);
|
|
107
|
+
if (head === null) return false;
|
|
108
|
+
const rest = after.slice(head[0].length);
|
|
109
|
+
if (rest.startsWith("{")) return isNoOpBody(rest.slice(1, closing(rest, 0) - 1));
|
|
110
|
+
return /^(?:void 0|undefined)\s*(?:;|$|\n)/.test(rest);
|
|
111
|
+
}
|
|
112
|
+
const brace = after.indexOf("{");
|
|
113
|
+
return brace !== -1 && isNoOpBody(after.slice(brace + 1, closing(after, brace) - 1));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Each value export: true when it is a function that does nothing. A type export carries no
|
|
118
|
+
* behavior and is skipped; anything else, a constant, a class or a re-export, is real.
|
|
119
|
+
*/
|
|
120
|
+
function valueExports(source) {
|
|
121
|
+
const found = [];
|
|
122
|
+
for (const match of source.matchAll(/(?:^|[;\n])\s*export\s+/g)) {
|
|
123
|
+
const rest = source.slice(match.index + match[0].length);
|
|
124
|
+
if (/^(?:type|interface|declare)\b/.test(rest)) continue;
|
|
125
|
+
const declared = /^(?:default\s+)?(?:async\s+)?function\b\s*\*?\s*[\w$]*\s*(?:<[^>]*>\s*)?(?=\()/.exec(rest);
|
|
126
|
+
if (declared) {
|
|
127
|
+
found.push(noOpFunctionAt(rest, declared[0].length, { arrow: false }));
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const bound = /^(?:const|let|var)\s+[\w$]+\s*(?::[^=]+)?=\s*(?:async\s+)?/.exec(rest);
|
|
131
|
+
if (bound) {
|
|
132
|
+
const value = rest.slice(bound[0].length);
|
|
133
|
+
const expression = /^function\b\s*[\w$]*\s*(?=\()/.exec(value);
|
|
134
|
+
if (expression) found.push(noOpFunctionAt(value, expression[0].length, { arrow: false }));
|
|
135
|
+
else if (value.startsWith("(")) found.push(noOpFunctionAt(value, 0, { arrow: true }));
|
|
136
|
+
else if (/^[\w$]+\s*=>/.test(value)) found.push(noOpFunctionAt(`(${value.replace(/^([\w$]+)/, "$1)")}`, 0, { arrow: true }));
|
|
137
|
+
else found.push(false);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
found.push(false);
|
|
141
|
+
}
|
|
142
|
+
return found;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A slice file whose every value export is a function that does nothing: it pairs a name and
|
|
147
|
+
* carries no behavior. A file with no value export is left to the empty-block check.
|
|
148
|
+
* @param {{path: string, contents: string}[]} files
|
|
149
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
150
|
+
*/
|
|
151
|
+
export function placeholderSlices(files) {
|
|
152
|
+
const problems = [];
|
|
153
|
+
for (const { path, contents } of files) {
|
|
154
|
+
const exports = valueExports(contents.replace(COMMENT_OR_STRING, (whole, string) => string ?? ""));
|
|
155
|
+
if (exports.length === 0 || !exports.every(Boolean)) continue;
|
|
156
|
+
problems.push({
|
|
157
|
+
path,
|
|
158
|
+
rule: "placeholder-slice",
|
|
159
|
+
detail: "every export is a function that does nothing, so the slice pairs a name and carries no behavior — build the slice, or name the divergence in the parity registry",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return problems;
|
|
163
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkParity, placeholderSlices } from "./parity.mjs";
|
|
4
|
+
|
|
5
|
+
const roots = { server: "backend/src/features", client: "frontend/src/features", registry: "contracts/parity.json" };
|
|
6
|
+
|
|
7
|
+
function run({ server = [], client = [], registry = {} }) {
|
|
8
|
+
return checkParity({ server, client, registry }, roots);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const rules = (problems) => problems.map((problem) => problem.rule);
|
|
12
|
+
|
|
13
|
+
test("two surfaces that carry the same features and slices pass", () => {
|
|
14
|
+
const server = [{ name: "pins", slices: ["create-pin"], routes: ["create-pin"] }];
|
|
15
|
+
const client = [{ name: "pins", slices: ["create-pin"] }];
|
|
16
|
+
assert.deepEqual(run({ server, client }), []);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("a feature on one surface only fails in both directions", () => {
|
|
20
|
+
const problems = run({ server: [{ name: "billing", slices: [], routes: [] }], client: [{ name: "outbox", slices: [] }] });
|
|
21
|
+
assert.deepEqual(rules(problems), ["one-sided-feature", "one-sided-feature"]);
|
|
22
|
+
assert.equal(problems[0].path, "backend/src/features/billing");
|
|
23
|
+
assert.equal(problems[1].path, "frontend/src/features/outbox");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("a one-sided feature the registry names passes, and an entry both surfaces now have fails", () => {
|
|
27
|
+
const registry = {
|
|
28
|
+
backendOnlyFeatures: [{ name: "billing", why: "server only" }],
|
|
29
|
+
frontendOnlyFeatures: [{ name: "pins", why: "stale" }],
|
|
30
|
+
};
|
|
31
|
+
const server = [{ name: "billing", slices: [], routes: [] }, { name: "pins", slices: [], routes: [] }];
|
|
32
|
+
const problems = run({ server, client: [{ name: "pins", slices: [] }], registry });
|
|
33
|
+
assert.deepEqual(rules(problems), ["stale-divergence"]);
|
|
34
|
+
assert.equal(problems[0].path, "contracts/parity.json");
|
|
35
|
+
assert.match(problems[0].detail, /pins: listed as one-sided/);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a slice or a route with no counterpart fails, in both directions", () => {
|
|
39
|
+
const server = [{ name: "pins", slices: ["create-pin", "move-pin"], routes: ["create-pin", "drop-pin"] }];
|
|
40
|
+
const client = [{ name: "pins", slices: ["create-pin", "draw-pin"] }];
|
|
41
|
+
const details = run({ server, client }).map((problem) => problem.detail);
|
|
42
|
+
assert.deepEqual(details.sort(), [
|
|
43
|
+
"pins/draw-pin: a frontend slice with no backend slice/route of that name",
|
|
44
|
+
"pins/drop-pin: backend route has no frontend slice",
|
|
45
|
+
"pins/drop-pin: route in trigger.ts has no backend slice implementation",
|
|
46
|
+
"pins/move-pin: a backend slice with no frontend slice of that name",
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("a registry excuse covers a one-sided slice, and fails once it stops being true", () => {
|
|
51
|
+
const server = [{ name: "pins", slices: ["render", "both"], routes: [] }];
|
|
52
|
+
const client = [{ name: "pins", slices: ["both", "local"] }];
|
|
53
|
+
const registry = {
|
|
54
|
+
slices: {
|
|
55
|
+
pins: [
|
|
56
|
+
{ stem: "render", why: "Backend-only: a worker route." },
|
|
57
|
+
{ stem: "local", why: "Client-only: a draft." },
|
|
58
|
+
{ stem: "both", why: "Backend-only: stale." },
|
|
59
|
+
{ stem: "gone", why: "Backend-only: deleted." },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
const details = run({ server, client, registry }).map((problem) => problem.detail);
|
|
64
|
+
assert.deepEqual(details, [
|
|
65
|
+
"pins/both: excused in contracts/parity.json, but both surfaces have it — delete the entry",
|
|
66
|
+
"pins/gone: excused in contracts/parity.json, but neither surface has a slice of that name — delete the entry",
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("an excuse whose reason names the wrong side fails", () => {
|
|
71
|
+
const server = [{ name: "pins", slices: ["render"], routes: [] }];
|
|
72
|
+
const client = [{ name: "pins", slices: [] }];
|
|
73
|
+
const registry = { slices: { pins: [{ stem: "render", why: "Client-only: wrong side." }] } };
|
|
74
|
+
const problems = run({ server, client, registry });
|
|
75
|
+
assert.deepEqual(rules(problems), ["stale-divergence"]);
|
|
76
|
+
assert.match(problems[0].detail, /excused as client-only/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const slice = (contents, path = "frontend/src/features/tasks/slices/get-task.tsx") => [{ path, contents }];
|
|
80
|
+
|
|
81
|
+
test("a slice whose only export is a no-op fails as a placeholder, whatever its name", () => {
|
|
82
|
+
const problems = placeholderSlices(slice("// placeholder for parity\nexport function PLACEHOLDER(): void { void 0; }"));
|
|
83
|
+
assert.equal(problems.length, 1);
|
|
84
|
+
assert.equal(problems[0].rule, "placeholder-slice");
|
|
85
|
+
assert.equal(problems[0].path, "frontend/src/features/tasks/slices/get-task.tsx");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("an empty body, a bare return, and return undefined are no-ops too", () => {
|
|
89
|
+
for (const body of ["", "return;", "return undefined;", "void 0"]) {
|
|
90
|
+
assert.equal(placeholderSlices(slice(`export function stub() { ${body} }`)).length, 1, body);
|
|
91
|
+
}
|
|
92
|
+
assert.equal(placeholderSlices(slice("export const stub = () => {};")).length, 1);
|
|
93
|
+
assert.equal(placeholderSlices(slice("export const stub = (): void => void 0;")).length, 1);
|
|
94
|
+
assert.equal(placeholderSlices(slice("export async function stub(a: string): Promise<void> {\n return;\n}")).length, 1);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("a slice with one real export passes, even beside a no-op", () => {
|
|
98
|
+
const real = "export function GetTask(props: Props) {\n return <Task id={props.id} />;\n}";
|
|
99
|
+
assert.deepEqual(placeholderSlices(slice(real)), []);
|
|
100
|
+
assert.deepEqual(placeholderSlices(slice(`${real}\nexport function noop(): void { void 0; }`)), []);
|
|
101
|
+
assert.deepEqual(placeholderSlices(slice("export const LIMIT = 20;\nexport function noop() {}")), []);
|
|
102
|
+
assert.deepEqual(placeholderSlices(slice("export { getTask } from './get';\nexport function noop() {}")), []);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a file with no value export is not a placeholder, and a type export does not make one real", () => {
|
|
106
|
+
assert.deepEqual(placeholderSlices(slice("const local = 1;\n")), []);
|
|
107
|
+
assert.equal(placeholderSlices(slice("export type Row = { id: string };\nexport function noop() {}")).length, 1);
|
|
108
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// A number a registry owns is written in the docs as a token, never as the figure. A figure goes
|
|
2
|
+
// stale the day the registry moves, and nothing else would notice.
|
|
3
|
+
|
|
4
|
+
import { escape } from "../eslint/options.mjs";
|
|
5
|
+
import { globMatcher } from "../glob.mjs";
|
|
6
|
+
|
|
7
|
+
const TOKEN = /\{\{(?:q|ver|v):[^}]*\}\}/g;
|
|
8
|
+
// Words between a phrase and a number, at most. Past that the number belongs to another sentence.
|
|
9
|
+
const WINDOW = 6;
|
|
10
|
+
const NUMBER = /^(\d{1,3}(?:,\d{3})+|\d+(?:\.\d+)?)(%|[a-z]+)?$/;
|
|
11
|
+
const UNIT_SPELLINGS = {
|
|
12
|
+
percent: ["%", "percent"],
|
|
13
|
+
ms: ["ms", "millisecond", "milliseconds"],
|
|
14
|
+
s: ["s", "second", "seconds"],
|
|
15
|
+
px: ["px", "pixel", "pixels"],
|
|
16
|
+
lines: ["line", "lines"],
|
|
17
|
+
days: ["day", "days"],
|
|
18
|
+
months: ["month", "months"],
|
|
19
|
+
count: [],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const spellings = (unit) => UNIT_SPELLINGS[unit] ?? (unit ? [unit, unit.replace(/s$/, "")] : []);
|
|
23
|
+
|
|
24
|
+
/** Lowercase, with the punctuation around a word gone. A trailing `%` stays: it is the unit. */
|
|
25
|
+
const normal = (word) => word.toLowerCase().replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}%]+$/gu, "");
|
|
26
|
+
|
|
27
|
+
/** Each entry that states its phrases, with the units that are its own. */
|
|
28
|
+
function thresholdEntries(thresholds) {
|
|
29
|
+
return Object.entries(thresholds)
|
|
30
|
+
.filter(([, entry]) => typeof entry.value === "number" && entry.match?.length > 0)
|
|
31
|
+
.map(([key, entry]) => ({
|
|
32
|
+
key,
|
|
33
|
+
value: entry.value,
|
|
34
|
+
unit: entry.unit,
|
|
35
|
+
own: new Set(spellings(entry.unit)),
|
|
36
|
+
phrases: entry.match.map((phrase) => phrase.split(/\s+/).map(normal).filter(Boolean)),
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The unit a number carries: attached as `70%`, or the next word as `70 percent`. */
|
|
41
|
+
function unitOf(words, index, attached, known) {
|
|
42
|
+
if (attached) return attached;
|
|
43
|
+
const next = words[index + 1]?.norm;
|
|
44
|
+
return next !== undefined && known.has(next) ? next : "";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The word ranges where a phrase stands. */
|
|
48
|
+
function phraseSpans(words, phrases) {
|
|
49
|
+
const spans = [];
|
|
50
|
+
for (const phrase of phrases) {
|
|
51
|
+
for (let start = 0; start + phrase.length <= words.length; start += 1) {
|
|
52
|
+
if (phrase.every((part, offset) => words[start + offset].norm === part)) spans.push([start, start + phrase.length - 1]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return spans;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const near = (index, [start, end]) => (index > end ? index - end - 1 : start - index - 1) <= WINDOW;
|
|
59
|
+
|
|
60
|
+
function thresholdProblems(file, words, entries, known) {
|
|
61
|
+
const problems = [];
|
|
62
|
+
const spansOf = new Map(entries.map((entry) => [entry.key, phraseSpans(words, entry.phrases)]));
|
|
63
|
+
words.forEach((word, index) => {
|
|
64
|
+
const found = NUMBER.exec(word.norm);
|
|
65
|
+
if (found === null) return;
|
|
66
|
+
const value = Number(found[1].replaceAll(",", ""));
|
|
67
|
+
const unit = unitOf(words, index, found[2], known);
|
|
68
|
+
for (const entry of entries) {
|
|
69
|
+
if (entry.value !== value) continue;
|
|
70
|
+
// An attached suffix that is no unit at all, as in `70th`, is not the entry's number either.
|
|
71
|
+
if (unit && !entry.own.has(unit)) continue;
|
|
72
|
+
if (!spansOf.get(entry.key).some((span) => near(index, span))) continue;
|
|
73
|
+
problems.push({
|
|
74
|
+
path: `${file}:${word.line}`,
|
|
75
|
+
rule: "threshold-literal",
|
|
76
|
+
detail: `'${word.norm}' restates the '${entry.key}' threshold (${entry.value}${entry.unit ? ` ${entry.unit}` : ""}); write {{q:${entry.key}}}`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return problems;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Paragraphs as word lists, each word with its line. A blank line ends a paragraph. */
|
|
84
|
+
function paragraphs(text) {
|
|
85
|
+
const out = [];
|
|
86
|
+
let words = [];
|
|
87
|
+
text.split("\n").forEach((line, index) => {
|
|
88
|
+
if (line.trim() === "") {
|
|
89
|
+
if (words.length > 0) out.push(words);
|
|
90
|
+
words = [];
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
for (const [raw] of line.matchAll(/\S+/g)) {
|
|
94
|
+
const norm = normal(raw);
|
|
95
|
+
if (norm) words.push({ raw, norm, line: index + 1 });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
if (words.length > 0) out.push(words);
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function versionProblems(file, text, libraries) {
|
|
103
|
+
const problems = [];
|
|
104
|
+
for (const [id, library] of Object.entries(libraries)) {
|
|
105
|
+
if (typeof library?.label !== "string" || library.label.trim() === "") continue;
|
|
106
|
+
const label = escape(library.label.trim()).replace(/\s+/g, "\\s+");
|
|
107
|
+
for (const found of text.matchAll(new RegExp(`(?<![\\w.])${label}\\s+v?\\d+(?:\\.\\d+)*\\b`, "gi"))) {
|
|
108
|
+
const line = text.slice(0, found.index).split("\n").length;
|
|
109
|
+
problems.push({
|
|
110
|
+
path: `${file}:${line}`,
|
|
111
|
+
rule: "version-literal",
|
|
112
|
+
detail: `'${found[0].replace(/\s+/g, " ")}' restates the '${id}' version; write {{ver:${id}}} or {{v:${id}}}`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return problems;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param {{path: string, contents: string}[]} docs the markdown files, paths relative to the root
|
|
121
|
+
* @param {{thresholds: object, libraries: object, exempt: string[]}} registries the `gates` of the
|
|
122
|
+
* thresholds registry, the `libraries` of the versions registry, and the globs of files that may hold a figure
|
|
123
|
+
*/
|
|
124
|
+
export function checkRegistryLiteral(docs, { thresholds = {}, libraries = {}, exempt = [] }) {
|
|
125
|
+
const entries = thresholdEntries(thresholds);
|
|
126
|
+
const known = new Set([...Object.values(UNIT_SPELLINGS).flat(), ...entries.flatMap((entry) => [...entry.own])]);
|
|
127
|
+
const isExempt = globMatcher(exempt);
|
|
128
|
+
const problems = [];
|
|
129
|
+
for (const { path: file, contents } of docs) {
|
|
130
|
+
if (isExempt(file)) continue;
|
|
131
|
+
// A placeholder word, so a token still counts as one word of the window.
|
|
132
|
+
const text = contents.replace(/\r\n?/g, "\n").replace(TOKEN, "TOKEN");
|
|
133
|
+
for (const words of paragraphs(text)) problems.push(...thresholdProblems(file, words, entries, known));
|
|
134
|
+
problems.push(...versionProblems(file, text, libraries));
|
|
135
|
+
}
|
|
136
|
+
return problems;
|
|
137
|
+
}
|