@sdxc/kit 2026.10.20 → 2026.10.23
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/README.md +178 -93
- package/changelog.jsonl +97 -0
- package/config.schema.json +160 -0
- package/dist/architecture/complexity.d.ts +73 -0
- package/dist/architecture/complexity.js +185 -0
- package/dist/architecture/crap.d.ts +62 -0
- package/dist/architecture/crap.js +121 -0
- package/dist/architecture/graph.d.ts +55 -0
- package/dist/architecture/graph.js +194 -0
- package/dist/architecture/layers.d.ts +29 -0
- package/dist/architecture/layers.js +81 -0
- package/dist/architecture/mermaid.d.ts +20 -0
- package/dist/architecture/mermaid.js +51 -0
- package/dist/cli.js +9 -0
- package/dist/commands/create.js +4 -3
- package/dist/commands/deploy.js +10 -9
- package/dist/commands/destroy.d.ts +1 -1
- package/dist/commands/diagram.d.ts +17 -0
- package/dist/commands/diagram.js +45 -0
- package/dist/commands/fix.d.ts +69 -0
- package/dist/commands/fix.js +102 -0
- package/dist/commands/make.d.ts +77 -2
- package/dist/commands/make.js +53 -1
- package/dist/commands/run.d.ts +15 -11
- package/dist/commands/run.js +21 -16
- package/dist/commands/scaffold.d.ts +2 -1
- package/dist/commands/scaffold.js +1 -0
- package/dist/commands/setup.d.ts +26 -0
- package/dist/commands/setup.js +33 -0
- package/dist/config.d.ts +54 -0
- package/dist/config.js +92 -1
- package/dist/contract.d.ts +56 -3
- package/dist/contract.js +140 -7
- package/dist/dependencies.d.ts +2 -1
- package/dist/dependencies.js +11 -2
- package/dist/deploy/plan.d.ts +5 -0
- package/dist/deploy/plan.js +10 -0
- package/dist/destroy.d.ts +4 -4
- package/dist/destroy.js +24 -11
- package/dist/diagram.d.ts +32 -0
- package/dist/diagram.js +47 -0
- package/dist/doctor/checks.d.ts +2 -1
- package/dist/doctor/checks.js +26 -1
- package/dist/doctor/complexity.d.ts +13 -0
- package/dist/doctor/complexity.js +52 -0
- package/dist/doctor/crap.d.ts +14 -0
- package/dist/doctor/crap.js +67 -0
- package/dist/doctor/dependencies.d.ts +13 -0
- package/dist/doctor/dependencies.js +42 -0
- package/dist/doctor/duplication.d.ts +19 -0
- package/dist/doctor/duplication.js +106 -0
- package/dist/doctor/fan-out.d.ts +12 -0
- package/dist/doctor/fan-out.js +42 -0
- package/dist/doctor/feature-scatter.d.ts +15 -0
- package/dist/doctor/feature-scatter.js +84 -0
- package/dist/doctor/jsdoc.d.ts +44 -0
- package/dist/doctor/jsdoc.js +201 -0
- package/dist/doctor/module-size.d.ts +13 -0
- package/dist/doctor/module-size.js +50 -0
- package/dist/doctor/mutation.d.ts +20 -0
- package/dist/doctor/mutation.js +97 -0
- package/dist/doctor/orphans.d.ts +13 -0
- package/dist/doctor/orphans.js +50 -0
- package/dist/doctor/project.js +8 -5
- package/dist/doctor/result-throws.d.ts +17 -0
- package/dist/doctor/result-throws.js +72 -0
- package/dist/doctor/test-ratio.d.ts +12 -0
- package/dist/doctor/test-ratio.js +31 -0
- package/dist/fix/commands.d.ts +37 -0
- package/dist/fix/commands.js +63 -0
- package/dist/fix/emails.d.ts +21 -0
- package/dist/fix/emails.js +159 -0
- package/dist/fix/env.d.ts +29 -0
- package/dist/fix/env.js +58 -0
- package/dist/fix/index.d.ts +11 -0
- package/dist/fix/index.js +11 -0
- package/dist/fix/models.d.ts +20 -0
- package/dist/fix/models.js +95 -0
- package/dist/lock.js +12 -2
- package/dist/make/binding.d.ts +42 -0
- package/dist/make/binding.js +237 -0
- package/dist/make/generate.d.ts +11 -2
- package/dist/make/generate.js +47 -16
- package/dist/make/imports.js +7 -5
- package/dist/make/index.d.ts +4 -2
- package/dist/make/index.js +4 -1
- package/dist/make/kinds.d.ts +5 -0
- package/dist/make/kinds.js +14 -2
- package/dist/make/schema-diff.js +8 -1
- package/dist/make/stubs.d.ts +7 -2
- package/dist/make/stubs.js +8 -3
- package/dist/make/test.js +1 -0
- package/dist/models.js +7 -7
- package/dist/routes/modules.js +8 -5
- package/dist/routes/parse.js +8 -7
- package/dist/routes/routers.js +9 -5
- package/dist/scaffold/index.d.ts +2 -0
- package/dist/scaffold/index.js +13 -4
- package/dist/scaffold/render.d.ts +11 -1
- package/dist/scaffold/render.js +86 -1
- package/dist/setup/auth.d.ts +12 -0
- package/dist/setup/auth.js +209 -0
- package/dist/setup/billing.d.ts +11 -0
- package/dist/setup/billing.js +116 -0
- package/dist/setup/catalog.d.ts +25 -0
- package/dist/setup/catalog.js +34 -0
- package/dist/setup/credit.d.ts +13 -0
- package/dist/setup/credit.js +21 -0
- package/dist/setup/flags.d.ts +11 -0
- package/dist/setup/flags.js +62 -0
- package/dist/setup/index.d.ts +25 -0
- package/dist/setup/index.js +53 -0
- package/dist/setup/plan.d.ts +38 -0
- package/dist/setup/plan.js +62 -0
- package/dist/testing.js +3 -3
- package/dist/ui/layout.js +1 -1
- package/dist/ui/registry.d.ts +2 -2
- package/dist/upgrade/project.d.ts +2 -2
- package/dist/upgrade/prompt.js +7 -5
- package/package.json +10 -7
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/sidebar.json +3 -3
- package/registry/ui/typeset.json +3 -3
- package/stubs/command.spec.stub +15 -0
- package/stubs/controller.api.spec.stub +15 -0
- package/stubs/controller.spec.stub +15 -0
- package/stubs/mcp.spec.stub +16 -0
- package/stubs/scaffold.migration.stub +2 -1
- package/stubs/scaffold.spec.stub +7 -0
- package/templates/app/AGENTS.md +21 -4
- package/templates/app/README.md +6 -3
- package/templates/app/app/commands/typegen.test.ts +20 -1
- package/templates/app/app/commands/typegen.ts +16 -2
- package/templates/app/app/initializers/cli.ts +7 -0
- package/templates/app/app/initializers/mail-transport.test.ts +8 -2
- package/templates/app/app/test/cloudflare-workers.ts +2 -0
- package/templates/app/package.json +1 -0
- package/templates/app/spec/config.jsonc +16 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cyclomatic complexity per function, from an oxc AST walk: each decision point adds one to a
|
|
3
|
+
* base of one, so a straight-line function scores 1 and an `if` raises it to 2. Doctor and CRAP
|
|
4
|
+
* both read this so the same function never gets two answers.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { parseSync } from "oxc-parser";
|
|
10
|
+
/** Node types that open a function whose body is measured on its own. */
|
|
11
|
+
const FUNCTION_TYPES = new Set([
|
|
12
|
+
"FunctionDeclaration",
|
|
13
|
+
"FunctionExpression",
|
|
14
|
+
"ArrowFunctionExpression",
|
|
15
|
+
"TSDeclareFunction",
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* Every function in `source` with its cyclomatic complexity. A file oxc cannot parse yields an
|
|
19
|
+
* empty list so the type checker, not doctor, names the syntax error.
|
|
20
|
+
*
|
|
21
|
+
* @param filename - Path reported for language detection (`.ts` / `.tsx`).
|
|
22
|
+
* @param source - Module text.
|
|
23
|
+
*/
|
|
24
|
+
export function measureComplexity(filename, source) {
|
|
25
|
+
let parsed = parseSync(filename, source);
|
|
26
|
+
if (parsed.errors.length > 0 && parsed.program.body.length === 0)
|
|
27
|
+
return [];
|
|
28
|
+
let findings = [];
|
|
29
|
+
visit(parsed.program, source, findings, null);
|
|
30
|
+
return findings;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Walks `node`, recording each function and descending into nested ones without double-counting
|
|
34
|
+
* a method's `FunctionExpression` under its `MethodDefinition`.
|
|
35
|
+
*/
|
|
36
|
+
function visit(node, source, findings, className) {
|
|
37
|
+
if (node == null || typeof node !== "object" || typeof node.type !== "string")
|
|
38
|
+
return;
|
|
39
|
+
if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
|
|
40
|
+
let name = node.id?.name ?? className;
|
|
41
|
+
for (let child of children(node))
|
|
42
|
+
visit(child, source, findings, name ?? null);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (node.type === "MethodDefinition" || node.type === "PropertyDefinition") {
|
|
46
|
+
let method = methodName(node, className);
|
|
47
|
+
if (node.value && FUNCTION_TYPES.has(node.value.type)) {
|
|
48
|
+
recordFunction(node.value, source, findings, method);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
for (let child of children(node))
|
|
52
|
+
visit(child, source, findings, className);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (node.type === "Property" && node.value && FUNCTION_TYPES.has(node.value.type)) {
|
|
56
|
+
recordFunction(node.value, source, findings, propertyName(node) ?? "(anonymous)");
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (FUNCTION_TYPES.has(node.type)) {
|
|
60
|
+
recordFunction(node, source, findings, functionName(node, className));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (let child of children(node))
|
|
64
|
+
visit(child, source, findings, className);
|
|
65
|
+
}
|
|
66
|
+
/** Records one function and walks nested functions inside its body. */
|
|
67
|
+
function recordFunction(node, source, findings, name) {
|
|
68
|
+
if (node.type === "TSDeclareFunction" || node.body == null)
|
|
69
|
+
return;
|
|
70
|
+
findings.push({
|
|
71
|
+
name,
|
|
72
|
+
line: lineOf(source, node.start ?? 0),
|
|
73
|
+
complexity: complexityOf(node),
|
|
74
|
+
});
|
|
75
|
+
for (let child of children(node.body))
|
|
76
|
+
visit(child, source, findings, null);
|
|
77
|
+
for (let param of node.params ?? [])
|
|
78
|
+
visit(param, source, findings, null);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* McCabe complexity of `node`'s own body: nested functions contribute their entry edge only
|
|
82
|
+
* through separate records, not through this count.
|
|
83
|
+
*/
|
|
84
|
+
export function complexityOf(node) {
|
|
85
|
+
let score = 1;
|
|
86
|
+
walkDecisions(node.body ?? node, (decision) => {
|
|
87
|
+
score += decision;
|
|
88
|
+
});
|
|
89
|
+
return score;
|
|
90
|
+
}
|
|
91
|
+
/** Visits decision points inside `node`, skipping nested function boundaries. */
|
|
92
|
+
function walkDecisions(node, add) {
|
|
93
|
+
if (node == null)
|
|
94
|
+
return;
|
|
95
|
+
if (Array.isArray(node)) {
|
|
96
|
+
for (let child of node)
|
|
97
|
+
walkDecisions(child, add);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (typeof node !== "object" || typeof node.type !== "string")
|
|
101
|
+
return;
|
|
102
|
+
if (FUNCTION_TYPES.has(node.type))
|
|
103
|
+
return;
|
|
104
|
+
switch (node.type) {
|
|
105
|
+
case "IfStatement":
|
|
106
|
+
case "ForStatement":
|
|
107
|
+
case "ForInStatement":
|
|
108
|
+
case "ForOfStatement":
|
|
109
|
+
case "WhileStatement":
|
|
110
|
+
case "DoWhileStatement":
|
|
111
|
+
case "CatchClause":
|
|
112
|
+
case "ConditionalExpression":
|
|
113
|
+
add(1);
|
|
114
|
+
break;
|
|
115
|
+
case "SwitchCase":
|
|
116
|
+
if (node.test != null)
|
|
117
|
+
add(1);
|
|
118
|
+
break;
|
|
119
|
+
case "LogicalExpression":
|
|
120
|
+
if (node.operator === "&&" || node.operator === "||" || node.operator === "??")
|
|
121
|
+
add(1);
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
for (let child of children(node))
|
|
127
|
+
walkDecisions(child, add);
|
|
128
|
+
}
|
|
129
|
+
/** Direct child nodes, skipping location metadata. */
|
|
130
|
+
function children(node) {
|
|
131
|
+
let result = [];
|
|
132
|
+
for (let [key, value] of Object.entries(node)) {
|
|
133
|
+
if (key === "type" || key === "start" || key === "end" || key === "range" || key === "loc") {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (Array.isArray(value)) {
|
|
137
|
+
for (let item of value) {
|
|
138
|
+
if (item && typeof item === "object" && typeof item.type === "string") {
|
|
139
|
+
result.push(item);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else if (value && typeof value === "object" && typeof value.type === "string") {
|
|
144
|
+
result.push(value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
function functionName(node, className) {
|
|
150
|
+
if (node.id?.name) {
|
|
151
|
+
return className ? `${className}.${node.id.name}` : node.id.name;
|
|
152
|
+
}
|
|
153
|
+
return "(anonymous)";
|
|
154
|
+
}
|
|
155
|
+
function methodName(node, className) {
|
|
156
|
+
let key = propertyName(node) ?? "(anonymous)";
|
|
157
|
+
let prefix = node.static ? "static " : "";
|
|
158
|
+
let kind = node.kind === "get" || node.kind === "set" ? `${node.kind} ` : "";
|
|
159
|
+
let full = `${prefix}${kind}${key}`;
|
|
160
|
+
return className ? `${className}.${full}` : full;
|
|
161
|
+
}
|
|
162
|
+
function propertyName(node) {
|
|
163
|
+
let key = node.key;
|
|
164
|
+
if (!key)
|
|
165
|
+
return null;
|
|
166
|
+
if (key.type === "Identifier")
|
|
167
|
+
return key.name ?? null;
|
|
168
|
+
if (key.type === "PrivateIdentifier")
|
|
169
|
+
return `#${key.name ?? ""}`;
|
|
170
|
+
if (key.type === "Literal") {
|
|
171
|
+
let literal = key;
|
|
172
|
+
if (typeof literal.value === "string")
|
|
173
|
+
return literal.value;
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
/** 1-based line of `index` in `source`. */
|
|
178
|
+
function lineOf(source, index) {
|
|
179
|
+
let line = 1;
|
|
180
|
+
for (let i = 0; i < index && i < source.length; i++) {
|
|
181
|
+
if (source.charCodeAt(i) === 10)
|
|
182
|
+
line += 1;
|
|
183
|
+
}
|
|
184
|
+
return line;
|
|
185
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CRAP (Change Risk Anti-Patterns) scores: cyclomatic complexity squared times uncovered cubed,
|
|
3
|
+
* plus complexity — the metric Uncle Bob uses with agents so an untested complex function
|
|
4
|
+
* outranks a covered one. Coverage comes from Istanbul's `coverage-final.json` when present.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { FunctionComplexity } from "./complexity.js";
|
|
10
|
+
/** One function's CRAP score with the inputs that produced it. */
|
|
11
|
+
export interface CrapScore {
|
|
12
|
+
file: string;
|
|
13
|
+
name: string;
|
|
14
|
+
line: number;
|
|
15
|
+
complexity: number;
|
|
16
|
+
/** Coverage fraction in `[0, 1]`; `null` when no coverage data matched the function. */
|
|
17
|
+
coverage: number | null;
|
|
18
|
+
/** The CRAP value, or `null` when coverage is unknown. */
|
|
19
|
+
crap: number | null;
|
|
20
|
+
}
|
|
21
|
+
/** Istanbul's per-file entry in `coverage-final.json`. */
|
|
22
|
+
interface IstanbulFile {
|
|
23
|
+
path?: string;
|
|
24
|
+
fnMap?: Record<string, {
|
|
25
|
+
name?: string;
|
|
26
|
+
decl?: {
|
|
27
|
+
start?: {
|
|
28
|
+
line?: number;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}>;
|
|
32
|
+
f?: Record<string, number>;
|
|
33
|
+
s?: Record<string, number>;
|
|
34
|
+
statementMap?: Record<string, {
|
|
35
|
+
start?: {
|
|
36
|
+
line?: number;
|
|
37
|
+
};
|
|
38
|
+
end?: {
|
|
39
|
+
line?: number;
|
|
40
|
+
};
|
|
41
|
+
}>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* CRAP = CC² × (1 − coverage)³ + CC. Coverage is a fraction; a fully covered function of
|
|
45
|
+
* complexity `cc` scores exactly `cc`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function crapScore(complexity: number, coverage: number): number;
|
|
48
|
+
/**
|
|
49
|
+
* Scores each function, attaching coverage from `coverageByFile` when a hit map names it.
|
|
50
|
+
* Without a match the score stays `null` so the doctor check can skip or warn on unknowns.
|
|
51
|
+
*/
|
|
52
|
+
export declare function scoreFunctions(file: string, functions: readonly FunctionComplexity[], coverageByFile: ReadonlyMap<string, IstanbulFile>): CrapScore[];
|
|
53
|
+
/**
|
|
54
|
+
* Loads Istanbul's `coverage-final.json` at `path` (absolute or relative to `root`) into a map
|
|
55
|
+
* keyed by project-relative paths. A missing or unreadable file yields an empty map.
|
|
56
|
+
*/
|
|
57
|
+
export declare function loadCoverage(root: string, path: string): Promise<Map<string, IstanbulFile>>;
|
|
58
|
+
/** Whether `path` exists as a readable coverage file under `root`. */
|
|
59
|
+
export declare function coverageExists(root: string, path: string): Promise<boolean>;
|
|
60
|
+
/** Joins `root` and a relative coverage path for error messages. */
|
|
61
|
+
export declare function coveragePath(root: string, relative: string): string;
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CRAP (Change Risk Anti-Patterns) scores: cyclomatic complexity squared times uncovered cubed,
|
|
3
|
+
* plus complexity — the metric Uncle Bob uses with agents so an untested complex function
|
|
4
|
+
* outranks a covered one. Coverage comes from Istanbul's `coverage-final.json` when present.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { join, resolve } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
/**
|
|
13
|
+
* CRAP = CC² × (1 − coverage)³ + CC. Coverage is a fraction; a fully covered function of
|
|
14
|
+
* complexity `cc` scores exactly `cc`.
|
|
15
|
+
*/
|
|
16
|
+
export function crapScore(complexity, coverage) {
|
|
17
|
+
let uncovered = 1 - clamp(coverage, 0, 1);
|
|
18
|
+
return complexity * complexity * uncovered * uncovered * uncovered + complexity;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Scores each function, attaching coverage from `coverageByFile` when a hit map names it.
|
|
22
|
+
* Without a match the score stays `null` so the doctor check can skip or warn on unknowns.
|
|
23
|
+
*/
|
|
24
|
+
export function scoreFunctions(file, functions, coverageByFile) {
|
|
25
|
+
let entry = coverageByFile.get(normalizePath(file));
|
|
26
|
+
return functions.map((fn) => {
|
|
27
|
+
let coverage = entry ? functionCoverage(entry, fn) : null;
|
|
28
|
+
return {
|
|
29
|
+
file,
|
|
30
|
+
name: fn.name,
|
|
31
|
+
line: fn.line,
|
|
32
|
+
complexity: fn.complexity,
|
|
33
|
+
coverage,
|
|
34
|
+
crap: coverage === null ? null : crapScore(fn.complexity, coverage),
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Loads Istanbul's `coverage-final.json` at `path` (absolute or relative to `root`) into a map
|
|
40
|
+
* keyed by project-relative paths. A missing or unreadable file yields an empty map.
|
|
41
|
+
*/
|
|
42
|
+
export async function loadCoverage(root, path) {
|
|
43
|
+
let absolute = resolve(root, path);
|
|
44
|
+
let text = await wrap(() => readFile(absolute, "utf8"));
|
|
45
|
+
if (isFailure(text))
|
|
46
|
+
return new Map();
|
|
47
|
+
let parsed = await wrap(() => Promise.resolve(JSON.parse(text.data)));
|
|
48
|
+
if (isFailure(parsed) || typeof parsed.data !== "object" || parsed.data === null) {
|
|
49
|
+
return new Map();
|
|
50
|
+
}
|
|
51
|
+
let map = new Map();
|
|
52
|
+
for (let [key, value] of Object.entries(parsed.data)) {
|
|
53
|
+
let relative = toProjectPath(root, value.path ?? key);
|
|
54
|
+
map.set(normalizePath(relative), value);
|
|
55
|
+
}
|
|
56
|
+
return map;
|
|
57
|
+
}
|
|
58
|
+
/** Whether `path` exists as a readable coverage file under `root`. */
|
|
59
|
+
export async function coverageExists(root, path) {
|
|
60
|
+
let absolute = resolve(root, path);
|
|
61
|
+
let text = await wrap(() => readFile(absolute, "utf8"));
|
|
62
|
+
return !isFailure(text);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Coverage fraction for `fn` from Istanbul's function hit map, falling back to statement
|
|
66
|
+
* coverage on the function's starting line when the name is missing from `fnMap`.
|
|
67
|
+
*/
|
|
68
|
+
function functionCoverage(entry, fn) {
|
|
69
|
+
if (entry.fnMap && entry.f) {
|
|
70
|
+
for (let [id, meta] of Object.entries(entry.fnMap)) {
|
|
71
|
+
let name = meta.name ?? "";
|
|
72
|
+
let line = meta.decl?.start?.line;
|
|
73
|
+
let nameMatch = name === fn.name ||
|
|
74
|
+
name.endsWith(`.${fn.name}`) ||
|
|
75
|
+
(fn.name !== "(anonymous)" && name === fn.name.split(".").pop());
|
|
76
|
+
let lineMatch = line === undefined || line === fn.line;
|
|
77
|
+
if (nameMatch && lineMatch) {
|
|
78
|
+
let hits = entry.f[id] ?? 0;
|
|
79
|
+
return hits > 0 ? 1 : 0;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (entry.statementMap && entry.s) {
|
|
84
|
+
let total = 0;
|
|
85
|
+
let covered = 0;
|
|
86
|
+
for (let [id, span] of Object.entries(entry.statementMap)) {
|
|
87
|
+
let start = span.start?.line ?? 0;
|
|
88
|
+
let end = span.end?.line ?? start;
|
|
89
|
+
if (fn.line < start || fn.line > end)
|
|
90
|
+
continue;
|
|
91
|
+
total += 1;
|
|
92
|
+
if ((entry.s[id] ?? 0) > 0)
|
|
93
|
+
covered += 1;
|
|
94
|
+
}
|
|
95
|
+
if (total > 0)
|
|
96
|
+
return covered / total;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
function toProjectPath(root, filePath) {
|
|
101
|
+
let absolute = resolve(filePath);
|
|
102
|
+
let rootAbsolute = resolve(root);
|
|
103
|
+
if (absolute.startsWith(rootAbsolute)) {
|
|
104
|
+
return absolute
|
|
105
|
+
.slice(rootAbsolute.length)
|
|
106
|
+
.replace(/^[\\/]/, "")
|
|
107
|
+
.split("\\")
|
|
108
|
+
.join("/");
|
|
109
|
+
}
|
|
110
|
+
return filePath.split("\\").join("/");
|
|
111
|
+
}
|
|
112
|
+
function normalizePath(path) {
|
|
113
|
+
return path.split("\\").join("/");
|
|
114
|
+
}
|
|
115
|
+
function clamp(value, min, max) {
|
|
116
|
+
return Math.min(max, Math.max(min, value));
|
|
117
|
+
}
|
|
118
|
+
/** Joins `root` and a relative coverage path for error messages. */
|
|
119
|
+
export function coveragePath(root, relative) {
|
|
120
|
+
return join(root, relative);
|
|
121
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's import graph over app source: each relative or `~/` edge between modules, so
|
|
3
|
+
* architecture diagrams and the dependency doctor check share one walk of `findModuleReferences`.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import type { Layer } from "./layers.js";
|
|
9
|
+
/** One directed import from a module to another module inside the project. */
|
|
10
|
+
export interface ImportEdge {
|
|
11
|
+
/** The importing file, relative to the project root. */
|
|
12
|
+
from: string;
|
|
13
|
+
/** The resolved target file when known, else the normalised specifier path. */
|
|
14
|
+
to: string;
|
|
15
|
+
/** The specifier as written in the source. */
|
|
16
|
+
specifier: string;
|
|
17
|
+
}
|
|
18
|
+
/** An edge between two layers, with how many module edges it aggregates. */
|
|
19
|
+
export interface LayerEdge {
|
|
20
|
+
from: Layer;
|
|
21
|
+
to: Layer;
|
|
22
|
+
count: number;
|
|
23
|
+
/** Whether every underlying edge obeys the dependency rule. */
|
|
24
|
+
allowed: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** What {@link projectGraph} reports. */
|
|
27
|
+
export interface ProjectGraph {
|
|
28
|
+
/** Every app-source module that was scanned. */
|
|
29
|
+
modules: string[];
|
|
30
|
+
/** Relative and `~/` imports that resolve inside the project. */
|
|
31
|
+
edges: ImportEdge[];
|
|
32
|
+
/** Module edges rolled up by layer, for the architecture diagram. */
|
|
33
|
+
layers: LayerEdge[];
|
|
34
|
+
/** Strongly connected components with more than one module, or a self-cycle. */
|
|
35
|
+
cycles: string[][];
|
|
36
|
+
/** Module edges that point from an inner layer to an outer one. */
|
|
37
|
+
violations: ImportEdge[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Builds the import graph of the project at `root` from every app-source module under the
|
|
41
|
+
* layout folders. Specifiers that leave the project (packages, absolute URLs) are ignored.
|
|
42
|
+
*/
|
|
43
|
+
export declare function projectGraph(root: string): Promise<ProjectGraph>;
|
|
44
|
+
/** Rolls module edges into layer-to-layer counts. */
|
|
45
|
+
export declare function aggregateLayers(edges: readonly ImportEdge[]): LayerEdge[];
|
|
46
|
+
/**
|
|
47
|
+
* Resolves `specifier` written in `file` to a project-relative path without extension, or
|
|
48
|
+
* `null` when it names a package or a URL.
|
|
49
|
+
*/
|
|
50
|
+
export declare function resolveSpecifier(specifier: string, file: string): string | null;
|
|
51
|
+
/**
|
|
52
|
+
* Every cycle in the directed graph, as the modules on the cycle in visit order. A module that
|
|
53
|
+
* imports itself is a one-node cycle.
|
|
54
|
+
*/
|
|
55
|
+
export declare function findCycles(modules: readonly string[], edges: readonly ImportEdge[]): string[][];
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's import graph over app source: each relative or `~/` edge between modules, so
|
|
3
|
+
* architecture diagrams and the dependency doctor check share one walk of `findModuleReferences`.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import { readFile } from "node:fs/promises";
|
|
9
|
+
import { dirname, join, posix } from "node:path";
|
|
10
|
+
import { findModuleReferences } from "@sdxc/codemod";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { isAppSource, listFiles } from "../doctor/files.js";
|
|
13
|
+
import { PROJECT_FOLDERS } from "../stats.js";
|
|
14
|
+
import { allowsDependency, layerOf } from "./layers.js";
|
|
15
|
+
/**
|
|
16
|
+
* Builds the import graph of the project at `root` from every app-source module under the
|
|
17
|
+
* layout folders. Specifiers that leave the project (packages, absolute URLs) are ignored.
|
|
18
|
+
*/
|
|
19
|
+
export async function projectGraph(root) {
|
|
20
|
+
let modules = [];
|
|
21
|
+
let edges = [];
|
|
22
|
+
let known = new Set();
|
|
23
|
+
for (let folder of PROJECT_FOLDERS) {
|
|
24
|
+
for (let file of (await listFiles(root, folder)).filter(isAppSource)) {
|
|
25
|
+
modules.push(file);
|
|
26
|
+
known.add(file);
|
|
27
|
+
known.add(withoutExtension(file));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
for (let file of modules) {
|
|
31
|
+
let source = await wrap(() => readFile(join(root, file), "utf8"));
|
|
32
|
+
if (isFailure(source))
|
|
33
|
+
continue;
|
|
34
|
+
let references = findModuleReferences(file, source.data);
|
|
35
|
+
if (isFailure(references))
|
|
36
|
+
continue;
|
|
37
|
+
for (let { specifier } of references.data) {
|
|
38
|
+
let target = resolveSpecifier(specifier, file);
|
|
39
|
+
if (target === null)
|
|
40
|
+
continue;
|
|
41
|
+
let resolved = resolveKnown(target, known);
|
|
42
|
+
if (resolved === null)
|
|
43
|
+
continue;
|
|
44
|
+
edges.push({ from: file, to: resolved, specifier });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
let violations = edges.filter((edge) => !allowsDependency(edge.from, edge.to));
|
|
48
|
+
return {
|
|
49
|
+
modules,
|
|
50
|
+
edges,
|
|
51
|
+
layers: aggregateLayers(edges),
|
|
52
|
+
cycles: findCycles(modules, edges),
|
|
53
|
+
violations,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Rolls module edges into layer-to-layer counts. */
|
|
57
|
+
export function aggregateLayers(edges) {
|
|
58
|
+
let counts = new Map();
|
|
59
|
+
for (let edge of edges) {
|
|
60
|
+
let from = layerOf(edge.from);
|
|
61
|
+
let to = layerOf(edge.to);
|
|
62
|
+
if (from === null || to === null || from === to)
|
|
63
|
+
continue;
|
|
64
|
+
let key = `${from}->${to}`;
|
|
65
|
+
let existing = counts.get(key);
|
|
66
|
+
if (existing) {
|
|
67
|
+
existing.count += 1;
|
|
68
|
+
if (!allowsDependency(edge.from, edge.to))
|
|
69
|
+
existing.allowed = false;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
counts.set(key, {
|
|
73
|
+
from,
|
|
74
|
+
to,
|
|
75
|
+
count: 1,
|
|
76
|
+
allowed: allowsDependency(edge.from, edge.to),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return [...counts.values()].sort((a, b) => a.from === b.from ? a.to.localeCompare(b.to) : a.from.localeCompare(b.from));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Resolves `specifier` written in `file` to a project-relative path without extension, or
|
|
83
|
+
* `null` when it names a package or a URL.
|
|
84
|
+
*/
|
|
85
|
+
export function resolveSpecifier(specifier, file) {
|
|
86
|
+
if (specifier.startsWith("~/"))
|
|
87
|
+
return specifier.slice(2);
|
|
88
|
+
if (!specifier.startsWith("."))
|
|
89
|
+
return null;
|
|
90
|
+
return posix.normalize(posix.join(dirname(file).split("\\").join("/"), specifier));
|
|
91
|
+
}
|
|
92
|
+
/** Matches a resolved specifier against known modules, trying common extensions. */
|
|
93
|
+
function resolveKnown(target, known) {
|
|
94
|
+
let candidates = [
|
|
95
|
+
target,
|
|
96
|
+
`${target}.ts`,
|
|
97
|
+
`${target}.tsx`,
|
|
98
|
+
`${target}.js`,
|
|
99
|
+
`${target}.jsx`,
|
|
100
|
+
`${target}.mts`,
|
|
101
|
+
`${target}.cts`,
|
|
102
|
+
joinIndex(target),
|
|
103
|
+
];
|
|
104
|
+
for (let candidate of candidates) {
|
|
105
|
+
if (candidate === null)
|
|
106
|
+
continue;
|
|
107
|
+
if (known.has(candidate))
|
|
108
|
+
return withPreferredExtension(candidate, known);
|
|
109
|
+
let bare = withoutExtension(candidate);
|
|
110
|
+
if (known.has(bare)) {
|
|
111
|
+
for (let module of known) {
|
|
112
|
+
if (withoutExtension(module) === bare && module.includes("."))
|
|
113
|
+
return module;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
/** `dir/index.ts` when `target` names a folder the project holds as an index module. */
|
|
120
|
+
function joinIndex(target) {
|
|
121
|
+
if (target.endsWith("/"))
|
|
122
|
+
return `${target}index`;
|
|
123
|
+
return `${target}/index`;
|
|
124
|
+
}
|
|
125
|
+
/** The first known module path that matches `candidate`'s bare name. */
|
|
126
|
+
function withPreferredExtension(candidate, known) {
|
|
127
|
+
if (known.has(candidate) && candidate.includes("."))
|
|
128
|
+
return candidate;
|
|
129
|
+
let bare = withoutExtension(candidate);
|
|
130
|
+
for (let module of known) {
|
|
131
|
+
if (withoutExtension(module) === bare && module.includes("."))
|
|
132
|
+
return module;
|
|
133
|
+
}
|
|
134
|
+
return candidate;
|
|
135
|
+
}
|
|
136
|
+
/** Drops a trailing `.ts` / `.tsx` / `.js` / … extension. */
|
|
137
|
+
function withoutExtension(path) {
|
|
138
|
+
return path.replace(/\.[cm]?[jt]sx?$/, "");
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Every cycle in the directed graph, as the modules on the cycle in visit order. A module that
|
|
142
|
+
* imports itself is a one-node cycle.
|
|
143
|
+
*/
|
|
144
|
+
export function findCycles(modules, edges) {
|
|
145
|
+
let adjacency = new Map();
|
|
146
|
+
for (let module of modules)
|
|
147
|
+
adjacency.set(module, []);
|
|
148
|
+
for (let edge of edges) {
|
|
149
|
+
let list = adjacency.get(edge.from);
|
|
150
|
+
if (list && !list.includes(edge.to))
|
|
151
|
+
list.push(edge.to);
|
|
152
|
+
}
|
|
153
|
+
let index = 0;
|
|
154
|
+
let stack = [];
|
|
155
|
+
let onStack = new Set();
|
|
156
|
+
let indices = new Map();
|
|
157
|
+
let lowlink = new Map();
|
|
158
|
+
let cycles = [];
|
|
159
|
+
function strongconnect(node) {
|
|
160
|
+
indices.set(node, index);
|
|
161
|
+
lowlink.set(node, index);
|
|
162
|
+
index += 1;
|
|
163
|
+
stack.push(node);
|
|
164
|
+
onStack.add(node);
|
|
165
|
+
for (let next of adjacency.get(node) ?? []) {
|
|
166
|
+
if (!indices.has(next)) {
|
|
167
|
+
strongconnect(next);
|
|
168
|
+
lowlink.set(node, Math.min(lowlink.get(node), lowlink.get(next)));
|
|
169
|
+
}
|
|
170
|
+
else if (onStack.has(next)) {
|
|
171
|
+
lowlink.set(node, Math.min(lowlink.get(node), indices.get(next)));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (lowlink.get(node) === indices.get(node)) {
|
|
175
|
+
let component = [];
|
|
176
|
+
let member;
|
|
177
|
+
do {
|
|
178
|
+
member = stack.pop();
|
|
179
|
+
if (member === undefined)
|
|
180
|
+
break;
|
|
181
|
+
onStack.delete(member);
|
|
182
|
+
component.push(member);
|
|
183
|
+
} while (member !== node);
|
|
184
|
+
let self = (adjacency.get(node) ?? []).includes(node);
|
|
185
|
+
if (component.length > 1 || self)
|
|
186
|
+
cycles.push(component.reverse());
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (let module of modules) {
|
|
190
|
+
if (!indices.has(module))
|
|
191
|
+
strongconnect(module);
|
|
192
|
+
}
|
|
193
|
+
return cycles.sort((a, b) => a[0].localeCompare(b[0]));
|
|
194
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The app-layout layers Clean Architecture's dependency rule maps onto: outer kinds may import
|
|
3
|
+
* inner ones, never the reverse, so a model never reaches a controller and a view never pulls a
|
|
4
|
+
* job. Ranks are the only rule; same-rank edges stay allowed.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* One kind of code the dependency rule names. Unranked paths (config, database, packages) sit
|
|
11
|
+
* outside the rule so anyone may import them.
|
|
12
|
+
*/
|
|
13
|
+
export type Layer = "models" | "policies" | "services" | "initializers" | "http" | "jobs" | "mcp" | "emails" | "commands" | "resources" | "routes" | "bootstrap" | "test";
|
|
14
|
+
/** Higher rank is more inward; an import may only reach a layer at this rank or higher. */
|
|
15
|
+
export declare const LAYER_RANK: Readonly<Record<Layer, number>>;
|
|
16
|
+
/** Display order for architecture diagrams, outer first. */
|
|
17
|
+
export declare const LAYER_ORDER: readonly Layer[];
|
|
18
|
+
/**
|
|
19
|
+
* The layer `path` belongs to, or `null` when the path is outside the ranked kinds (config,
|
|
20
|
+
* database, or a package).
|
|
21
|
+
*
|
|
22
|
+
* @param path - Project-relative path with `/` separators.
|
|
23
|
+
*/
|
|
24
|
+
export declare function layerOf(path: string): Layer | null;
|
|
25
|
+
/**
|
|
26
|
+
* Whether an import from `from` to `to` obeys the dependency rule. Unranked endpoints are left
|
|
27
|
+
* alone so config and packages stay free to import.
|
|
28
|
+
*/
|
|
29
|
+
export declare function allowsDependency(from: string, to: string): boolean;
|