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,359 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { checkFeatureAnatomy, checkNoEmptyBlock } from "./eight-blocks.mjs";
|
|
4
|
+
|
|
5
|
+
test("a complete feature passes", () => {
|
|
6
|
+
const problems = checkFeatureAnatomy([
|
|
7
|
+
{ feature: "pins", files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts"] },
|
|
8
|
+
]);
|
|
9
|
+
assert.deepEqual(problems, []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("a missing required block fails and names it", () => {
|
|
13
|
+
const problems = checkFeatureAnatomy([
|
|
14
|
+
{ feature: "pins", files: ["index.ts", "trigger.ts", "pipeline.ts"] },
|
|
15
|
+
]);
|
|
16
|
+
assert.equal(problems.length, 1);
|
|
17
|
+
assert.equal(problems[0].rule, "missing-block");
|
|
18
|
+
assert.equal(problems[0].detail, "resource");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("a file that is not one of the eight blocks fails", () => {
|
|
22
|
+
const problems = checkFeatureAnatomy([
|
|
23
|
+
{
|
|
24
|
+
feature: "pins",
|
|
25
|
+
files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts", "helpers.ts"],
|
|
26
|
+
},
|
|
27
|
+
]);
|
|
28
|
+
assert.equal(problems.length, 1);
|
|
29
|
+
assert.equal(problems[0].rule, "unknown-block");
|
|
30
|
+
assert.equal(problems[0].detail, "helpers.ts");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("a subfolder fails — one block is one file", () => {
|
|
34
|
+
const problems = checkFeatureAnatomy([
|
|
35
|
+
{
|
|
36
|
+
feature: "pins",
|
|
37
|
+
files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts", "lib/util.ts"],
|
|
38
|
+
},
|
|
39
|
+
]);
|
|
40
|
+
assert.ok(problems.some((p) => p.rule === "no-subfolder"));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("test files are not blocks and do not fail the anatomy", () => {
|
|
44
|
+
const problems = checkFeatureAnatomy([
|
|
45
|
+
{
|
|
46
|
+
feature: "pins",
|
|
47
|
+
files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts", "pipeline.test.ts"],
|
|
48
|
+
},
|
|
49
|
+
]);
|
|
50
|
+
assert.deepEqual(problems, []);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("optional blocks are allowed but never required", () => {
|
|
54
|
+
const problems = checkFeatureAnatomy([
|
|
55
|
+
{
|
|
56
|
+
feature: "photos",
|
|
57
|
+
files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts", "ledger.ts", "record.ts"],
|
|
58
|
+
},
|
|
59
|
+
]);
|
|
60
|
+
assert.deepEqual(problems, []);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("a block whose file holds only comments is an empty block", () => {
|
|
64
|
+
const problems = checkNoEmptyBlock([
|
|
65
|
+
{ path: "features/pins/ledger.ts", contents: "// TODO\n\n" },
|
|
66
|
+
]);
|
|
67
|
+
assert.equal(problems.length, 1);
|
|
68
|
+
assert.equal(problems[0].rule, "empty-block");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("a block with real code is not empty", () => {
|
|
72
|
+
const problems = checkNoEmptyBlock([
|
|
73
|
+
{ path: "features/pins/ledger.ts", contents: "// docs/architecture.md\nexport const ledger = {};\n" },
|
|
74
|
+
]);
|
|
75
|
+
assert.deepEqual(problems, []);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a valid bounded vertical slice feature passes", () => {
|
|
79
|
+
const problems = checkFeatureAnatomy([
|
|
80
|
+
{
|
|
81
|
+
feature: "pins",
|
|
82
|
+
files: [
|
|
83
|
+
"index.ts",
|
|
84
|
+
"schema.ts",
|
|
85
|
+
"trigger.ts",
|
|
86
|
+
"slices/create-pin.ts",
|
|
87
|
+
"slices/move-pin.ts",
|
|
88
|
+
"shared/queries.ts",
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
]);
|
|
92
|
+
assert.deepEqual(problems, []);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("a slice feature with a disallowed subfolder fails", () => {
|
|
96
|
+
const problems = checkFeatureAnatomy([
|
|
97
|
+
{
|
|
98
|
+
feature: "pins",
|
|
99
|
+
files: [
|
|
100
|
+
"index.ts",
|
|
101
|
+
"schema.ts",
|
|
102
|
+
"trigger.ts",
|
|
103
|
+
"slices/create-pin.ts",
|
|
104
|
+
"helpers/utils.ts",
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
108
|
+
assert.equal(problems.length, 1);
|
|
109
|
+
assert.equal(problems[0].rule, "disallowed-subfolder");
|
|
110
|
+
assert.equal(problems[0].detail, "helpers/utils.ts");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a slice feature with nested sub-subfolder fails", () => {
|
|
114
|
+
const problems = checkFeatureAnatomy([
|
|
115
|
+
{
|
|
116
|
+
feature: "pins",
|
|
117
|
+
files: [
|
|
118
|
+
"index.ts",
|
|
119
|
+
"schema.ts",
|
|
120
|
+
"trigger.ts",
|
|
121
|
+
"slices/pin/create.ts",
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
]);
|
|
125
|
+
assert.ok(problems.some((p) => p.rule === "disallowed-subfolder" && p.detail === "slices/pin/create.ts"));
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("a slice feature missing schema fails", () => {
|
|
129
|
+
const problems = checkFeatureAnatomy([
|
|
130
|
+
{
|
|
131
|
+
feature: "pins",
|
|
132
|
+
files: [
|
|
133
|
+
"index.ts",
|
|
134
|
+
"trigger.ts",
|
|
135
|
+
"slices/create-pin.ts",
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
]);
|
|
139
|
+
assert.ok(problems.some((p) => p.rule === "missing-block" && p.detail === "schema"));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("an empty slice file is caught by checkNoEmptyBlock", () => {
|
|
143
|
+
const problems = checkNoEmptyBlock([
|
|
144
|
+
{ path: "features/pins/slices/create-pin.ts", contents: "// empty slice\n\n" },
|
|
145
|
+
]);
|
|
146
|
+
assert.equal(problems.length, 1);
|
|
147
|
+
assert.equal(problems[0].rule, "empty-block");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("a slice feature with nested test file fails", () => {
|
|
151
|
+
const problems = checkFeatureAnatomy([
|
|
152
|
+
{
|
|
153
|
+
feature: "pins",
|
|
154
|
+
files: [
|
|
155
|
+
"index.ts",
|
|
156
|
+
"schema.ts",
|
|
157
|
+
"trigger.ts",
|
|
158
|
+
"slices/create-pin.ts",
|
|
159
|
+
"slices/pin/create.test.ts",
|
|
160
|
+
],
|
|
161
|
+
},
|
|
162
|
+
]);
|
|
163
|
+
assert.ok(problems.some((p) => p.rule === "disallowed-subfolder" && p.detail === "slices/pin/create.test.ts"));
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a slice feature with index in subfolder fails", () => {
|
|
167
|
+
const problems = checkFeatureAnatomy([
|
|
168
|
+
{
|
|
169
|
+
feature: "pins",
|
|
170
|
+
files: [
|
|
171
|
+
"index.ts",
|
|
172
|
+
"schema.ts",
|
|
173
|
+
"trigger.ts",
|
|
174
|
+
"slices/create-pin.ts",
|
|
175
|
+
"shared/index.ts",
|
|
176
|
+
],
|
|
177
|
+
},
|
|
178
|
+
]);
|
|
179
|
+
assert.ok(problems.some((p) => p.rule === "no-subfolder-index" && p.detail === "shared/index.ts"));
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("a slice feature with disallowed shared file fails", () => {
|
|
183
|
+
const problems = checkFeatureAnatomy([
|
|
184
|
+
{
|
|
185
|
+
feature: "pins",
|
|
186
|
+
files: [
|
|
187
|
+
"index.ts",
|
|
188
|
+
"schema.ts",
|
|
189
|
+
"trigger.ts",
|
|
190
|
+
"slices/create-pin.ts",
|
|
191
|
+
"shared/utils.ts",
|
|
192
|
+
],
|
|
193
|
+
},
|
|
194
|
+
]);
|
|
195
|
+
assert.ok(problems.some((p) => p.rule === "disallowed-shared-file" && p.detail === "shared/utils.ts"));
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("a slice feature with co-located test and allowed shared files passes", () => {
|
|
199
|
+
const problems = checkFeatureAnatomy([
|
|
200
|
+
{
|
|
201
|
+
feature: "pins",
|
|
202
|
+
files: [
|
|
203
|
+
"index.ts",
|
|
204
|
+
"schema.ts",
|
|
205
|
+
"trigger.ts",
|
|
206
|
+
"slices/create-pin.ts",
|
|
207
|
+
"slices/create-pin.test.ts",
|
|
208
|
+
"shared/types.ts",
|
|
209
|
+
"shared/queries.ts",
|
|
210
|
+
"shared/guards.ts",
|
|
211
|
+
"shared/guards.test.ts",
|
|
212
|
+
"shared/runner.ts",
|
|
213
|
+
],
|
|
214
|
+
},
|
|
215
|
+
]);
|
|
216
|
+
assert.deepEqual(problems, []);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("a backend feature with flat anatomy fails review", () => {
|
|
220
|
+
const problems = checkFeatureAnatomy([
|
|
221
|
+
{
|
|
222
|
+
feature: "backend/src/features/sample",
|
|
223
|
+
files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts"],
|
|
224
|
+
},
|
|
225
|
+
]);
|
|
226
|
+
assert.equal(problems.length, 1);
|
|
227
|
+
assert.equal(problems[0].rule, "disallowed-flat-anatomy");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
test("the slice taxonomy comes from options", () => {
|
|
232
|
+
const features = [
|
|
233
|
+
{
|
|
234
|
+
feature: "src/modules/pins",
|
|
235
|
+
files: ["index.ts", "model.ts", "ops/create.ts", "common/lookups.ts"],
|
|
236
|
+
},
|
|
237
|
+
];
|
|
238
|
+
const anatomy = {
|
|
239
|
+
slice: {
|
|
240
|
+
required: ["index.ts", "model.ts"],
|
|
241
|
+
sliceDir: "ops",
|
|
242
|
+
sharedDir: "common",
|
|
243
|
+
sharedFiles: ["lookups.ts"],
|
|
244
|
+
},
|
|
245
|
+
sliceOnlyRoots: [],
|
|
246
|
+
};
|
|
247
|
+
assert.deepEqual(checkFeatureAnatomy(features, anatomy), []);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("a file outside the configured shared taxonomy fails", () => {
|
|
251
|
+
const features = [
|
|
252
|
+
{ feature: "src/modules/pins", files: ["index.ts", "model.ts", "ops/create.ts", "common/misc.ts"] },
|
|
253
|
+
];
|
|
254
|
+
const anatomy = {
|
|
255
|
+
slice: { required: ["index.ts", "model.ts"], sliceDir: "ops", sharedDir: "common", sharedFiles: ["lookups.ts"] },
|
|
256
|
+
sliceOnlyRoots: [],
|
|
257
|
+
};
|
|
258
|
+
const problems = checkFeatureAnatomy(features, anatomy);
|
|
259
|
+
assert.equal(problems.length, 1);
|
|
260
|
+
assert.equal(problems[0].rule, "disallowed-shared-file");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("a repository naming no slice-only root may keep the flat anatomy anywhere", () => {
|
|
264
|
+
const features = [
|
|
265
|
+
{ feature: "backend/src/features/pins", files: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts"] },
|
|
266
|
+
];
|
|
267
|
+
assert.deepEqual(checkFeatureAnatomy(features, { sliceOnlyRoots: [] }), []);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("a slice feature with components directory with index passes", () => {
|
|
271
|
+
const features = [
|
|
272
|
+
{
|
|
273
|
+
feature: "photos",
|
|
274
|
+
files: [
|
|
275
|
+
"index.ts",
|
|
276
|
+
"schema.ts",
|
|
277
|
+
"trigger.ts",
|
|
278
|
+
"slices/view-photo.ts",
|
|
279
|
+
"components/photo-viewer/index.ts",
|
|
280
|
+
"components/photo-viewer/toolbar.tsx",
|
|
281
|
+
],
|
|
282
|
+
},
|
|
283
|
+
];
|
|
284
|
+
assert.deepEqual(checkFeatureAnatomy(features), []);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test("a slice feature with component view missing index fails", () => {
|
|
288
|
+
const features = [
|
|
289
|
+
{
|
|
290
|
+
feature: "photos",
|
|
291
|
+
files: [
|
|
292
|
+
"index.ts",
|
|
293
|
+
"schema.ts",
|
|
294
|
+
"trigger.ts",
|
|
295
|
+
"slices/view-photo.ts",
|
|
296
|
+
"components/photo-viewer/toolbar.tsx",
|
|
297
|
+
],
|
|
298
|
+
},
|
|
299
|
+
];
|
|
300
|
+
const problems = checkFeatureAnatomy(features);
|
|
301
|
+
assert.equal(problems.length, 1);
|
|
302
|
+
assert.equal(problems[0].rule, "missing-component-index");
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test("a slice feature with loose component outside a view folder fails", () => {
|
|
306
|
+
const features = [
|
|
307
|
+
{
|
|
308
|
+
feature: "photos",
|
|
309
|
+
files: [
|
|
310
|
+
"index.ts",
|
|
311
|
+
"schema.ts",
|
|
312
|
+
"trigger.ts",
|
|
313
|
+
"slices/view-photo.ts",
|
|
314
|
+
"components/loose-button.tsx",
|
|
315
|
+
],
|
|
316
|
+
},
|
|
317
|
+
];
|
|
318
|
+
const problems = checkFeatureAnatomy(features);
|
|
319
|
+
assert.equal(problems.length, 1);
|
|
320
|
+
assert.equal(problems[0].rule, "loose-component");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("a slice feature with ui fragments in slices fails", () => {
|
|
324
|
+
const features = [
|
|
325
|
+
{
|
|
326
|
+
feature: "blackboard",
|
|
327
|
+
files: [
|
|
328
|
+
"index.ts",
|
|
329
|
+
"schema.ts",
|
|
330
|
+
"trigger.ts",
|
|
331
|
+
"slices/list-boards.ts",
|
|
332
|
+
"slices/board-rail.tsx",
|
|
333
|
+
],
|
|
334
|
+
},
|
|
335
|
+
];
|
|
336
|
+
const problems = checkFeatureAnatomy(features);
|
|
337
|
+
assert.equal(problems.length, 1);
|
|
338
|
+
assert.equal(problems[0].rule, "ui-fragment-in-slices");
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test("a slice feature with deeply nested components (> depth 3) fails", () => {
|
|
342
|
+
const features = [
|
|
343
|
+
{
|
|
344
|
+
feature: "photos",
|
|
345
|
+
files: [
|
|
346
|
+
"index.ts",
|
|
347
|
+
"schema.ts",
|
|
348
|
+
"trigger.ts",
|
|
349
|
+
"slices/view-photo.ts",
|
|
350
|
+
"components/a/b/c.tsx",
|
|
351
|
+
],
|
|
352
|
+
},
|
|
353
|
+
];
|
|
354
|
+
const problems = checkFeatureAnatomy(features);
|
|
355
|
+
assert.equal(problems.length, 1);
|
|
356
|
+
assert.equal(problems[0].rule, "disallowed-subfolder");
|
|
357
|
+
assert.equal(problems[0].detail, "components/a/b/c.tsx");
|
|
358
|
+
});
|
|
359
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// QC-007: a rule that cannot name its check is deleted or converted into one. The map
|
|
2
|
+
// in docs/enforcement.md is what carries that, and nothing has been watching the map.
|
|
3
|
+
//
|
|
4
|
+
// A stale row is the quiet failure here: a rule renamed in code, still listed under its
|
|
5
|
+
// old name, reads as enforced by something that no longer exists. Only the checks this
|
|
6
|
+
// kit owns are resolved — a row naming a third-party rule or a shell command is listed
|
|
7
|
+
// and left alone, because this gate cannot know what those are.
|
|
8
|
+
|
|
9
|
+
const RULE = /`qc\/([a-z0-9-]+)`/g;
|
|
10
|
+
const GATE = /`qc check`\s*\(([a-z0-9-]+)\)/g;
|
|
11
|
+
|
|
12
|
+
function found(markdown, pattern) {
|
|
13
|
+
pattern.lastIndex = 0;
|
|
14
|
+
const names = new Set();
|
|
15
|
+
let match = pattern.exec(markdown);
|
|
16
|
+
while (match !== null) {
|
|
17
|
+
names.add(match[1]);
|
|
18
|
+
match = pattern.exec(markdown);
|
|
19
|
+
}
|
|
20
|
+
return names;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string} markdown docs/enforcement.md
|
|
25
|
+
* @param {string[]} rules rule names the kit provides
|
|
26
|
+
* @param {string[]} gates gate names `qc check` runs
|
|
27
|
+
* @param {{requireEveryCheckListed?: boolean}} [options]
|
|
28
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
29
|
+
*/
|
|
30
|
+
export function checkEnforcementMap(markdown, rules, gates, options = {}) {
|
|
31
|
+
const label = "docs/enforcement.md";
|
|
32
|
+
const problems = [];
|
|
33
|
+
|
|
34
|
+
const citedRules = found(markdown, RULE);
|
|
35
|
+
const citedGates = found(markdown, GATE);
|
|
36
|
+
|
|
37
|
+
for (const name of [...citedRules].sort()) {
|
|
38
|
+
if (!rules.includes(name)) {
|
|
39
|
+
problems.push({ path: label, rule: "unknown-check", detail: `qc/${name} is listed but no such rule exists` });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (const name of [...citedGates].sort()) {
|
|
43
|
+
if (!gates.includes(name)) {
|
|
44
|
+
problems.push({ path: label, rule: "unknown-check", detail: `qc check (${name}) is listed but no such gate exists` });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The other direction: a check nobody wrote down is a rule nobody can find.
|
|
49
|
+
if (options.requireEveryCheckListed !== false) {
|
|
50
|
+
for (const name of rules) {
|
|
51
|
+
if (!citedRules.has(name)) {
|
|
52
|
+
problems.push({ path: label, rule: "unlisted-check", detail: `qc/${name} enforces something the map does not state` });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const name of gates) {
|
|
56
|
+
if (!citedGates.has(name)) {
|
|
57
|
+
problems.push({ path: label, rule: "unlisted-check", detail: `qc check (${name}) enforces something the map does not state` });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return problems;
|
|
62
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkEnforcementMap } from "./enforcement-map.mjs";
|
|
4
|
+
|
|
5
|
+
const RULES = ["no-raw-fetch"];
|
|
6
|
+
const GATES = ["citations"];
|
|
7
|
+
const complete = "| One module calls fetch | `qc/no-raw-fetch` |\n| Cited ids resolve | `qc check` (citations) |";
|
|
8
|
+
|
|
9
|
+
test("a map naming every check, and only real ones, passes", () => {
|
|
10
|
+
assert.deepEqual(checkEnforcementMap(complete, RULES, GATES), []);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// The failure this exists for: a rule renamed in code, still listed under the old name.
|
|
14
|
+
test("a row naming a rule that no longer exists fails", () => {
|
|
15
|
+
const stale = "| Storage in resource | `qc/drizzle-only-in-resource` |\n| Cited ids resolve | `qc check` (citations) |";
|
|
16
|
+
const problems = checkEnforcementMap(stale, ["storage-only-in-resource"], GATES);
|
|
17
|
+
assert.ok(problems.some((p) => p.rule === "unknown-check" && p.detail.includes("drizzle-only-in-resource")));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("a row naming a gate that does not exist fails", () => {
|
|
21
|
+
const problems = checkEnforcementMap("| x | `qc check` (nonesuch) |", [], GATES);
|
|
22
|
+
assert.ok(problems.some((p) => p.rule === "unknown-check" && p.detail.includes("nonesuch")));
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a check the map never mentions fails", () => {
|
|
26
|
+
const problems = checkEnforcementMap(complete, [...RULES, "signal-last-param"], GATES);
|
|
27
|
+
assert.equal(problems.length, 1);
|
|
28
|
+
assert.equal(problems[0].rule, "unlisted-check");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("a third-party check is listed and left alone", () => {
|
|
32
|
+
const foreign = `${complete}\n| No any | \`@typescript-eslint/no-explicit-any\` |\n| Duplication | \`jscpd\` |`;
|
|
33
|
+
assert.deepEqual(checkEnforcementMap(foreign, RULES, GATES), []);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("the completeness half can be turned off", () => {
|
|
37
|
+
const problems = checkEnforcementMap(complete, [...RULES, "signal-last-param"], GATES, {
|
|
38
|
+
requireEveryCheckListed: false,
|
|
39
|
+
});
|
|
40
|
+
assert.deepEqual(problems, []);
|
|
41
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Comments, docs and rules are written in English. The repository's domain is Japanese, so the
|
|
2
|
+
// rule cannot be "no Japanese in the tree" -- an official standard's title, a prompt that reads a
|
|
3
|
+
// Japanese drawing and the user-facing dictionary are all data, and translating them breaks them.
|
|
4
|
+
//
|
|
5
|
+
// The line is prose against data. Prose a developer writes for another developer is English. Data
|
|
6
|
+
// the product needs in another language is declared, and its English variant must exist beside it.
|
|
7
|
+
|
|
8
|
+
const SCRIPTS = {
|
|
9
|
+
// Hiragana, katakana, CJK ideographs, Hangul: language a reader must know to follow the text.
|
|
10
|
+
cjk: "\\u3040-\\u30ff\\u3400-\\u4dbf\\u4e00-\\u9fff\\uac00-\\ud7af",
|
|
11
|
+
// Fullwidth forms are typography, not language -- `({count})` costs an English reader nothing.
|
|
12
|
+
// Separate, so a repository that wants ASCII punctuation can ask for it without conflating the two.
|
|
13
|
+
fullwidth: "\\uff00-\\uffef",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function scriptPattern(scripts) {
|
|
17
|
+
const ranges = scripts.map((name) => SCRIPTS[name] ?? name).join("");
|
|
18
|
+
if (ranges === "") throw new Error("language.scripts must name at least one script range");
|
|
19
|
+
return new RegExp(`[${ranges}]`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function globToRegExp(glob) {
|
|
23
|
+
const body = glob
|
|
24
|
+
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
25
|
+
.replace(/\*\*/g, "\u0000")
|
|
26
|
+
.replace(/\*/g, "[^/]*")
|
|
27
|
+
.replace(/\u0000/g, ".*");
|
|
28
|
+
return new RegExp(`^${body}$`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function matchesAny(relPath, globs) {
|
|
32
|
+
return globs.some((glob) => globToRegExp(glob).test(relPath));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Splits a source file's lines into the ones that are comment prose and the ones that are not.
|
|
37
|
+
* A heuristic, deliberately: it never needs to be exact, because a string that trips the comment
|
|
38
|
+
* rule is still a string that has to be declared.
|
|
39
|
+
*/
|
|
40
|
+
export function commentLines(contents, lineComment = ["//", "#", "--"]) {
|
|
41
|
+
const out = [];
|
|
42
|
+
let inBlock = false;
|
|
43
|
+
contents.split("\n").forEach((line, index) => {
|
|
44
|
+
const trimmed = line.trim();
|
|
45
|
+
const opensBlock = trimmed.startsWith("/*");
|
|
46
|
+
const isComment =
|
|
47
|
+
inBlock || opensBlock || trimmed.startsWith("*") || lineComment.some((token) => trimmed.startsWith(token));
|
|
48
|
+
if (opensBlock && !trimmed.includes("*/")) inBlock = true;
|
|
49
|
+
else if (inBlock && trimmed.includes("*/")) inBlock = false;
|
|
50
|
+
if (isComment) out.push({ line: index + 1, text: line });
|
|
51
|
+
});
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `legacyNonEnglish` is `{path: count}`: a ceiling, not a pass. A list is read as an old-style
|
|
56
|
+
* unlimited entry and reported, so a repository cannot keep one by accident. */
|
|
57
|
+
function legacyCeilings(legacy) {
|
|
58
|
+
if (Array.isArray(legacy)) return { ceilings: Object.fromEntries(legacy.map((p) => [p, Infinity])), uncounted: legacy };
|
|
59
|
+
return { ceilings: legacy ?? {}, uncounted: [] };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {{path: string, contents: string}[]} files paths relative to the repository root
|
|
64
|
+
* @param {{scripts?: string[], allowNonEnglish?: string[], legacyNonEnglish?: Record<string,number>|string[], translationPairs?: {pattern: string, english: string}[]}} [options]
|
|
65
|
+
*/
|
|
66
|
+
export function checkEnglishSource(files, options = {}) {
|
|
67
|
+
const script = scriptPattern(options.scripts ?? ["cjk"]);
|
|
68
|
+
// A file that predates the rule is listed with the number of occurrences it had. Over that is a
|
|
69
|
+
// finding, so the debt cannot grow; at zero the entry is stale, so the list can only shrink.
|
|
70
|
+
const { ceilings, uncounted } = legacyCeilings(options.legacyNonEnglish);
|
|
71
|
+
const legacy = Object.keys(ceilings);
|
|
72
|
+
const allowed = [...(options.allowNonEnglish ?? []), ...legacy];
|
|
73
|
+
const used = new Set();
|
|
74
|
+
const problems = [];
|
|
75
|
+
for (const path of uncounted) {
|
|
76
|
+
problems.push({
|
|
77
|
+
path,
|
|
78
|
+
rule: "uncounted-legacy-entry",
|
|
79
|
+
detail: "listed as a bare path, which allows any amount. Record the count it has: { \"path\": <n> }.",
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const { path, contents } of files) {
|
|
84
|
+
if (!script.test(contents)) continue;
|
|
85
|
+
const declared = matchesAny(path, allowed);
|
|
86
|
+
|
|
87
|
+
// In Markdown every line is prose, and `#` opens a heading rather than a comment. Running the
|
|
88
|
+
// code heuristic over a document reports its headings as comments, so the file-level rule --
|
|
89
|
+
// is this document declared -- is the only one that applies.
|
|
90
|
+
const prose = path.endsWith(".md") ? [] : commentLines(contents);
|
|
91
|
+
for (const { line, text } of prose) {
|
|
92
|
+
if (!script.test(text)) continue;
|
|
93
|
+
problems.push({
|
|
94
|
+
path,
|
|
95
|
+
rule: "english-comment",
|
|
96
|
+
detail: `line ${line}: a comment is written in English. Name the term in English, or cite the glossary entry for it.`,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (declared && legacy.includes(path)) {
|
|
100
|
+
used.add(path);
|
|
101
|
+
const found = (contents.match(new RegExp(script.source, "g")) ?? []).length;
|
|
102
|
+
if (found > ceilings[path]) {
|
|
103
|
+
problems.push({
|
|
104
|
+
path,
|
|
105
|
+
rule: "legacy-entry-grew",
|
|
106
|
+
detail: `carries ${found} non-English characters, over the ${ceilings[path]} recorded in language.legacyNonEnglish. A listed file may not get worse.`,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (!declared && !problems.some((problem) => problem.path === path)) {
|
|
111
|
+
problems.push({
|
|
112
|
+
path,
|
|
113
|
+
rule: "english-source",
|
|
114
|
+
detail:
|
|
115
|
+
"carries non-English text that is not declared. Data the product needs in another " +
|
|
116
|
+
"language belongs in a path listed under language.allowNonEnglish, beside its English variant.",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// A budget the repository states, so adding a file to the ledger is a visible act rather than an
|
|
121
|
+
// edit nobody reviews. It may only be met or lowered: a budget above what the tree carries is
|
|
122
|
+
// itself a finding, which is what stops the debt sitting there unnoticed -- the way a document
|
|
123
|
+
// stayed Japanese for weeks because it was listed and nothing ever said so again.
|
|
124
|
+
const budget = options.legacyBudget;
|
|
125
|
+
if (budget) {
|
|
126
|
+
const files = legacy.length;
|
|
127
|
+
const occurrences = Object.values(ceilings).reduce((sum, n) => sum + (Number.isFinite(n) ? n : 0), 0);
|
|
128
|
+
if (files > budget.files || occurrences > budget.occurrences) {
|
|
129
|
+
problems.push({
|
|
130
|
+
path: "qc.config.json",
|
|
131
|
+
rule: "legacy-over-budget",
|
|
132
|
+
detail: `language.legacyNonEnglish carries ${files} file(s) and ${occurrences} occurrence(s), over the budget of ${budget.files} and ${budget.occurrences}. The ledger may only shrink.`,
|
|
133
|
+
});
|
|
134
|
+
} else if (files < budget.files || occurrences < budget.occurrences) {
|
|
135
|
+
problems.push({
|
|
136
|
+
path: "qc.config.json",
|
|
137
|
+
rule: "legacy-budget-stale",
|
|
138
|
+
detail: `language.legacyBudget still allows ${budget.files} file(s) and ${budget.occurrences} occurrence(s), but the tree carries ${files} and ${occurrences}. Lower the budget to what is left.`,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for (const path of legacy) {
|
|
144
|
+
if (!used.has(path)) {
|
|
145
|
+
problems.push({
|
|
146
|
+
path,
|
|
147
|
+
rule: "stale-legacy-entry",
|
|
148
|
+
detail: "listed under language.legacyNonEnglish but carries no non-English text. Delete the entry.",
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return problems;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Every declared translation has an English variant beside it, so English is the one language
|
|
157
|
+
* present in every set -- the main variant, whatever else a repository adds.
|
|
158
|
+
* @param {string[]} paths every file in the repository, relative to its root
|
|
159
|
+
*/
|
|
160
|
+
export function checkTranslationPairs(paths, pairs = []) {
|
|
161
|
+
const problems = [];
|
|
162
|
+
for (const { pattern, english } of pairs) {
|
|
163
|
+
for (const path of paths.filter((candidate) => matchesAny(candidate, [pattern]))) {
|
|
164
|
+
const beside = path.replace(/[^/]+$/, english);
|
|
165
|
+
if (beside === path) continue;
|
|
166
|
+
if (!paths.includes(beside)) {
|
|
167
|
+
problems.push({
|
|
168
|
+
path,
|
|
169
|
+
rule: "english-variant-missing",
|
|
170
|
+
detail: `a translation without its English variant. Add ${beside}: every language set carries English.`,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return problems;
|
|
176
|
+
}
|