architecture-harness 0.9.0 → 0.11.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 +3 -2
- package/src/cli/check.mjs +50 -1
- package/src/cli/registry-history.mjs +53 -0
- package/src/cli/registry-history.test.mjs +88 -0
- package/src/config.mjs +38 -0
- package/src/descriptions.mjs +4 -0
- package/src/eslint/index.mjs +6 -0
- package/src/eslint/preset.mjs +23 -8
- package/src/eslint/preset.test.mjs +62 -0
- package/src/eslint/rules/ime-safe-key.mjs +179 -0
- package/src/eslint/rules/no-sql-raw.mjs +30 -0
- package/src/eslint/rules/registry-literal.mjs +66 -0
- package/src/eslint/rules.test.mjs +151 -0
- package/src/gates/migration-numbers.mjs +33 -0
- package/src/gates/migration-numbers.test.mjs +35 -0
- package/src/gates/registry-literal.mjs +137 -0
- package/src/gates/registry-literal.test.mjs +68 -0
- package/src/gates/registry-readers.mjs +24 -0
- package/src/gates/registry-readers.test.mjs +35 -0
- package/src/gates/sql-identifiers.mjs +28 -9
- package/src/gates/sql-identifiers.test.mjs +27 -0
- package/src/gates/threshold-ratchet.mjs +46 -0
- package/src/gates/threshold-ratchet.test.mjs +60 -0
- package/templates/quality-thresholds.json +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "architecture-harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
|
|
6
6
|
"license": "MIT",
|
|
@@ -76,6 +76,7 @@
|
|
|
76
76
|
"@typescript-eslint/eslint-plugin": "^8",
|
|
77
77
|
"@typescript-eslint/parser": "^8",
|
|
78
78
|
"eslint-plugin-boundaries": "^5",
|
|
79
|
-
"eslint-plugin-sonarjs": "^3"
|
|
79
|
+
"eslint-plugin-sonarjs": "^3",
|
|
80
|
+
"typescript": "^5"
|
|
80
81
|
}
|
|
81
82
|
}
|
package/src/cli/check.mjs
CHANGED
|
@@ -30,7 +30,12 @@ import { checkAdrFormat } from "../gates/adr-format.mjs";
|
|
|
30
30
|
import { checkConfigFloor } from "../gates/config-floor.mjs";
|
|
31
31
|
import { defaults } from "../config.mjs";
|
|
32
32
|
import { rules as lintRules } from "../eslint/index.mjs";
|
|
33
|
-
import { enabled } from "../config.mjs";
|
|
33
|
+
import { adrLog, enabled, readerEntries, thresholds } from "../config.mjs";
|
|
34
|
+
import { checkRegistryReaders } from "../gates/registry-readers.mjs";
|
|
35
|
+
import { checkMigrationNumbers } from "../gates/migration-numbers.mjs";
|
|
36
|
+
import { checkRegistryLiteral } from "../gates/registry-literal.mjs";
|
|
37
|
+
import { checkThresholdRatchet } from "../gates/threshold-ratchet.mjs";
|
|
38
|
+
import { ratchetInputs } from "./registry-history.mjs";
|
|
34
39
|
import { repoFiles } from "./repo-files.mjs";
|
|
35
40
|
|
|
36
41
|
// Each skip is tested on "/" + the relative path, so it reads a path the way it read a full one.
|
|
@@ -178,6 +183,15 @@ function sourceFiles(config, tree, only) {
|
|
|
178
183
|
return readEach(config.root, rels);
|
|
179
184
|
}
|
|
180
185
|
|
|
186
|
+
function parsedJson(source) {
|
|
187
|
+
if (source === null) return null;
|
|
188
|
+
try {
|
|
189
|
+
return JSON.parse(source);
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
181
195
|
async function registered(file) {
|
|
182
196
|
const source = await read(file);
|
|
183
197
|
if (source === null) return [];
|
|
@@ -454,6 +468,34 @@ export async function runCheck(config, only = [], { lister = repoFiles } = {}) {
|
|
|
454
468
|
problems.push(...(await agreements(config)));
|
|
455
469
|
}
|
|
456
470
|
|
|
471
|
+
if (enabled(config.gates, "registry-readers")) {
|
|
472
|
+
const entries = readerEntries(thresholds(config));
|
|
473
|
+
const readers = [...new Set(entries.flatMap((entry) => entry.readers))];
|
|
474
|
+
const sources = new Map(await Promise.all(readers.map(async (reader) => [reader, await read(path.join(config.root, reader))])));
|
|
475
|
+
problems.push(...checkRegistryReaders(entries, sources));
|
|
476
|
+
if (readers.length > 0) lines.push(`OK readers ${readers.length} registry reader(s), each present and naming its entry`);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (enabled(config.gates, "registry-literal")) {
|
|
480
|
+
const docs = await readEach(config.root, tree.under(config.docs.root).filter((file) => file.endsWith(".md")));
|
|
481
|
+
const libraries = parsedJson(await read(path.join(config.root, config.versions)))?.libraries ?? {};
|
|
482
|
+
const exempt = config.registryLiteral.exempt ?? adrLog(config);
|
|
483
|
+
problems.push(...checkRegistryLiteral(docs, { thresholds: thresholds(config), libraries, exempt }));
|
|
484
|
+
if (docs.length > 0) lines.push(`OK literals ${docs.length} doc(s) carry a token, never a figure a registry owns`);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (enabled(config.gates, "threshold-ratchet")) {
|
|
488
|
+
const { base } = config.ratchet;
|
|
489
|
+
const inputs = await ratchetInputs(config.root, { base, registry: config.thresholds, adrGlobs: adrLog(config) });
|
|
490
|
+
if (inputs.skip) {
|
|
491
|
+
lines.push(`OK ratchet skipped: ${inputs.skip}`);
|
|
492
|
+
} else {
|
|
493
|
+
problems.push(...checkThresholdRatchet(inputs.before, inputs.after, inputs.adrs, { registry: config.thresholds }));
|
|
494
|
+
const count = Object.keys(inputs.after).length;
|
|
495
|
+
if (count > 0) lines.push(`OK ratchet ${count} threshold(s) against the merge base with ${base}; a loosening names its ADR`);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
457
499
|
if (enabled(config.gates, "sql-identifiers") || enabled(config.gates, "tenant-predicate")) {
|
|
458
500
|
problems.push(...(await sqlAgreement(config, tree, taken)));
|
|
459
501
|
lines.push("OK sql every column is declared, every statement and shared insert carries the tenant");
|
|
@@ -461,6 +503,13 @@ export async function runCheck(config, only = [], { lister = repoFiles } = {}) {
|
|
|
461
503
|
for (const exemption of taken) lines.push(` exempt ${exemption.path}: ${exemption.reason}`);
|
|
462
504
|
}
|
|
463
505
|
|
|
506
|
+
if (enabled(config.gates, "migration-numbers")) {
|
|
507
|
+
const migrationDir = posix(config.paths.migrations);
|
|
508
|
+
const names = tree.namesIn(migrationDir).filter((name) => tree.isFile(join(migrationDir, name)));
|
|
509
|
+
problems.push(...checkMigrationNumbers(names, migrationDir));
|
|
510
|
+
if (names.length > 0) lines.push(`OK migrations ${names.filter((name) => name.endsWith(".sql")).length} migration(s), each number unique, with no gap`);
|
|
511
|
+
}
|
|
512
|
+
|
|
464
513
|
if (enabled(config.gates, "saga-tests")) {
|
|
465
514
|
const sources = await sourceFiles(config, tree);
|
|
466
515
|
const sagas = declaredSagas(
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// A resource: the thresholds registry at the merge base with a ref, the registry now, and the ADR
|
|
2
|
+
// files the change touches. Git answers each question; no answer is a reason to skip, never a failure.
|
|
3
|
+
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { globMatcher } from "../glob.mjs";
|
|
8
|
+
|
|
9
|
+
/** @returns {string|null} stdout, or null when git is absent or the command fails. */
|
|
10
|
+
function git(root, ...args) {
|
|
11
|
+
const result = spawnSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
12
|
+
return result.status === 0 ? result.stdout : null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function gatesOf(text) {
|
|
16
|
+
if (text === null) return {};
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(text).gates ?? {};
|
|
19
|
+
} catch {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const paths = (listing) => (listing ?? "").split("\0").filter(Boolean);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} root the repository root, or a folder inside one
|
|
28
|
+
* @param {{base: string, registry: string, adrGlobs: string[]}} options
|
|
29
|
+
* @returns {Promise<{skip: string} | {before: object, after: object, adrs: {path: string, text: string}[]}>}
|
|
30
|
+
*/
|
|
31
|
+
export async function ratchetInputs(root, { base, registry, adrGlobs }) {
|
|
32
|
+
if (git(root, "rev-parse", "--verify", "--quiet", "HEAD") === null) return { skip: "no git, or no commit yet" };
|
|
33
|
+
if (git(root, "rev-parse", "--verify", "--quiet", `${base}^{commit}`) === null) return { skip: `no ref '${base}'` };
|
|
34
|
+
const mergeBase = git(root, "merge-base", base, "HEAD")?.trim();
|
|
35
|
+
if (!mergeBase) return { skip: `no merge base with '${base}', as in a shallow clone` };
|
|
36
|
+
|
|
37
|
+
const file = registry.split(path.sep).join("/");
|
|
38
|
+
const before = gatesOf(git(root, "show", `${mergeBase}:./${file}`));
|
|
39
|
+
const after = gatesOf(await readFile(path.join(root, registry), "utf8").catch(() => null));
|
|
40
|
+
|
|
41
|
+
// Committed, staged and unstaged changes since the merge base, and a new file not yet added.
|
|
42
|
+
const isAdr = globMatcher(adrGlobs);
|
|
43
|
+
const changed = new Set([
|
|
44
|
+
...paths(git(root, "diff", "--name-only", "--relative", "-z", mergeBase)),
|
|
45
|
+
...paths(git(root, "ls-files", "--others", "--exclude-standard", "-z")),
|
|
46
|
+
]);
|
|
47
|
+
const adrs = [];
|
|
48
|
+
for (const rel of [...changed].filter((rel) => rel.endsWith(".md") && isAdr(rel))) {
|
|
49
|
+
const text = await readFile(path.join(root, rel), "utf8").catch(() => null);
|
|
50
|
+
if (text !== null) adrs.push({ path: rel, text });
|
|
51
|
+
}
|
|
52
|
+
return { before, after, adrs };
|
|
53
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { load } from "../config.mjs";
|
|
8
|
+
import { runCheck } from "./check.mjs";
|
|
9
|
+
|
|
10
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", encoding: "utf8" });
|
|
11
|
+
const REGISTRY = "quality-thresholds.json";
|
|
12
|
+
const registry = (gates) => `${JSON.stringify({ gates }, null, 2)}\n`;
|
|
13
|
+
const FILELENGTH = { filelength: { value: 500, unit: "lines", comparator: "max" } };
|
|
14
|
+
|
|
15
|
+
function write(dir, files) {
|
|
16
|
+
for (const [rel, contents] of Object.entries(files)) {
|
|
17
|
+
mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });
|
|
18
|
+
writeFileSync(path.join(dir, rel), contents);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A repository whose `main` holds the registry, checked out on a branch `feat`. One per case. */
|
|
23
|
+
function repo({ base = "main", commit = true } = {}) {
|
|
24
|
+
const dir = mkdtempSync(path.join(tmpdir(), "qc-ratchet-"));
|
|
25
|
+
git(dir, "init", "-q", "-b", "main");
|
|
26
|
+
git(dir, "config", "user.name", "qc");
|
|
27
|
+
git(dir, "config", "user.email", "qc@example.com");
|
|
28
|
+
git(dir, "config", "commit.gpgsign", "false");
|
|
29
|
+
write(dir, { "qc.config.json": JSON.stringify({ featureRoots: [], ratchet: { base } }), [REGISTRY]: registry(FILELENGTH) });
|
|
30
|
+
if (!commit) return dir;
|
|
31
|
+
git(dir, "add", ".");
|
|
32
|
+
git(dir, "commit", "-q", "-m", "init");
|
|
33
|
+
git(dir, "checkout", "-q", "-b", "feat");
|
|
34
|
+
return dir;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function ratchet(dir) {
|
|
38
|
+
const { problems, lines } = await runCheck(load(dir));
|
|
39
|
+
return {
|
|
40
|
+
loosened: problems.filter((problem) => problem.rule === "loosened-threshold"),
|
|
41
|
+
line: lines.find((line) => line.includes("ratchet")),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
test("a max that rises from 500 to 550 fails", async () => {
|
|
46
|
+
const dir = repo();
|
|
47
|
+
write(dir, { [REGISTRY]: registry({ filelength: { ...FILELENGTH.filelength, value: 550 } }) });
|
|
48
|
+
const { loosened } = await ratchet(dir);
|
|
49
|
+
assert.equal(loosened.length, 1);
|
|
50
|
+
assert.match(loosened[0].detail, /filelength/);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("the same rise passes with a committed ADR that names the key", async () => {
|
|
54
|
+
const dir = repo();
|
|
55
|
+
write(dir, {
|
|
56
|
+
[REGISTRY]: registry({ filelength: { ...FILELENGTH.filelength, value: 550 } }),
|
|
57
|
+
"docs/decisions/0002-longer-files.md": "# Longer files\n\n`filelength` rises to 550: the parser needs one file.\n",
|
|
58
|
+
});
|
|
59
|
+
git(dir, "add", ".");
|
|
60
|
+
git(dir, "commit", "-q", "-m", "loosen");
|
|
61
|
+
assert.deepEqual((await ratchet(dir)).loosened, []);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("a tightening passes", async () => {
|
|
65
|
+
const dir = repo();
|
|
66
|
+
write(dir, { [REGISTRY]: registry({ filelength: { ...FILELENGTH.filelength, value: 450 } }) });
|
|
67
|
+
assert.deepEqual((await ratchet(dir)).loosened, []);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("a new entry passes", async () => {
|
|
71
|
+
const dir = repo();
|
|
72
|
+
write(dir, { [REGISTRY]: registry({ ...FILELENGTH, nesting: { value: 9, unit: "count", comparator: "max" } }) });
|
|
73
|
+
assert.deepEqual((await ratchet(dir)).loosened, []);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("a base ref that does not exist skips with an OK line that says why", async () => {
|
|
77
|
+
const dir = repo({ base: "origin/main" });
|
|
78
|
+
write(dir, { [REGISTRY]: registry({ filelength: { ...FILELENGTH.filelength, value: 550 } }) });
|
|
79
|
+
const { loosened, line } = await ratchet(dir);
|
|
80
|
+
assert.deepEqual(loosened, []);
|
|
81
|
+
assert.match(line, /^OK .*no ref 'origin\/main'/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a repository with no commit yet skips with an OK line", async () => {
|
|
85
|
+
const { loosened, line } = await ratchet(repo({ commit: false }));
|
|
86
|
+
assert.deepEqual(loosened, []);
|
|
87
|
+
assert.match(line, /^OK .*no commit/);
|
|
88
|
+
});
|
package/src/config.mjs
CHANGED
|
@@ -36,6 +36,15 @@ export const defaults = {
|
|
|
36
36
|
},
|
|
37
37
|
|
|
38
38
|
thresholds: "quality-thresholds.json",
|
|
39
|
+
// Pinned library versions. Docs carry a {{ver:id}} or {{v:id}} token for each.
|
|
40
|
+
versions: "versions.json",
|
|
41
|
+
|
|
42
|
+
// Globs of docs that may state a registry figure. Null is the ADR log, from adrLog(): a decision
|
|
43
|
+
// states the number as of its date.
|
|
44
|
+
registryLiteral: { exempt: null },
|
|
45
|
+
|
|
46
|
+
// A threshold loosens only with an ADR. `base` is the ref whose merge base the registry is compared with.
|
|
47
|
+
ratchet: { base: "origin/main" },
|
|
39
48
|
|
|
40
49
|
// A key that makes a retry a resume rather than a second write. docs/guards.md.
|
|
41
50
|
idempotency: {
|
|
@@ -241,6 +250,13 @@ export const defaults = {
|
|
|
241
250
|
// racing against a cancellation, and observing a rejection nobody is waiting for any more.
|
|
242
251
|
promise: { allow: [] },
|
|
243
252
|
|
|
253
|
+
// Enter and Escape in text entry wait for the IME. `components` are a repository's own text
|
|
254
|
+
// inputs. `guards` name the one guard function; empty accepts an inline isComposing or keyCode check.
|
|
255
|
+
ime: { components: [], guards: [] },
|
|
256
|
+
|
|
257
|
+
// Files a person reviewed that may call sql.raw. Each one is a decision, so the list stays short.
|
|
258
|
+
sqlRaw: { allow: [] },
|
|
259
|
+
|
|
244
260
|
anatomy: {
|
|
245
261
|
// Bounded vertical slices.
|
|
246
262
|
slice: {
|
|
@@ -264,7 +280,11 @@ export const defaults = {
|
|
|
264
280
|
"eight-blocks": true,
|
|
265
281
|
citations: true,
|
|
266
282
|
"registry-agreement": true,
|
|
283
|
+
"registry-readers": true,
|
|
267
284
|
"sql-identifiers": true,
|
|
285
|
+
"migration-numbers": true,
|
|
286
|
+
"registry-literal": true,
|
|
287
|
+
"threshold-ratchet": true,
|
|
268
288
|
"tenant-predicate": true,
|
|
269
289
|
"claimed-requirements": true,
|
|
270
290
|
"public-routes": true,
|
|
@@ -306,6 +326,9 @@ export const defaults = {
|
|
|
306
326
|
"tenant-scoped-table": true,
|
|
307
327
|
"signal-last-param": true,
|
|
308
328
|
"no-raw-fetch": true,
|
|
329
|
+
"no-sql-raw": true,
|
|
330
|
+
"ime-safe-key": true,
|
|
331
|
+
"registry-literal": true,
|
|
309
332
|
"durable-idempotency-key": true,
|
|
310
333
|
"no-supersession-trail": true,
|
|
311
334
|
},
|
|
@@ -376,6 +399,21 @@ export function thresholds(config) {
|
|
|
376
399
|
return createRequire(import.meta.url)(file).gates ?? {};
|
|
377
400
|
}
|
|
378
401
|
|
|
402
|
+
/** The files that hold decision records: each ADR under `adr.root`, and the decision register. */
|
|
403
|
+
export function adrLog(config) {
|
|
404
|
+
return [`${config.adr.root.replace(/\/+$/, "")}/**`, config.docs.decisions];
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Registry entries that say who may hold their number. `names` are identifier patterns, each a
|
|
409
|
+
* regular expression matched against the whole identifier, case-insensitive. `readers` are paths.
|
|
410
|
+
*/
|
|
411
|
+
export function readerEntries(registry) {
|
|
412
|
+
return Object.entries(registry)
|
|
413
|
+
.filter(([, entry]) => entry.names?.length > 0 || entry.readers?.length > 0)
|
|
414
|
+
.map(([key, entry]) => ({ key, names: entry.names ?? [], readers: entry.readers ?? [] }));
|
|
415
|
+
}
|
|
416
|
+
|
|
379
417
|
export function enabled(map, id) {
|
|
380
418
|
return map[id] !== false;
|
|
381
419
|
}
|
package/src/descriptions.mjs
CHANGED
|
@@ -4,7 +4,11 @@ export const gateDescriptions = Object.freeze({
|
|
|
4
4
|
"eight-blocks": "A feature keeps the anatomy it declares, and no block or slice is empty",
|
|
5
5
|
citations: "Every cited id and doc path resolves, and nothing references another repository",
|
|
6
6
|
"registry-agreement": "A value written twice agrees with the registry that owns it",
|
|
7
|
+
"registry-readers": "Every reader a registry entry names exists and names the entry",
|
|
7
8
|
"sql-identifiers": "A query names a column a migration declares, and only its own feature's tables",
|
|
9
|
+
"migration-numbers": "Each migration number is unique, and the numbers run with no gap",
|
|
10
|
+
"registry-literal": "A doc states a registry threshold or library version as its token, never as the figure",
|
|
11
|
+
"threshold-ratchet": "A threshold never loosens against the merge base unless a changed ADR names it",
|
|
8
12
|
"tenant-predicate": "Every statement on a business table filters on the tenant",
|
|
9
13
|
"claimed-requirements": "A requirement a route claims names the test that proves it",
|
|
10
14
|
"public-routes": "The edge lets through exactly the routes the api serves without a session",
|
package/src/eslint/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import durableIdempotencyKey from "./rules/durable-idempotency-key.mjs";
|
|
2
2
|
import externalServiceOnlyInAdapter from "./rules/external-service-only-in-adapter.mjs";
|
|
3
|
+
import imeSafeKey from "./rules/ime-safe-key.mjs";
|
|
3
4
|
import noCrossFeatureInternals from "./rules/no-cross-feature-internals.mjs";
|
|
5
|
+
import registryLiteral from "./rules/registry-literal.mjs";
|
|
4
6
|
import noCommentParagraph from "./rules/no-comment-paragraph.mjs";
|
|
5
7
|
import nameThePattern from "./rules/name-the-pattern.mjs";
|
|
6
8
|
import noPromiseThen from "./rules/no-promise-then.mjs";
|
|
@@ -8,6 +10,7 @@ import noNumberInComment from "./rules/no-number-in-comment.mjs";
|
|
|
8
10
|
import noOffsetPagination from "./rules/no-offset-pagination.mjs";
|
|
9
11
|
import noOrchestrationInTrigger from "./rules/no-orchestration-in-trigger.mjs";
|
|
10
12
|
import noRawFetch from "./rules/no-raw-fetch.mjs";
|
|
13
|
+
import noSqlRaw from "./rules/no-sql-raw.mjs";
|
|
11
14
|
import noStatusLiteral from "./rules/no-status-literal.mjs";
|
|
12
15
|
import noSupersessionTrail from "./rules/no-supersession-trail.mjs";
|
|
13
16
|
import scopedRepository from "./rules/scoped-repository.mjs";
|
|
@@ -18,7 +21,9 @@ import tenantScopedTable from "./rules/tenant-scoped-table.mjs";
|
|
|
18
21
|
export const rules = {
|
|
19
22
|
"durable-idempotency-key": durableIdempotencyKey,
|
|
20
23
|
"external-service-only-in-adapter": externalServiceOnlyInAdapter,
|
|
24
|
+
"ime-safe-key": imeSafeKey,
|
|
21
25
|
"no-cross-feature-internals": noCrossFeatureInternals,
|
|
26
|
+
"registry-literal": registryLiteral,
|
|
22
27
|
"no-comment-paragraph": noCommentParagraph,
|
|
23
28
|
"name-the-pattern": nameThePattern,
|
|
24
29
|
"no-promise-then": noPromiseThen,
|
|
@@ -26,6 +31,7 @@ export const rules = {
|
|
|
26
31
|
"no-offset-pagination": noOffsetPagination,
|
|
27
32
|
"no-orchestration-in-trigger": noOrchestrationInTrigger,
|
|
28
33
|
"no-raw-fetch": noRawFetch,
|
|
34
|
+
"no-sql-raw": noSqlRaw,
|
|
29
35
|
"no-status-literal": noStatusLiteral,
|
|
30
36
|
"no-supersession-trail": noSupersessionTrail,
|
|
31
37
|
"scoped-repository": scopedRepository,
|
package/src/eslint/preset.mjs
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
// Thresholds are imported, never restated — quality-thresholds.json is their one home.
|
|
3
3
|
|
|
4
4
|
import qc from "./index.mjs";
|
|
5
|
-
import { enabled, load, thresholds } from "../config.mjs";
|
|
5
|
+
import { enabled, load, readerEntries, thresholds } from "../config.mjs";
|
|
6
6
|
|
|
7
7
|
/** Options each rule needs, derived from one config so a name is written once. */
|
|
8
|
-
function ruleOptions(config) {
|
|
8
|
+
function ruleOptions(config, registry) {
|
|
9
9
|
return {
|
|
10
|
+
"no-sql-raw": { allow: config.sqlRaw.allow },
|
|
11
|
+
"ime-safe-key": { components: config.ime.components, guards: config.ime.guards },
|
|
12
|
+
"registry-literal": { entries: readerEntries(registry).filter((entry) => entry.names.length > 0) },
|
|
10
13
|
"no-comment-paragraph": {
|
|
11
14
|
doc: config.enforcement ?? "docs/enforcement.md",
|
|
12
15
|
decisions: config.docs.decisions,
|
|
@@ -48,8 +51,8 @@ function ruleOptions(config) {
|
|
|
48
51
|
};
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
function entries(config, names) {
|
|
52
|
-
const options = ruleOptions(config);
|
|
54
|
+
function entries(config, names, registry = {}) {
|
|
55
|
+
const options = ruleOptions(config, registry);
|
|
53
56
|
const out = {};
|
|
54
57
|
for (const name of names) {
|
|
55
58
|
if (!enabled(config.rules, name)) continue;
|
|
@@ -75,13 +78,16 @@ const UNIVERSAL = [
|
|
|
75
78
|
// Not a layered-architecture concern like the SERVER rules below — a worker or a
|
|
76
79
|
// composition root outside backend/src needs this exactly as much as backend/src does.
|
|
77
80
|
"external-service-only-in-adapter",
|
|
81
|
+
// A worker or a script builds a query and holds a number as much as a feature does.
|
|
82
|
+
"no-sql-raw",
|
|
83
|
+
"registry-literal",
|
|
78
84
|
];
|
|
79
85
|
|
|
80
86
|
/** Rules that only make sense where the server's storage and ports live. */
|
|
81
87
|
const SERVER = ["storage-only-in-resource", "tenant-scoped-table", "signal-last-param"];
|
|
82
88
|
|
|
83
89
|
/** Rules that only make sense in the browser. */
|
|
84
|
-
const CLIENT = ["no-raw-fetch", "storage-only-in-resource"];
|
|
90
|
+
const CLIENT = ["no-raw-fetch", "storage-only-in-resource", "ime-safe-key"];
|
|
85
91
|
|
|
86
92
|
/**
|
|
87
93
|
* The architecture as a flat config. Spread it, then append your own blocks.
|
|
@@ -96,13 +102,22 @@ export function preset(options = {}) {
|
|
|
96
102
|
const max = (id) => gates[id]?.value;
|
|
97
103
|
const { tseslint, boundaries, sonarjs } = options.plugins ?? {};
|
|
98
104
|
|
|
99
|
-
|
|
105
|
+
// No file carries its own exemption: a rule is switched off in config, where a reviewer sees it.
|
|
106
|
+
const blocks = [{ ignores: config.ignores }, { linterOptions: { noInlineConfig: true, reportUnusedDisableDirectives: "error" } }];
|
|
100
107
|
|
|
101
108
|
const base = {
|
|
102
109
|
files: ["**/*.{ts,tsx,mjs}"],
|
|
103
110
|
plugins: { qc, ...(tseslint ? { "@typescript-eslint": tseslint } : {}), ...(sonarjs ? { sonarjs } : {}) },
|
|
104
111
|
rules: {
|
|
105
|
-
...(tseslint
|
|
112
|
+
...(tseslint
|
|
113
|
+
? {
|
|
114
|
+
"@typescript-eslint/no-explicit-any": "error",
|
|
115
|
+
"@typescript-eslint/ban-ts-comment": [
|
|
116
|
+
"error",
|
|
117
|
+
{ "ts-ignore": true, "ts-nocheck": true, "ts-check": false, "ts-expect-error": "allow-with-description" },
|
|
118
|
+
],
|
|
119
|
+
}
|
|
120
|
+
: {}),
|
|
106
121
|
...(sonarjs
|
|
107
122
|
? {
|
|
108
123
|
"sonarjs/no-identical-functions": "error",
|
|
@@ -116,7 +131,7 @@ export function preset(options = {}) {
|
|
|
116
131
|
? { "max-lines-per-function": ["error", { max: max("funclength"), skipBlankLines: true, skipComments: true }] }
|
|
117
132
|
: {}),
|
|
118
133
|
...(max("nesting") ? { "max-depth": ["error", max("nesting")] } : {}),
|
|
119
|
-
...entries(config, UNIVERSAL),
|
|
134
|
+
...entries(config, UNIVERSAL, gates),
|
|
120
135
|
},
|
|
121
136
|
};
|
|
122
137
|
if (options.languageOptions) base.languageOptions = options.languageOptions;
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { Linter } from "eslint";
|
|
7
|
+
import tseslint from "@typescript-eslint/eslint-plugin";
|
|
8
|
+
import tsparser from "@typescript-eslint/parser";
|
|
3
9
|
import { defaults, merge } from "../config.mjs";
|
|
4
10
|
import { rules as allRules } from "./index.mjs";
|
|
5
11
|
import { preset } from "./preset.mjs";
|
|
@@ -80,6 +86,62 @@ test("a repository with no layers still gets the browser rules", () => {
|
|
|
80
86
|
assert.ok(client.rules["qc/no-raw-fetch"]);
|
|
81
87
|
});
|
|
82
88
|
|
|
89
|
+
/** One file through the whole preset, the way a consumer's `eslint .` sees it. */
|
|
90
|
+
function lint(code, filename, override = {}) {
|
|
91
|
+
const blocks = preset({
|
|
92
|
+
config: config(override),
|
|
93
|
+
plugins: { tseslint },
|
|
94
|
+
languageOptions: { parser: tsparser, ecmaVersion: 2023, sourceType: "module" },
|
|
95
|
+
});
|
|
96
|
+
return new Linter({ configType: "flat" }).verify(code, blocks, filename);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const RESOURCE = "backend/src/features/pins/resource.ts";
|
|
100
|
+
const MIGRATE = "backend/src/infrastructure/db/migrate.ts";
|
|
101
|
+
const byRule = (messages, ruleId) => messages.filter((m) => m.ruleId === ruleId);
|
|
102
|
+
|
|
103
|
+
test("a registry entry with names reaches qc/registry-literal, and one without stays out", () => {
|
|
104
|
+
const root = mkdtempSync(path.join(tmpdir(), "qc-preset-"));
|
|
105
|
+
const holdpress = { value: 400, names: ["HOLD_\\w*MS"], readers: ["frontend/src/platform/gesture-thresholds.ts"] };
|
|
106
|
+
writeFileSync(path.join(root, "quality-thresholds.json"), JSON.stringify({ gates: { holdpress, coverage: { value: 70 } } }));
|
|
107
|
+
const setting = emitted(preset({ config: { ...config(), root } })).get("qc/registry-literal");
|
|
108
|
+
assert.deepEqual(setting[1].entries, [{ key: "holdpress", names: holdpress.names, readers: holdpress.readers }]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("the ime config reaches qc/ime-safe-key", () => {
|
|
112
|
+
const setting = emitted(preset({ config: config({ ime: { guards: ["isImeComposing"] } }) })).get("qc/ime-safe-key");
|
|
113
|
+
assert.deepEqual(setting[1], { components: [], guards: ["isImeComposing"] });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("the preset refuses an inline config comment in every file", () => {
|
|
117
|
+
const block = preset({ config: config() }).find((b) => b.linterOptions);
|
|
118
|
+
assert.ok(block, "no linterOptions block emitted");
|
|
119
|
+
assert.equal(block.files, undefined, "linterOptions must reach every file");
|
|
120
|
+
assert.deepEqual(block.linterOptions, { noInlineConfig: true, reportUnusedDisableDirectives: "error" });
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("an inline disable comment does not silence a rule", () => {
|
|
124
|
+
const messages = lint("// eslint-disable-next-line qc/no-sql-raw\nawait db.execute(sql.raw(text));\n", RESOURCE);
|
|
125
|
+
assert.equal(byRule(messages, "qc/no-sql-raw").length, 1, "the disable comment silenced the rule");
|
|
126
|
+
const directive = messages.find((m) => m.ruleId === null && /noInlineConfig/.test(m.message));
|
|
127
|
+
assert.ok(directive, "the ignored disable comment is not reported");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("sql.raw fails, and sqlRaw.allow lets one reviewed file through", () => {
|
|
131
|
+
const code = "await db.execute(sql.raw(ddl));\n";
|
|
132
|
+
assert.equal(byRule(lint(code, RESOURCE), "qc/no-sql-raw")[0]?.severity, 2);
|
|
133
|
+
assert.equal(byRule(lint(code, MIGRATE, { sqlRaw: { allow: [MIGRATE] } }), "qc/no-sql-raw").length, 0);
|
|
134
|
+
assert.equal(byRule(lint(code, RESOURCE, { sqlRaw: { allow: [MIGRATE] } }), "qc/no-sql-raw").length, 1);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("ts-ignore and ts-nocheck fail; ts-expect-error passes only with a description", () => {
|
|
138
|
+
const banned = (code) => byRule(lint(code, RESOURCE), "@typescript-eslint/ban-ts-comment").length;
|
|
139
|
+
assert.equal(banned("// @ts-ignore\nconst a = 1;\n"), 1);
|
|
140
|
+
assert.equal(banned("// @ts-nocheck\nconst a = 1;\n"), 1);
|
|
141
|
+
assert.equal(banned("// @ts-expect-error\nconst a = 1;\n"), 1);
|
|
142
|
+
assert.equal(banned("// @ts-expect-error: the vendor type omits this field\nconst a = 1;\n"), 0);
|
|
143
|
+
});
|
|
144
|
+
|
|
83
145
|
test("the frontendLayers block is emitted when configured and boundaries plugin is supplied", () => {
|
|
84
146
|
const feConfig = config({
|
|
85
147
|
frontendLayers: {
|