architecture-harness 0.4.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 +80 -0
- package/src/cli/check.mjs +535 -0
- package/src/cli/cli.test.mjs +120 -0
- package/src/cli/commit-attribution.mjs +68 -0
- package/src/cli/commit-attribution.test.mjs +47 -0
- package/src/cli/decisions.mjs +223 -0
- package/src/cli/doctor.mjs +135 -0
- package/src/cli/doctor.test.mjs +57 -0
- package/src/cli/init.mjs +98 -0
- package/src/cli/install-hooks.mjs +26 -0
- package/src/cli/qc.mjs +89 -0
- package/src/cli/scaffold.mjs +194 -0
- package/src/cli/sync-enforcement-map.mjs +28 -0
- package/src/cli/work-order-guard.mjs +0 -0
- package/src/cli/work-order-guard.test.mjs +79 -0
- package/src/cli/worktree-isolation.mjs +94 -0
- package/src/cli/worktree-isolation.test.mjs +82 -0
- package/src/config.mjs +338 -0
- package/src/config.test.mjs +59 -0
- package/src/decisions.mjs +198 -0
- package/src/decisions.test.mjs +183 -0
- package/src/descriptions.mjs +28 -0
- package/src/descriptions.test.mjs +27 -0
- package/src/enforcement-map.mjs +46 -0
- package/src/enforcement-map.test.mjs +60 -0
- package/src/eslint/index.mjs +37 -0
- package/src/eslint/options.mjs +53 -0
- package/src/eslint/preset.mjs +192 -0
- package/src/eslint/preset.test.mjs +96 -0
- package/src/eslint/rules/durable-idempotency-key.mjs +120 -0
- package/src/eslint/rules/external-service-only-in-adapter.mjs +52 -0
- package/src/eslint/rules/module-boundary.mjs +12 -0
- package/src/eslint/rules/name-the-pattern.mjs +111 -0
- package/src/eslint/rules/no-comment-paragraph.mjs +101 -0
- package/src/eslint/rules/no-cross-feature-internals.mjs +57 -0
- package/src/eslint/rules/no-number-in-comment.mjs +89 -0
- package/src/eslint/rules/no-offset-pagination.mjs +29 -0
- package/src/eslint/rules/no-orchestration-in-trigger.mjs +56 -0
- package/src/eslint/rules/no-promise-then.mjs +43 -0
- package/src/eslint/rules/no-promise-then.test.mjs +63 -0
- package/src/eslint/rules/no-raw-fetch.mjs +27 -0
- package/src/eslint/rules/no-status-literal.mjs +34 -0
- package/src/eslint/rules/no-supersession-trail.mjs +51 -0
- package/src/eslint/rules/scoped-repository.mjs +43 -0
- package/src/eslint/rules/signal-last-param.mjs +43 -0
- package/src/eslint/rules/storage-only-in-resource.mjs +52 -0
- package/src/eslint/rules/tenant-scoped-table.mjs +71 -0
- package/src/eslint/rules.test.mjs +627 -0
- package/src/gates/adr-format.mjs +87 -0
- package/src/gates/adr-format.test.mjs +54 -0
- package/src/gates/audit-append-only.mjs +34 -0
- package/src/gates/audit-append-only.test.mjs +36 -0
- package/src/gates/citations.mjs +93 -0
- package/src/gates/citations.test.mjs +115 -0
- package/src/gates/claimed-requirements.mjs +53 -0
- package/src/gates/claimed-requirements.test.mjs +60 -0
- package/src/gates/comment-style.mjs +76 -0
- package/src/gates/comment-style.test.mjs +46 -0
- package/src/gates/config-floor.mjs +68 -0
- package/src/gates/config-floor.test.mjs +52 -0
- package/src/gates/contract-compose.mjs +38 -0
- package/src/gates/contract-compose.test.mjs +40 -0
- package/src/gates/eight-blocks.mjs +223 -0
- package/src/gates/eight-blocks.test.mjs +359 -0
- package/src/gates/enforcement-map.mjs +62 -0
- package/src/gates/enforcement-map.test.mjs +41 -0
- package/src/gates/english-source.mjs +176 -0
- package/src/gates/english-source.test.mjs +122 -0
- package/src/gates/frontend-boundaries.mjs +154 -0
- package/src/gates/frontend-boundaries.test.mjs +72 -0
- package/src/gates/gate-tests.mjs +44 -0
- package/src/gates/gate-tests.test.mjs +41 -0
- package/src/gates/headless-sagas.mjs +54 -0
- package/src/gates/headless-sagas.test.mjs +71 -0
- package/src/gates/internal-routes.mjs +76 -0
- package/src/gates/internal-routes.test.mjs +53 -0
- package/src/gates/public-routes.mjs +67 -0
- package/src/gates/public-routes.test.mjs +38 -0
- package/src/gates/registry-agreement.mjs +37 -0
- package/src/gates/registry-agreement.test.mjs +36 -0
- package/src/gates/saga-tests.mjs +94 -0
- package/src/gates/saga-tests.test.mjs +92 -0
- package/src/gates/sql-identifiers.mjs +244 -0
- package/src/gates/sql-identifiers.test.mjs +197 -0
- package/src/gates/tenant-predicate.mjs +175 -0
- package/src/gates/tenant-predicate.test.mjs +161 -0
- package/src/gates/tenant-tables.mjs +65 -0
- package/src/gates/tenant-tables.test.mjs +56 -0
- package/src/gates/test-mirror.mjs +47 -0
- package/src/gates/test-mirror.test.mjs +31 -0
- package/src/index.mjs +9 -0
- package/templates/.jscpd.json +15 -0
- package/templates/CLAUDE.md +54 -0
- package/templates/decisions.examples.md +25 -0
- package/templates/docs/adr-template.md +58 -0
- package/templates/docs/architecture.md +179 -0
- package/templates/docs/decisions.md +72 -0
- package/templates/docs/enforcement.md +94 -0
- package/templates/docs/glossary.md +16 -0
- package/templates/docs/guards.md +23 -0
- package/templates/docs/pagination.md +113 -0
- package/templates/docs/performance.md +35 -0
- package/templates/docs/ui.md +32 -0
- package/templates/githooks/commit-msg +4 -0
- package/templates/githooks/pre-commit +18 -0
- package/templates/githooks/pre-push +6 -0
- package/templates/github/workflows/ci.yml +21 -0
- package/templates/qc.config.json +6 -0
- package/templates/quality-thresholds.json +114 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { defaults } from "./config.mjs";
|
|
4
|
+
import {
|
|
5
|
+
alreadyChecked,
|
|
6
|
+
audit,
|
|
7
|
+
isBareId,
|
|
8
|
+
isImmutable,
|
|
9
|
+
mergeCandidates,
|
|
10
|
+
parseRegister,
|
|
11
|
+
planProblems,
|
|
12
|
+
registerProblems,
|
|
13
|
+
renamePlan,
|
|
14
|
+
rewrite,
|
|
15
|
+
similarity,
|
|
16
|
+
squashPlan,
|
|
17
|
+
terms,
|
|
18
|
+
} from "./decisions.mjs";
|
|
19
|
+
|
|
20
|
+
test("a squash closes every gap and leaves a contiguous register alone", () => {
|
|
21
|
+
assert.deepEqual(
|
|
22
|
+
[...squashPlan(["ADR-0001", "ADR-0006", "ADR-0009", "QC-001", "QC-002"])],
|
|
23
|
+
[["ADR-0006", "ADR-0002"], ["ADR-0009", "ADR-0003"]],
|
|
24
|
+
);
|
|
25
|
+
assert.equal(squashPlan(["ADR-0001", "ADR-0002"]).size, 0);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("a squash keeps the width the series was written at", () => {
|
|
29
|
+
assert.deepEqual([...squashPlan(["QC-003"])], [["QC-003", "QC-001"]]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a series written at two widths settles on the wider one, so no id is truncated", () => {
|
|
33
|
+
assert.deepEqual([...squashPlan(["ADR-001", "ADR-0005"])], [["ADR-001", "ADR-0001"], ["ADR-0005", "ADR-0002"]]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("a run shifting down does not clobber an id it has not moved yet", () => {
|
|
37
|
+
const plan = squashPlan(["ADR-0001", "ADR-0002", "ADR-0003", "ADR-0005"]);
|
|
38
|
+
assert.equal(rewrite("ADR-0005 then ADR-0003", plan), "ADR-0004 then ADR-0003");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("a swap survives being written through itself", () => {
|
|
42
|
+
const plan = renamePlan("ADR-0001=ADR-0002,ADR-0002=ADR-0001");
|
|
43
|
+
assert.equal(rewrite("ADR-0001 and ADR-0002", plan), "ADR-0002 and ADR-0001");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a citation is rewritten only whole", () => {
|
|
47
|
+
assert.equal(rewrite("ADR-0001 ADR-00012", new Map([["ADR-0001", "ADR-0009"]])), "ADR-0009 ADR-00012");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("a plan naming an id the register does not define is refused", () => {
|
|
51
|
+
assert.deepEqual(planProblems(renamePlan("ADR-0099=ADR-0001"), new Set(["ADR-0001"])), [
|
|
52
|
+
"ADR-0099 is not a decision this repository defines",
|
|
53
|
+
"ADR-0001 is already taken by a decision this plan does not move",
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("a plan landing on an id that is not itself moving is refused", () => {
|
|
58
|
+
const problems = planProblems(renamePlan("ADR-0002=ADR-0001"), new Set(["ADR-0001", "ADR-0002"]));
|
|
59
|
+
assert.deepEqual(problems, ["ADR-0001 is already taken by a decision this plan does not move"]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("two ids landing on one are refused", () => {
|
|
63
|
+
const defined = new Set(["ADR-0001", "ADR-0002", "ADR-0003"]);
|
|
64
|
+
const problems = planProblems(renamePlan("ADR-0002=ADR-0009,ADR-0003=ADR-0009"), defined);
|
|
65
|
+
assert.deepEqual(problems, ["ADR-0002 and ADR-0003 would both become ADR-0009"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("a gap, an uncited decision and an undefined citation are each reported", () => {
|
|
69
|
+
const sites = new Map([
|
|
70
|
+
["ADR-0001", [{ file: "docs/decisions.md" }, { file: "src/a.ts" }]],
|
|
71
|
+
["ADR-0003", [{ file: "docs/decisions.md" }]],
|
|
72
|
+
["ADR-0400", [{ file: "src/b.ts" }]],
|
|
73
|
+
]);
|
|
74
|
+
const problems = registerProblems(new Set(["ADR-0001", "ADR-0003"]), sites, "docs/decisions.md");
|
|
75
|
+
assert.deepEqual(problems, [
|
|
76
|
+
{ text: "ADR: 1 gap(s) below ADR-0003", fatal: true },
|
|
77
|
+
{ text: "ADR-0003: defined and never cited — delete it, or cite it", fatal: false },
|
|
78
|
+
{ text: "ADR-0400: cited and not defined in docs/decisions.md", fatal: true },
|
|
79
|
+
]);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("a register with no gap, no orphan and no dangling citation reports nothing", () => {
|
|
83
|
+
const sites = new Map([["ADR-0001", [{ file: "docs/decisions.md" }, { file: "src/a.ts" }]]]);
|
|
84
|
+
assert.deepEqual(registerProblems(new Set(["ADR-0001"]), sites, "docs/decisions.md"), []);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("an id is a lookup, and a rename pair that contains one is not", () => {
|
|
88
|
+
assert.equal(isBareId("ADR-0001"), true);
|
|
89
|
+
assert.equal(isBareId("ADR-0047=ADR-0027"), false);
|
|
90
|
+
assert.equal(isBareId("see ADR-0001", undefined), false);
|
|
91
|
+
assert.equal(isBareId("--squash"), false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("an uncited decision is reported and does not fail the check", () => {
|
|
95
|
+
const sites = new Map([["ADR-0001", [{ file: "docs/decisions.md" }]]]);
|
|
96
|
+
const problems = registerProblems(new Set(["ADR-0001"]), sites, "docs/decisions.md");
|
|
97
|
+
assert.deepEqual(problems.map((problem) => problem.fatal), [false]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const REGISTER = `# Decisions
|
|
101
|
+
|
|
102
|
+
| id | decision | the rule it imposes |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| ADR-0001 | Keyset pagination only | \`offset\` and \`skip\` are rejected |
|
|
105
|
+
| ADR-0002 | Pagination is keyset | Never an offset, never a skip |
|
|
106
|
+
| ADR-0003 | One cloud | No provider abstraction layer |
|
|
107
|
+
`;
|
|
108
|
+
|
|
109
|
+
test("a register parses to rows, and a heading separator is not one", () => {
|
|
110
|
+
const rows = parseRegister(REGISTER, ["ADR"]);
|
|
111
|
+
assert.deepEqual(rows.map((row) => row.id), ["ADR-0001", "ADR-0002", "ADR-0003"]);
|
|
112
|
+
assert.equal(rows[2].decision, "One cloud");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("filler words are not what makes two decisions alike", () => {
|
|
116
|
+
assert.equal(terms("the one and only of it").size, 0);
|
|
117
|
+
assert.deepEqual([...terms("Keyset pagination only")], ["keyset", "pagination"]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("similarity is over the smaller set, so a terse decision is comparable to a wordy one", () => {
|
|
121
|
+
assert.equal(similarity(terms("keyset pagination"), terms("keyset pagination rejected skip offset")), 1);
|
|
122
|
+
assert.equal(similarity(new Set(), terms("anything")), 0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("two decisions saying the same thing are offered as a merge, and a third is not", () => {
|
|
126
|
+
const pairs = mergeCandidates(parseRegister(REGISTER, ["ADR"]));
|
|
127
|
+
assert.deepEqual(pairs.map((pair) => [pair.left, pair.right]), [["ADR-0001", "ADR-0002"]]);
|
|
128
|
+
assert.ok(pairs[0].shared.includes("keyset"));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("every decision whose rule a check already states is reported against that check", () => {
|
|
132
|
+
const rows = parseRegister(REGISTER, ["ADR"]);
|
|
133
|
+
const checks = [{ name: "qc/no-offset-pagination", description: "offset and skip are rejected" }];
|
|
134
|
+
assert.deepEqual(alreadyChecked(rows, checks), [
|
|
135
|
+
{ id: "ADR-0001", check: "qc/no-offset-pagination", score: 1 },
|
|
136
|
+
{ id: "ADR-0002", check: "qc/no-offset-pagination", score: 1 },
|
|
137
|
+
]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("a backticked identifier is what makes a rule distinctive, not noise to strip", () => {
|
|
141
|
+
assert.ok(terms("`offset` and `skip` are rejected").has("offset"));
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("a verdict follows how far a decision reaches", () => {
|
|
145
|
+
const sites = new Map([
|
|
146
|
+
["ADR-0001", [{ file: "docs/decisions.md" }, { file: "backend/src/a.ts" }, { file: "frontend/src/b.ts" }]],
|
|
147
|
+
["ADR-0002", [{ file: "backend/src/a.ts" }, { file: "backend/src/b.ts" }]],
|
|
148
|
+
["ADR-0003", [{ file: "backend/src/a.ts" }]],
|
|
149
|
+
]);
|
|
150
|
+
const { verdicts } = audit(parseRegister(REGISTER, ["ADR"]), sites, "docs/decisions.md");
|
|
151
|
+
assert.deepEqual(verdicts.map((entry) => [entry.id, entry.verdict]), [
|
|
152
|
+
["ADR-0001", "keep"],
|
|
153
|
+
["ADR-0002", "local?"],
|
|
154
|
+
["ADR-0003", "inline?"],
|
|
155
|
+
]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("a decision nothing cites is offered for abort", () => {
|
|
159
|
+
const { verdicts } = audit(parseRegister(REGISTER, ["ADR"]), new Map(), "docs/decisions.md");
|
|
160
|
+
assert.deepEqual([...new Set(verdicts.map((entry) => entry.verdict))], ["abort?"]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("a migration a ledger hashes is never rewritten, wherever it sits", () => {
|
|
164
|
+
const patterns = defaults.decisions.immutable;
|
|
165
|
+
for (const file of [
|
|
166
|
+
"backend/src/drizzle/0004_pins.sql",
|
|
167
|
+
"db/migrations/0001_init.sql",
|
|
168
|
+
"services/api/migration/0002_add_tenant.sql",
|
|
169
|
+
]) {
|
|
170
|
+
assert.equal(isImmutable(file, patterns), true, file);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("ordinary source and docs stay rewritable", () => {
|
|
175
|
+
const patterns = defaults.decisions.immutable;
|
|
176
|
+
for (const file of [
|
|
177
|
+
"backend/src/features/pins/schema.ts",
|
|
178
|
+
"docs/decisions.md",
|
|
179
|
+
"backend/src/drizzle/meta/_journal.json",
|
|
180
|
+
]) {
|
|
181
|
+
assert.equal(isImmutable(file, patterns), false, file);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// What each gate enforces, beside the gate, so a consumer generates its map instead of writing one.
|
|
2
|
+
|
|
3
|
+
export const gateDescriptions = Object.freeze({
|
|
4
|
+
"eight-blocks": "A feature keeps the anatomy it declares, and no block or slice is empty",
|
|
5
|
+
citations: "Every cited id and doc path resolves, and nothing references another repository",
|
|
6
|
+
"registry-agreement": "A value written twice agrees with the registry that owns it",
|
|
7
|
+
"sql-identifiers": "A query names a column a migration declares, and only its own feature's tables",
|
|
8
|
+
"tenant-predicate": "Every statement on a business table filters on the tenant",
|
|
9
|
+
"claimed-requirements": "A requirement a route claims names the test that proves it",
|
|
10
|
+
"public-routes": "The edge lets through exactly the routes the api serves without a session",
|
|
11
|
+
"internal-routes": "Every path a worker posts to is a route the api serves",
|
|
12
|
+
"headless-sagas": "The block that carries a workflow runs without a view layer",
|
|
13
|
+
"saga-tests": "Every workflow is named by a test that imports no view module",
|
|
14
|
+
"gate-tests": "A repository's own gates each ship a case that must fail",
|
|
15
|
+
"audit-append-only": "The audit log has no update or delete path in any repository",
|
|
16
|
+
"frontend-boundaries": "Platform remains a thin substrate and never holds feature domain logic or junk drawer directories",
|
|
17
|
+
"test-mirror": "Frontend tests mirror src paths 1:1, and no test is orphaned without a source file",
|
|
18
|
+
"enforcement-map": "Every rule names a check, and every check the kit runs is named",
|
|
19
|
+
"config-floor": "A check the kit ships on is switched off only by naming the decision that says why",
|
|
20
|
+
"english-source": "Comments, docs and rules are English; another language is data, declared beside its English variant",
|
|
21
|
+
"adr-format": "Every decision record carries its cost and the alternatives it rejected, not just its claim",
|
|
22
|
+
"comment-style": "A comment under infra/ is one line of why, never a paragraph, a banner or code",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/** The gates a consumer can switch on, so a caller need not import the defaults to find out. */
|
|
26
|
+
export function describedGates() {
|
|
27
|
+
return Object.keys(gateDescriptions);
|
|
28
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { gateDescriptions, describedGates } from "./descriptions.mjs";
|
|
4
|
+
import { defaults } from "./config.mjs";
|
|
5
|
+
import { rules } from "./eslint/index.mjs";
|
|
6
|
+
|
|
7
|
+
test("every gate the kit ships is described", () => {
|
|
8
|
+
const missing = Object.keys(defaults.gates).filter((name) => gateDescriptions[name] === undefined);
|
|
9
|
+
assert.deepEqual(missing, []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("a description that names a gate the kit does not ship fails", () => {
|
|
13
|
+
const stale = describedGates().filter((name) => defaults.gates[name] === undefined);
|
|
14
|
+
assert.deepEqual(stale, []);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("every lint rule the kit ships carries its own one-line description", () => {
|
|
18
|
+
const missing = Object.keys(defaults.rules).filter(
|
|
19
|
+
(name) => typeof rules[name]?.meta?.docs?.description !== "string",
|
|
20
|
+
);
|
|
21
|
+
assert.deepEqual(missing, []);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("a description is one line, so a generated map cannot grow a paragraph", () => {
|
|
25
|
+
const multiline = Object.entries(gateDescriptions).filter(([, text]) => text.includes("\n"));
|
|
26
|
+
assert.deepEqual(multiline, []);
|
|
27
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// QC-007's map, built from the rules and gates themselves so adding one cannot leave it stale.
|
|
2
|
+
|
|
3
|
+
import { enabled } from "./config.mjs";
|
|
4
|
+
import { gateDescriptions } from "./descriptions.mjs";
|
|
5
|
+
import { rules } from "./eslint/index.mjs";
|
|
6
|
+
|
|
7
|
+
export const OPEN = "<!-- generated: rule-to-check. -->";
|
|
8
|
+
export const CLOSE = "<!-- /generated -->";
|
|
9
|
+
// A repository may annotate the open marker with its own reminder (e.g. "pnpm codegen.") —
|
|
10
|
+
// matched by prefix, not by exact string, so that annotation does not break detection.
|
|
11
|
+
const OPEN_PATTERN = /<!--\s*generated:\s*rule-to-check\.[^>]*-->/;
|
|
12
|
+
|
|
13
|
+
function sentence(text) {
|
|
14
|
+
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @returns {string} a markdown table of every check in force, one row each. */
|
|
18
|
+
export function enforcementMap(config) {
|
|
19
|
+
const rows = [
|
|
20
|
+
...Object.keys(rules)
|
|
21
|
+
.filter((name) => enabled(config.rules, name))
|
|
22
|
+
.sort()
|
|
23
|
+
.map((name) => `| ${sentence(rules[name].meta.docs.description)} | \`qc/${name}\` |`),
|
|
24
|
+
...Object.keys(config.gates)
|
|
25
|
+
.filter((name) => enabled(config.gates, name))
|
|
26
|
+
.sort()
|
|
27
|
+
.map((name) => `| ${sentence(gateDescriptions[name])} | \`qc check\` (${name}) |`),
|
|
28
|
+
];
|
|
29
|
+
return `| Rule | Check |\n|---|---|\n${rows.join("\n")}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Splices the table between the markers, leaving whatever a repository wrote around them —
|
|
34
|
+
* including its own open marker's own text, which this keeps rather than resets to `OPEN`.
|
|
35
|
+
* @returns {string|null} null when no open marker is found, so a caller can tell that apart
|
|
36
|
+
* from a splice that ran and found nothing to change.
|
|
37
|
+
*/
|
|
38
|
+
export function withEnforcementMap(markdown, config) {
|
|
39
|
+
const found = OPEN_PATTERN.exec(markdown);
|
|
40
|
+
if (!found) return null;
|
|
41
|
+
const at = found.index;
|
|
42
|
+
const openText = found[0];
|
|
43
|
+
const after = markdown.indexOf(CLOSE, at);
|
|
44
|
+
const tail = after === -1 ? "" : markdown.slice(after + CLOSE.length);
|
|
45
|
+
return `${markdown.slice(0, at)}${openText}\n\n${enforcementMap(config)}\n\n${CLOSE}${tail}`;
|
|
46
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { enforcementMap, withEnforcementMap, OPEN, CLOSE } from "./enforcement-map.mjs";
|
|
7
|
+
import { checkEnforcementMap } from "./gates/enforcement-map.mjs";
|
|
8
|
+
import { defaults } from "./config.mjs";
|
|
9
|
+
import { rules } from "./eslint/index.mjs";
|
|
10
|
+
|
|
11
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
|
|
13
|
+
test("the template the kit ships passes the kit's own enforcement-map gate", async () => {
|
|
14
|
+
const template = await readFile(path.join(here, "../templates/docs/enforcement.md"), "utf8");
|
|
15
|
+
const filled = withEnforcementMap(template, allOn);
|
|
16
|
+
const problems = checkEnforcementMap(filled, Object.keys(rules), Object.keys(defaults.gates));
|
|
17
|
+
assert.deepEqual(problems, []);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Against an all-on config: a policy gate ships off, and the map states what is in force, so the
|
|
21
|
+
// two have to be asked separately -- does every check *reach* the map when enabled.
|
|
22
|
+
const allOn = {
|
|
23
|
+
...defaults,
|
|
24
|
+
gates: Object.fromEntries(Object.keys(defaults.gates).map((name) => [name, true])),
|
|
25
|
+
rules: Object.fromEntries(Object.keys(rules).map((name) => [name, true])),
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
test("a rule the kit adds reaches the map without anyone editing the template", () => {
|
|
29
|
+
const map = enforcementMap(allOn);
|
|
30
|
+
for (const name of Object.keys(rules)) assert.ok(map.includes(`qc/${name}`), name);
|
|
31
|
+
for (const name of Object.keys(defaults.gates)) assert.ok(map.includes(`(${name})`), name);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("a gate the kit ships off is not in the map, because it is not in force", () => {
|
|
35
|
+
const map = enforcementMap(defaults);
|
|
36
|
+
assert.ok(!map.includes("(english-source)"));
|
|
37
|
+
assert.ok(map.includes("(citations)"));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("a switched-off check is not listed, because it is not in force", () => {
|
|
41
|
+
const map = enforcementMap({ ...defaults, rules: { ...defaults.rules, "no-raw-fetch": false } });
|
|
42
|
+
assert.ok(!map.includes("qc/no-raw-fetch"));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("splicing keeps what a repository wrote around the markers", () => {
|
|
46
|
+
const filled = withEnforcementMap(`before\n\n${OPEN}\nstale\n${CLOSE}\n\nafter`, defaults);
|
|
47
|
+
assert.ok(filled.startsWith("before"));
|
|
48
|
+
assert.ok(filled.endsWith("after"));
|
|
49
|
+
assert.ok(!filled.includes("stale"));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a document with no markers returns null, distinct from a splice that changed nothing", () => {
|
|
53
|
+
assert.equal(withEnforcementMap("no markers here", defaults), null);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a repository's own annotation on the open marker survives the splice", () => {
|
|
57
|
+
const filled = withEnforcementMap(`before\n\n<!-- generated: rule-to-check. pnpm codegen. -->\nstale\n${CLOSE}\n\nafter`, defaults);
|
|
58
|
+
assert.ok(filled.includes("<!-- generated: rule-to-check. pnpm codegen. -->"));
|
|
59
|
+
assert.ok(!filled.includes("stale"));
|
|
60
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import durableIdempotencyKey from "./rules/durable-idempotency-key.mjs";
|
|
2
|
+
import externalServiceOnlyInAdapter from "./rules/external-service-only-in-adapter.mjs";
|
|
3
|
+
import noCrossFeatureInternals from "./rules/no-cross-feature-internals.mjs";
|
|
4
|
+
import noCommentParagraph from "./rules/no-comment-paragraph.mjs";
|
|
5
|
+
import nameThePattern from "./rules/name-the-pattern.mjs";
|
|
6
|
+
import noPromiseThen from "./rules/no-promise-then.mjs";
|
|
7
|
+
import noNumberInComment from "./rules/no-number-in-comment.mjs";
|
|
8
|
+
import noOffsetPagination from "./rules/no-offset-pagination.mjs";
|
|
9
|
+
import noOrchestrationInTrigger from "./rules/no-orchestration-in-trigger.mjs";
|
|
10
|
+
import noRawFetch from "./rules/no-raw-fetch.mjs";
|
|
11
|
+
import noStatusLiteral from "./rules/no-status-literal.mjs";
|
|
12
|
+
import noSupersessionTrail from "./rules/no-supersession-trail.mjs";
|
|
13
|
+
import scopedRepository from "./rules/scoped-repository.mjs";
|
|
14
|
+
import signalLastParam from "./rules/signal-last-param.mjs";
|
|
15
|
+
import storageOnlyInResource from "./rules/storage-only-in-resource.mjs";
|
|
16
|
+
import tenantScopedTable from "./rules/tenant-scoped-table.mjs";
|
|
17
|
+
|
|
18
|
+
export const rules = {
|
|
19
|
+
"durable-idempotency-key": durableIdempotencyKey,
|
|
20
|
+
"external-service-only-in-adapter": externalServiceOnlyInAdapter,
|
|
21
|
+
"no-cross-feature-internals": noCrossFeatureInternals,
|
|
22
|
+
"no-comment-paragraph": noCommentParagraph,
|
|
23
|
+
"name-the-pattern": nameThePattern,
|
|
24
|
+
"no-promise-then": noPromiseThen,
|
|
25
|
+
"no-number-in-comment": noNumberInComment,
|
|
26
|
+
"no-offset-pagination": noOffsetPagination,
|
|
27
|
+
"no-orchestration-in-trigger": noOrchestrationInTrigger,
|
|
28
|
+
"no-raw-fetch": noRawFetch,
|
|
29
|
+
"no-status-literal": noStatusLiteral,
|
|
30
|
+
"no-supersession-trail": noSupersessionTrail,
|
|
31
|
+
"scoped-repository": scopedRepository,
|
|
32
|
+
"signal-last-param": signalLastParam,
|
|
33
|
+
"storage-only-in-resource": storageOnlyInResource,
|
|
34
|
+
"tenant-scoped-table": tenantScopedTable,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default { rules };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// A rule reads its shape from its own options, so it stays a standard ESLint rule.
|
|
2
|
+
// The preset fills those options from qc.config.json; a rule used bare keeps the
|
|
3
|
+
// reference defaults, which is what makes adoption a no-op for a repo that matches.
|
|
4
|
+
|
|
5
|
+
/** Escape a literal path or module name for use inside a RegExp. */
|
|
6
|
+
export function escape(text) {
|
|
7
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Every path pattern below is forward-slash; `context.filename` is the OS separator, "\\" on Windows. */
|
|
11
|
+
export function filenameOf(context) {
|
|
12
|
+
return (context.filename ?? "").replaceAll("\\", "/");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** `platform/api-client.ts` -> matches any file ending in that path. */
|
|
16
|
+
export function pathSuffix(value) {
|
|
17
|
+
return new RegExp(`(?:^|/)${escape(value)}$`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** `backend/src/infrastructure/db/` -> matches any file under that directory. */
|
|
21
|
+
export function pathPrefix(value) {
|
|
22
|
+
return new RegExp(`(?:^|/)${escape(value.replace(/\/$/, ""))}/`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// A glob with `*` segments, e.g. frontend features' trigger file, at any extension.
|
|
26
|
+
export function pathGlob(value) {
|
|
27
|
+
const body = value.split("/").map((part) => (part === "*" ? "[^/]+" : escape(part))).join("/");
|
|
28
|
+
return new RegExp(`(?:^|/)${body}\\.[cm]?[jt]sx?$`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function moduleGroup(names) {
|
|
32
|
+
return new RegExp(`^(?:${names.map(escape).join("|")})(?:/|$)`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `@aws-sdk` -> matches any package published under that npm scope. */
|
|
36
|
+
export function scopeGroup(scopes) {
|
|
37
|
+
return new RegExp(`^(?:${scopes.map(escape).join("|")})/`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function suffixGroup(names) {
|
|
41
|
+
return new RegExp(`(?:${names.map(escape).join("|")})$`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const TEST_FILE = /\.test\.[cm]?[jt]sx?$/;
|
|
45
|
+
|
|
46
|
+
/** Options are optional everywhere: one object, every key defaulted by the rule. */
|
|
47
|
+
export function schemaOf(properties) {
|
|
48
|
+
return [{ type: "object", properties, additionalProperties: false }];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function optionsOf(context) {
|
|
52
|
+
return context.options[0] ?? {};
|
|
53
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// The lint rule is the architecture. docs/enforcement.md.
|
|
2
|
+
// Thresholds are imported, never restated — quality-thresholds.json is their one home.
|
|
3
|
+
|
|
4
|
+
import qc from "./index.mjs";
|
|
5
|
+
import { enabled, load, thresholds } from "../config.mjs";
|
|
6
|
+
|
|
7
|
+
/** Options each rule needs, derived from one config so a name is written once. */
|
|
8
|
+
function ruleOptions(config) {
|
|
9
|
+
return {
|
|
10
|
+
"no-comment-paragraph": {
|
|
11
|
+
doc: config.enforcement ?? "docs/enforcement.md",
|
|
12
|
+
decisions: config.docs.decisions,
|
|
13
|
+
maxLines: config.comments.maxLines,
|
|
14
|
+
},
|
|
15
|
+
"name-the-pattern": { maxWords: config.patterns.maxWords, extra: config.patterns.extra },
|
|
16
|
+
"no-number-in-comment": { prefixes: config.citations.prefixes, external: config.citations.external, registry: config.thresholds },
|
|
17
|
+
"no-cross-feature-internals": { featureDir: config.featureDir, publicFile: config.publicFile },
|
|
18
|
+
"no-raw-fetch": { client: config.apiClient },
|
|
19
|
+
"no-promise-then": { allow: config.promise.allow },
|
|
20
|
+
"no-orchestration-in-trigger": { presenters: config.presenters },
|
|
21
|
+
"scoped-repository": { suffixes: config.scopedSuffixes },
|
|
22
|
+
"storage-only-in-resource": {
|
|
23
|
+
modules: config.storage.modules,
|
|
24
|
+
resourceFiles: config.storage.resourceFiles,
|
|
25
|
+
driverBinding: config.storage.driverBinding,
|
|
26
|
+
},
|
|
27
|
+
"external-service-only-in-adapter": {
|
|
28
|
+
scopes: config.externalServices.scopes,
|
|
29
|
+
modules: config.externalServices.modules,
|
|
30
|
+
allowedFiles: config.externalServices.allowedFiles,
|
|
31
|
+
allowedPaths: config.externalServices.allowedPaths,
|
|
32
|
+
},
|
|
33
|
+
"tenant-scoped-table": {
|
|
34
|
+
column: config.tenant.column,
|
|
35
|
+
tableFactory: config.tenant.tableFactory,
|
|
36
|
+
indexFactory: config.tenant.indexFactory,
|
|
37
|
+
},
|
|
38
|
+
"durable-idempotency-key": {
|
|
39
|
+
keys: config.idempotency.keys,
|
|
40
|
+
volatile: config.idempotency.volatile,
|
|
41
|
+
ledgerKey: config.idempotency.ledgerKey,
|
|
42
|
+
throwawayLedgers: config.idempotency.throwawayLedgers,
|
|
43
|
+
},
|
|
44
|
+
"no-supersession-trail": {},
|
|
45
|
+
"signal-last-param": {},
|
|
46
|
+
"no-offset-pagination": {},
|
|
47
|
+
"no-status-literal": {},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function entries(config, names) {
|
|
52
|
+
const options = ruleOptions(config);
|
|
53
|
+
const out = {};
|
|
54
|
+
for (const name of names) {
|
|
55
|
+
if (!enabled(config.rules, name)) continue;
|
|
56
|
+
const given = options[name];
|
|
57
|
+
out[`qc/${name}`] = given && Object.keys(given).length > 0 ? ["error", given] : "error";
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Rules that apply to every file, on both surfaces. */
|
|
63
|
+
const UNIVERSAL = [
|
|
64
|
+
"durable-idempotency-key",
|
|
65
|
+
"no-supersession-trail",
|
|
66
|
+
"no-comment-paragraph",
|
|
67
|
+
"name-the-pattern",
|
|
68
|
+
"no-number-in-comment",
|
|
69
|
+
"no-promise-then",
|
|
70
|
+
"no-cross-feature-internals",
|
|
71
|
+
"no-offset-pagination",
|
|
72
|
+
"no-orchestration-in-trigger",
|
|
73
|
+
"no-status-literal",
|
|
74
|
+
"scoped-repository",
|
|
75
|
+
// Not a layered-architecture concern like the SERVER rules below — a worker or a
|
|
76
|
+
// composition root outside backend/src needs this exactly as much as backend/src does.
|
|
77
|
+
"external-service-only-in-adapter",
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
/** Rules that only make sense where the server's storage and ports live. */
|
|
81
|
+
const SERVER = ["storage-only-in-resource", "tenant-scoped-table", "signal-last-param"];
|
|
82
|
+
|
|
83
|
+
/** Rules that only make sense in the browser. */
|
|
84
|
+
const CLIENT = ["no-raw-fetch", "storage-only-in-resource"];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The architecture as a flat config. Spread it, then append your own blocks.
|
|
88
|
+
*
|
|
89
|
+
* @param {object} [options]
|
|
90
|
+
* @param {string} [options.root] repository root; defaults to the working directory
|
|
91
|
+
* @param {object} [options.plugins] tseslint, boundaries and sonarjs, when you want those blocks
|
|
92
|
+
*/
|
|
93
|
+
export function preset(options = {}) {
|
|
94
|
+
const config = options.config ?? load(options.root);
|
|
95
|
+
const gates = thresholds(config);
|
|
96
|
+
const max = (id) => gates[id]?.value;
|
|
97
|
+
const { tseslint, boundaries, sonarjs } = options.plugins ?? {};
|
|
98
|
+
|
|
99
|
+
const blocks = [{ ignores: config.ignores }];
|
|
100
|
+
|
|
101
|
+
const base = {
|
|
102
|
+
files: ["**/*.{ts,tsx,mjs}"],
|
|
103
|
+
plugins: { qc, ...(tseslint ? { "@typescript-eslint": tseslint } : {}), ...(sonarjs ? { sonarjs } : {}) },
|
|
104
|
+
rules: {
|
|
105
|
+
...(tseslint ? { "@typescript-eslint/no-explicit-any": "error" } : {}),
|
|
106
|
+
...(sonarjs
|
|
107
|
+
? {
|
|
108
|
+
"sonarjs/no-identical-functions": "error",
|
|
109
|
+
"sonarjs/no-duplicated-branches": "error",
|
|
110
|
+
"sonarjs/no-identical-conditions": "error",
|
|
111
|
+
"sonarjs/no-all-duplicated-branches": "error",
|
|
112
|
+
}
|
|
113
|
+
: {}),
|
|
114
|
+
...(max("filelength") ? { "max-lines": ["error", { max: max("filelength"), skipBlankLines: true, skipComments: true }] } : {}),
|
|
115
|
+
...(max("funclength")
|
|
116
|
+
? { "max-lines-per-function": ["error", { max: max("funclength"), skipBlankLines: true, skipComments: true }] }
|
|
117
|
+
: {}),
|
|
118
|
+
...(max("nesting") ? { "max-depth": ["error", max("nesting")] } : {}),
|
|
119
|
+
...entries(config, UNIVERSAL),
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
if (options.languageOptions) base.languageOptions = options.languageOptions;
|
|
123
|
+
blocks.push(base);
|
|
124
|
+
|
|
125
|
+
if (config.layers && boundaries) {
|
|
126
|
+
blocks.push({
|
|
127
|
+
files: config.layers.include,
|
|
128
|
+
plugins: { boundaries },
|
|
129
|
+
settings: {
|
|
130
|
+
"boundaries/elements": config.layers.elements,
|
|
131
|
+
"boundaries/include": config.layers.include,
|
|
132
|
+
},
|
|
133
|
+
rules: {
|
|
134
|
+
"boundaries/element-types": [
|
|
135
|
+
"error",
|
|
136
|
+
{
|
|
137
|
+
default: "disallow",
|
|
138
|
+
rules: Object.entries(config.layers.allow).map(([from, allow]) => ({ from, allow })),
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
"boundaries/external": ["error", { default: "allow", rules: config.layers.external }],
|
|
142
|
+
...entries(config, SERVER),
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
} else if (config.layers) {
|
|
146
|
+
blocks.push({ files: config.layers.include, rules: entries(config, SERVER) });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (config.clientFiles) {
|
|
150
|
+
blocks.push({ files: config.clientFiles, rules: entries(config, CLIENT) });
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (config.frontendLayers && boundaries) {
|
|
154
|
+
blocks.push({
|
|
155
|
+
files: config.frontendLayers.include,
|
|
156
|
+
plugins: { boundaries },
|
|
157
|
+
settings: {
|
|
158
|
+
"boundaries/elements": config.frontendLayers.elements,
|
|
159
|
+
"boundaries/include": config.frontendLayers.include,
|
|
160
|
+
},
|
|
161
|
+
rules: {
|
|
162
|
+
"boundaries/element-types": [
|
|
163
|
+
"error",
|
|
164
|
+
{
|
|
165
|
+
default: "disallow",
|
|
166
|
+
rules: Object.entries(config.frontendLayers.allow).map(([from, allow]) => ({ from, allow })),
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
...(config.frontendLayers.external
|
|
170
|
+
? { "boundaries/external": ["error", { default: "allow", rules: config.frontendLayers.external }] }
|
|
171
|
+
: {}),
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Gates and rules are tooling: they name the vocabulary they ban, so they trip
|
|
177
|
+
// their own checks. Fixtures still prove each rule fails on real code.
|
|
178
|
+
blocks.push({
|
|
179
|
+
files: config.toolingFiles ?? ["scripts/**/*.mjs", "**/*.test.{ts,tsx,mjs}"],
|
|
180
|
+
rules: {
|
|
181
|
+
...Object.fromEntries(Object.keys(qc.rules).map((name) => [`qc/${name}`, "off"])),
|
|
182
|
+
...(sonarjs ? { "sonarjs/no-identical-functions": "off" } : {}),
|
|
183
|
+
"max-lines": "off",
|
|
184
|
+
"max-lines-per-function": "off",
|
|
185
|
+
...(boundaries ? { "boundaries/external": "off", "boundaries/element-types": "off" } : {}),
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return blocks;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export default preset;
|