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,68 @@
|
|
|
1
|
+
// The kit ships every rule on. A repository may switch one off -- some do not apply to its shape --
|
|
2
|
+
// but not silently: the switch names a decision id, and the citations gate resolves that id against
|
|
3
|
+
// the register. Turning a rule off becomes a reviewed act with a reason attached, which is the
|
|
4
|
+
// difference between adopting a standard and negotiating with it.
|
|
5
|
+
|
|
6
|
+
const OFF_BY_DESIGN = "off-by-design";
|
|
7
|
+
|
|
8
|
+
/** @returns every check the kit ships that this repository has switched off. */
|
|
9
|
+
export function switchedOff(shipped, configured) {
|
|
10
|
+
return Object.keys(shipped)
|
|
11
|
+
.filter((name) => shipped[name] === true)
|
|
12
|
+
.filter((name) => configured[name] === false)
|
|
13
|
+
.sort();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {{gates: object, rules: object}} shipped the kit's own defaults
|
|
18
|
+
* @param {{gates: object, rules: object}} configured this repository's resolved config
|
|
19
|
+
* @param {{exemptions?: Record<string,string>, pattern?: RegExp}} [options]
|
|
20
|
+
*/
|
|
21
|
+
export function checkConfigFloor(shipped, configured, options = {}) {
|
|
22
|
+
const exemptions = options.exemptions ?? {};
|
|
23
|
+
const pattern = options.pattern ?? /^[A-Z]{2,5}-\d{3,4}$/;
|
|
24
|
+
const problems = [];
|
|
25
|
+
|
|
26
|
+
for (const kind of ["gates", "rules"]) {
|
|
27
|
+
for (const name of switchedOff(shipped[kind] ?? {}, configured[kind] ?? {})) {
|
|
28
|
+
const cited = exemptions[name];
|
|
29
|
+
if (cited === undefined) {
|
|
30
|
+
problems.push({
|
|
31
|
+
path: "qc.config.json",
|
|
32
|
+
rule: "unexempted-opt-out",
|
|
33
|
+
detail:
|
|
34
|
+
`${kind}.${name} is switched off, and the kit ships it on. Name the decision that says ` +
|
|
35
|
+
`why under floor.exemptions, or "${OFF_BY_DESIGN}" when the rule cannot apply to this repository's shape.`,
|
|
36
|
+
});
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (cited !== OFF_BY_DESIGN && !pattern.test(cited)) {
|
|
40
|
+
problems.push({
|
|
41
|
+
path: "qc.config.json",
|
|
42
|
+
rule: "unexempted-opt-out",
|
|
43
|
+
detail: `floor.exemptions.${name} is "${cited}", which is neither a decision id nor "${OFF_BY_DESIGN}".`,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// An exemption for something that is not off is a stale reason nobody will notice going wrong.
|
|
49
|
+
const off = new Set([
|
|
50
|
+
...switchedOff(shipped.gates ?? {}, configured.gates ?? {}),
|
|
51
|
+
...switchedOff(shipped.rules ?? {}, configured.rules ?? {}),
|
|
52
|
+
]);
|
|
53
|
+
for (const name of Object.keys(exemptions).sort()) {
|
|
54
|
+
if (!off.has(name)) {
|
|
55
|
+
problems.push({
|
|
56
|
+
path: "qc.config.json",
|
|
57
|
+
rule: "stale-exemption",
|
|
58
|
+
detail: `floor.exemptions.${name} exempts a check that is in force. Delete the exemption.`,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return problems;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The ids an exemption block cites, for the citations gate to resolve. */
|
|
66
|
+
export function exemptionIds(exemptions = {}) {
|
|
67
|
+
return [...new Set(Object.values(exemptions).filter((value) => value !== OFF_BY_DESIGN))];
|
|
68
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { checkConfigFloor, exemptionIds, switchedOff } from "./config-floor.mjs";
|
|
4
|
+
|
|
5
|
+
const shipped = { gates: { citations: true, "english-source": true }, rules: { "no-promise-then": true } };
|
|
6
|
+
const allOn = { gates: { citations: true, "english-source": true }, rules: { "no-promise-then": true } };
|
|
7
|
+
|
|
8
|
+
test("a repository running every shipped check has no findings", () => {
|
|
9
|
+
assert.deepEqual(checkConfigFloor(shipped, allOn), []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("switching a shipped check off without a reason is a finding", () => {
|
|
13
|
+
const off = { gates: { citations: true, "english-source": false }, rules: allOn.rules };
|
|
14
|
+
const [problem] = checkConfigFloor(shipped, off);
|
|
15
|
+
assert.equal(problem.rule, "unexempted-opt-out");
|
|
16
|
+
assert.match(problem.detail, /gates\.english-source is switched off/);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("a decision id makes the opt-out legitimate", () => {
|
|
20
|
+
const off = { gates: { citations: true, "english-source": false }, rules: allOn.rules };
|
|
21
|
+
assert.deepEqual(checkConfigFloor(shipped, off, { exemptions: { "english-source": "QC-011" } }), []);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("a rule that cannot apply to this repository's shape says so by name", () => {
|
|
25
|
+
const off = { gates: { citations: true, "english-source": false }, rules: allOn.rules };
|
|
26
|
+
assert.deepEqual(checkConfigFloor(shipped, off, { exemptions: { "english-source": "off-by-design" } }), []);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("prose in place of a decision id is refused, so the escape hatch stays narrow", () => {
|
|
30
|
+
const off = { gates: { citations: true, "english-source": false }, rules: allOn.rules };
|
|
31
|
+
const [problem] = checkConfigFloor(shipped, off, { exemptions: { "english-source": "we will do it later" } });
|
|
32
|
+
assert.match(problem.detail, /neither a decision id nor/);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("a lint rule switched off is held to the same floor as a gate", () => {
|
|
36
|
+
const off = { gates: allOn.gates, rules: { "no-promise-then": false } };
|
|
37
|
+
assert.equal(checkConfigFloor(shipped, off)[0].detail.startsWith("rules.no-promise-then"), true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("an exemption for a check that is on is stale and must be deleted", () => {
|
|
41
|
+
const [problem] = checkConfigFloor(shipped, allOn, { exemptions: { citations: "QC-011" } });
|
|
42
|
+
assert.equal(problem.rule, "stale-exemption");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a check the kit does not ship on is not held to the floor", () => {
|
|
46
|
+
const optional = { gates: { citations: true, extra: false }, rules: {} };
|
|
47
|
+
assert.deepEqual(switchedOff(optional.gates, { citations: true, extra: false }), []);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("cited ids are collected for the citations gate, and off-by-design is not one", () => {
|
|
51
|
+
assert.deepEqual(exemptionIds({ a: "QC-011", b: "off-by-design", c: "ADR-0031" }), ["QC-011", "ADR-0031"]);
|
|
52
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// One API description, composed from the files features own. A feature writes
|
|
2
|
+
// `contracts/paths/<name>.yaml` and nothing else; the linted root is generated,
|
|
3
|
+
// so no work order edits a shared document. .claude/rules/swarm.md.
|
|
4
|
+
|
|
5
|
+
/** @param {Record<string, unknown>} into @param {Record<string, unknown>} from */
|
|
6
|
+
function mergeDisjoint(into, from, label, owner, owners) {
|
|
7
|
+
for (const key of Object.keys(from)) {
|
|
8
|
+
const held = owners.get(`${label}:${key}`);
|
|
9
|
+
if (held) {
|
|
10
|
+
throw new Error(`${label} '${key}' is declared by both '${held}' and '${owner}'`);
|
|
11
|
+
}
|
|
12
|
+
owners.set(`${label}:${key}`, owner);
|
|
13
|
+
into[key] = from[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function sortKeys(record) {
|
|
18
|
+
return Object.fromEntries(Object.keys(record).sort().map((key) => [key, record[key]]));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {Record<string, any>} base the hand-written root: info, servers, security, shared components
|
|
23
|
+
* @param {{owner: string, document: Record<string, any>}[]} slices one per feature
|
|
24
|
+
*/
|
|
25
|
+
export function composeContract(base, slices) {
|
|
26
|
+
const owners = new Map();
|
|
27
|
+
const paths = {};
|
|
28
|
+
const schemas = { ...(base.components?.schemas ?? {}) };
|
|
29
|
+
for (const { owner, document } of slices) {
|
|
30
|
+
mergeDisjoint(paths, document.paths ?? {}, "path", owner, owners);
|
|
31
|
+
mergeDisjoint(schemas, document.components?.schemas ?? {}, "schema", owner, owners);
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
...base,
|
|
35
|
+
paths: sortKeys(paths),
|
|
36
|
+
components: { ...base.components, schemas: sortKeys(schemas) },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { composeContract } from "./contract-compose.mjs";
|
|
4
|
+
|
|
5
|
+
const base = { openapi: "3.1.0", paths: {}, components: { securitySchemes: { bearerAuth: {} } } };
|
|
6
|
+
|
|
7
|
+
const alpha = {
|
|
8
|
+
owner: "alpha",
|
|
9
|
+
document: { paths: { "/v1/alpha": { get: {} } }, components: { schemas: { Alpha: {} } } },
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
test("every feature's paths and schemas reach the one root", () => {
|
|
13
|
+
const composed = composeContract(base, [alpha]);
|
|
14
|
+
assert.deepEqual(Object.keys(composed.paths), ["/v1/alpha"]);
|
|
15
|
+
assert.deepEqual(Object.keys(composed.components.schemas), ["Alpha"]);
|
|
16
|
+
assert.deepEqual(composed.components.securitySchemes, { bearerAuth: {} });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("two features claiming one path fail, naming both", () => {
|
|
20
|
+
const twin = { owner: "beta", document: alpha.document };
|
|
21
|
+
assert.throws(() => composeContract(base, [alpha, twin]), /both 'alpha' and 'beta'/);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("two features claiming one schema name fail", () => {
|
|
25
|
+
const beta = { owner: "beta", document: { paths: { "/v1/beta": {} }, components: { schemas: { Alpha: {} } } } };
|
|
26
|
+
assert.throws(() => composeContract(base, [alpha, beta]), /schema 'Alpha'/);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("a feature that declares nothing contributes nothing", () => {
|
|
30
|
+
const composed = composeContract(base, [{ owner: "empty", document: { paths: {} } }]);
|
|
31
|
+
assert.deepEqual(composed.paths, {});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("output order does not depend on feature order", () => {
|
|
35
|
+
const beta = { owner: "beta", document: { paths: { "/v1/beta": {} }, components: { schemas: { Beta: {} } } } };
|
|
36
|
+
const one = composeContract(base, [alpha, beta]);
|
|
37
|
+
const two = composeContract(base, [beta, alpha]);
|
|
38
|
+
assert.deepEqual(Object.keys(one.paths), Object.keys(two.paths));
|
|
39
|
+
assert.deepEqual(Object.keys(one.components.schemas), Object.keys(two.components.schemas));
|
|
40
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// The feature anatomy, as a pure function. docs/architecture.md.
|
|
2
|
+
// The CLI does the I/O; every decision lives here so it can be tested.
|
|
3
|
+
//
|
|
4
|
+
// Two anatomies, both named by config: a bounded vertical slice, and the flat
|
|
5
|
+
// pipeline a narrow single-operation feature may keep.
|
|
6
|
+
|
|
7
|
+
export const BLOCKS = Object.freeze([
|
|
8
|
+
"index",
|
|
9
|
+
"trigger",
|
|
10
|
+
"pipeline",
|
|
11
|
+
"resource",
|
|
12
|
+
"branch",
|
|
13
|
+
"fragment",
|
|
14
|
+
"ledger",
|
|
15
|
+
"record",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export const REQUIRED_BLOCKS = Object.freeze(["index", "trigger", "pipeline", "resource"]);
|
|
19
|
+
|
|
20
|
+
export const SLICE_REQUIRED_ROOTS = Object.freeze(["index", "trigger", "schema"]);
|
|
21
|
+
export const ALLOWED_SUBDIRS = Object.freeze(["slices", "shared", "components"]);
|
|
22
|
+
export const ALLOWED_SHARED_FILES = Object.freeze(["types", "queries", "guards", "runner", "components"]);
|
|
23
|
+
|
|
24
|
+
const TEST_SUFFIX = ".test.ts";
|
|
25
|
+
const TEST_SUFFIX_TSX = ".test.tsx";
|
|
26
|
+
|
|
27
|
+
/** A config may name blocks with or without an extension; the gate compares stems. */
|
|
28
|
+
function stems(names) {
|
|
29
|
+
return names.map((name) => name.replace(/\.[cm]?[jt]sx?$/, ""));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Config, resolved once, with the reference anatomy as the default. */
|
|
33
|
+
function anatomyOf(options = {}) {
|
|
34
|
+
const slice = options.slice ?? {};
|
|
35
|
+
const block = options.block ?? {};
|
|
36
|
+
return {
|
|
37
|
+
sliceRequired: stems(slice.required ?? SLICE_REQUIRED_ROOTS),
|
|
38
|
+
sliceDir: slice.sliceDir ?? "slices",
|
|
39
|
+
sharedDir: slice.sharedDir ?? "shared",
|
|
40
|
+
componentsDir: slice.componentsDir ?? "components",
|
|
41
|
+
allowedSubdirs: slice.allowedSubdirs ?? [slice.sliceDir ?? "slices", slice.sharedDir ?? "shared", slice.componentsDir ?? "components"],
|
|
42
|
+
sharedFiles: stems(slice.sharedFiles ?? ALLOWED_SHARED_FILES),
|
|
43
|
+
blocks: stems([...(block.required ?? []), ...(block.optional ?? [])]).length
|
|
44
|
+
? stems([...(block.required ?? REQUIRED_BLOCKS), ...(block.optional ?? [])])
|
|
45
|
+
: BLOCKS,
|
|
46
|
+
blockRequired: stems(block.required ?? REQUIRED_BLOCKS),
|
|
47
|
+
// Roots where the flat anatomy is no longer allowed at all.
|
|
48
|
+
sliceOnlyRoots: options.sliceOnlyRoots ?? ["backend/src/features"],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function checkSliceSubfolder(feature, file, anatomy) {
|
|
53
|
+
const parts = file.split("/");
|
|
54
|
+
const dir = parts[0];
|
|
55
|
+
if (!anatomy.allowedSubdirs.includes(dir)) {
|
|
56
|
+
return { feature, rule: "disallowed-subfolder", detail: file };
|
|
57
|
+
}
|
|
58
|
+
if (dir === anatomy.componentsDir) {
|
|
59
|
+
if (parts.length > 3) {
|
|
60
|
+
return { feature, rule: "disallowed-subfolder", detail: file };
|
|
61
|
+
}
|
|
62
|
+
if (parts.length === 2) {
|
|
63
|
+
return {
|
|
64
|
+
feature,
|
|
65
|
+
rule: "loose-component",
|
|
66
|
+
detail: `${file}: UI components must be organized by view or entity under ${anatomy.componentsDir}/<view>/`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
if (parts.length > 2) {
|
|
72
|
+
return { feature, rule: "disallowed-subfolder", detail: file };
|
|
73
|
+
}
|
|
74
|
+
const basename = parts[1].replace(/\.test\.(tsx?)$/, "").replace(/\.(tsx?)$/, "");
|
|
75
|
+
if (basename === "index") {
|
|
76
|
+
return { feature, rule: "no-subfolder-index", detail: file };
|
|
77
|
+
}
|
|
78
|
+
if (dir === anatomy.sharedDir && !anatomy.sharedFiles.includes(basename)) {
|
|
79
|
+
return { feature, rule: "disallowed-shared-file", detail: file };
|
|
80
|
+
}
|
|
81
|
+
if (dir === anatomy.sliceDir) {
|
|
82
|
+
if (/-(?:rail|toolbar|dialogs?|fields)(?:\.test)?\.[cm]?[jt]sx?$/.test(file)) {
|
|
83
|
+
return {
|
|
84
|
+
feature,
|
|
85
|
+
rule: "ui-fragment-in-slices",
|
|
86
|
+
detail: `${file}: UI presentation fragments belong in components/<view>/, not in slices/`,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function checkSliceFeature(feature, files, anatomy) {
|
|
94
|
+
const problems = [];
|
|
95
|
+
const roots = new Set();
|
|
96
|
+
let hasSlice = false;
|
|
97
|
+
const componentViews = new Map();
|
|
98
|
+
|
|
99
|
+
for (const file of files) {
|
|
100
|
+
if (file.includes("/")) {
|
|
101
|
+
const subProb = checkSliceSubfolder(feature, file, anatomy);
|
|
102
|
+
if (subProb) {
|
|
103
|
+
problems.push(subProb);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (file.startsWith(`${anatomy.componentsDir}/`)) {
|
|
107
|
+
const parts = file.split("/");
|
|
108
|
+
if (parts.length === 3) {
|
|
109
|
+
const view = parts[1];
|
|
110
|
+
if (!componentViews.has(view)) componentViews.set(view, new Set());
|
|
111
|
+
componentViews.get(view).add(parts[2]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (file.endsWith(TEST_SUFFIX) || file.endsWith(TEST_SUFFIX_TSX)) continue;
|
|
115
|
+
if (file.startsWith(`${anatomy.sliceDir}/`)) hasSlice = true;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (file.endsWith(TEST_SUFFIX) || file.endsWith(TEST_SUFFIX_TSX)) continue;
|
|
119
|
+
const name = file.replace(/\.tsx?$/, "");
|
|
120
|
+
if (!anatomy.sliceRequired.includes(name)) {
|
|
121
|
+
problems.push({ feature, rule: "unknown-block", detail: file });
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
roots.add(name);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const [view, viewFiles] of componentViews) {
|
|
128
|
+
const hasIndex = [...viewFiles].some((f) => /^index\.[cm]?[jt]sx?$/.test(f));
|
|
129
|
+
if (!hasIndex) {
|
|
130
|
+
problems.push({
|
|
131
|
+
feature,
|
|
132
|
+
rule: "missing-component-index",
|
|
133
|
+
detail: `${anatomy.componentsDir}/${view}/index.ts: component view requires an index block for discovery`,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const required of anatomy.sliceRequired) {
|
|
139
|
+
if (!roots.has(required)) problems.push({ feature, rule: "missing-block", detail: required });
|
|
140
|
+
}
|
|
141
|
+
if (!hasSlice) {
|
|
142
|
+
problems.push({
|
|
143
|
+
feature,
|
|
144
|
+
rule: "empty-slices",
|
|
145
|
+
detail: `no slice files found under ${anatomy.sliceDir}/`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return problems;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function checkFlatFeature(feature, files, anatomy) {
|
|
152
|
+
const problems = [];
|
|
153
|
+
const blocks = new Set();
|
|
154
|
+
for (const file of files) {
|
|
155
|
+
if (file.includes("/")) {
|
|
156
|
+
problems.push({ feature, rule: "no-subfolder", detail: file });
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (file.endsWith(TEST_SUFFIX) || file.endsWith(TEST_SUFFIX_TSX)) continue;
|
|
160
|
+
const name = file.replace(/\.tsx?$/, "");
|
|
161
|
+
if (!anatomy.blocks.includes(name)) {
|
|
162
|
+
problems.push({ feature, rule: "unknown-block", detail: file });
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
blocks.add(name);
|
|
166
|
+
}
|
|
167
|
+
for (const required of anatomy.blockRequired) {
|
|
168
|
+
if (!blocks.has(required)) problems.push({ feature, rule: "missing-block", detail: required });
|
|
169
|
+
}
|
|
170
|
+
return problems;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @param {{feature: string, files: string[]}[]} features
|
|
175
|
+
* `files` are basenames directly inside the feature folder; a nested path
|
|
176
|
+
* keeps its separator so the subfolder rule can see it.
|
|
177
|
+
* @returns {{feature: string, rule: string, detail: string}[]}
|
|
178
|
+
*/
|
|
179
|
+
export function checkFeatureAnatomy(features, options = {}) {
|
|
180
|
+
const anatomy = anatomyOf(options);
|
|
181
|
+
const schemaFiles = anatomy.sliceRequired.filter((name) => name !== "index" && name !== "trigger");
|
|
182
|
+
const problems = [];
|
|
183
|
+
for (const { feature, files } of features) {
|
|
184
|
+
const isSlice = files.some(
|
|
185
|
+
(file) =>
|
|
186
|
+
file.startsWith(`${anatomy.sliceDir}/`) ||
|
|
187
|
+
schemaFiles.includes(file.replace(/\.tsx?$/, "")),
|
|
188
|
+
);
|
|
189
|
+
if (!isSlice && anatomy.sliceOnlyRoots.some((root) => feature.includes(root))) {
|
|
190
|
+
problems.push({
|
|
191
|
+
feature,
|
|
192
|
+
rule: "disallowed-flat-anatomy",
|
|
193
|
+
detail: `features under ${anatomy.sliceOnlyRoots.find((root) => feature.includes(root))} must use bounded vertical slices`,
|
|
194
|
+
});
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const featureProblems = isSlice
|
|
198
|
+
? checkSliceFeature(feature, files, anatomy)
|
|
199
|
+
: checkFlatFeature(feature, files, anatomy);
|
|
200
|
+
problems.push(...featureProblems);
|
|
201
|
+
}
|
|
202
|
+
return problems;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** An empty block or slice is a review finding, not an option. ADR-0047, ADR-0054. */
|
|
206
|
+
export function checkNoEmptyBlock(files, options = {}) {
|
|
207
|
+
const anatomy = anatomyOf(options);
|
|
208
|
+
const known = new Set([...anatomy.blocks, ...anatomy.sliceRequired]);
|
|
209
|
+
return files
|
|
210
|
+
.filter(({ path, contents }) => {
|
|
211
|
+
const name = path.split("/").pop().replace(/\.tsx?$/, "");
|
|
212
|
+
const isBlock = known.has(name);
|
|
213
|
+
const isSubdirFile =
|
|
214
|
+
path.includes(`/${anatomy.sliceDir}/`) || path.includes(`/${anatomy.sharedDir}/`);
|
|
215
|
+
if (!isBlock && !isSubdirFile) return false;
|
|
216
|
+
const meaningful = contents
|
|
217
|
+
.split("\n")
|
|
218
|
+
.map((line) => line.trim())
|
|
219
|
+
.filter((line) => line && !line.startsWith("//") && !line.startsWith("*") && !line.startsWith("/*"));
|
|
220
|
+
return meaningful.length === 0;
|
|
221
|
+
})
|
|
222
|
+
.map(({ path }) => ({ feature: path, rule: "empty-block", detail: path }));
|
|
223
|
+
}
|