@sdxc/kit 2026.10.21 → 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 +84 -4
- package/changelog.jsonl +86 -0
- package/config.schema.json +154 -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 +71 -2
- package/dist/commands/make.js +46 -0
- package/dist/commands/run.d.ts +15 -11
- package/dist/commands/run.js +21 -16
- package/dist/commands/scaffold.d.ts +1 -1
- package/dist/commands/setup.d.ts +26 -0
- package/dist/commands/setup.js +33 -0
- package/dist/config.d.ts +52 -0
- package/dist/config.js +88 -1
- package/dist/contract.d.ts +52 -3
- package/dist/contract.js +124 -2
- package/dist/dependencies.js +7 -1
- package/dist/deploy/plan.d.ts +5 -0
- package/dist/deploy/plan.js +10 -0
- package/dist/destroy.js +13 -6
- 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 +24 -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/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/imports.js +7 -5
- package/dist/make/schema-diff.js +8 -1
- 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/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/templates/app/AGENTS.md +14 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `duplication` check: identical blocks of at least `doctor.duplication.minLines` code
|
|
3
|
+
* lines (blank and comments stripped) appear in more than one module, so agents extract shared
|
|
4
|
+
* helpers instead of pasting.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { PROJECT_FOLDERS } from "../stats.js";
|
|
13
|
+
import { passed, skipped } from "./check.js";
|
|
14
|
+
import { isAppSource, listFiles } from "./files.js";
|
|
15
|
+
/**
|
|
16
|
+
* Warns when the same normalised code block of `minLines` or more appears in two places.
|
|
17
|
+
*/
|
|
18
|
+
export const duplicationCheck = {
|
|
19
|
+
name: "duplication",
|
|
20
|
+
async run(context) {
|
|
21
|
+
let minLines = context.config.doctor.duplication.minLines;
|
|
22
|
+
let blocks = new Map();
|
|
23
|
+
let files = 0;
|
|
24
|
+
for (let folder of PROJECT_FOLDERS) {
|
|
25
|
+
for (let file of (await listFiles(context.root, folder)).filter(isAppSource)) {
|
|
26
|
+
files += 1;
|
|
27
|
+
let source = await wrap(() => readFile(join(context.root, file), "utf8"));
|
|
28
|
+
if (isFailure(source))
|
|
29
|
+
continue;
|
|
30
|
+
for (let block of codeBlocks(file, source.data, minLines)) {
|
|
31
|
+
let hits = blocks.get(block.key) ?? [];
|
|
32
|
+
hits.push({ file: block.file, line: block.line });
|
|
33
|
+
blocks.set(block.key, hits);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (files === 0)
|
|
38
|
+
return skipped("No app source to scan for duplication");
|
|
39
|
+
let findings = [...blocks.values()]
|
|
40
|
+
.filter((hits) => uniqueFiles(hits).length > 1)
|
|
41
|
+
.flatMap((hits) => {
|
|
42
|
+
let places = uniqueFiles(hits)
|
|
43
|
+
.map((hit) => `${hit.file}:${hit.line}`)
|
|
44
|
+
.sort();
|
|
45
|
+
return [`duplicate block at ${places.join(" and ")}`];
|
|
46
|
+
})
|
|
47
|
+
.sort();
|
|
48
|
+
if (findings.length === 0) {
|
|
49
|
+
return passed(`No duplicated blocks of ${minLines}+ code lines`);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
status: "warn",
|
|
53
|
+
message: findings.length === 1
|
|
54
|
+
? "Found 1 duplicated code block"
|
|
55
|
+
: `Found ${findings.length} duplicated code blocks`,
|
|
56
|
+
findings,
|
|
57
|
+
fix: `Extract a shared helper, or raise doctor.duplication.minLines in .config/sdxc.jsonc`,
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
/** Sliding windows of `minLines` consecutive code lines, keyed by their joined text. */
|
|
62
|
+
export function codeBlocks(file, source, minLines) {
|
|
63
|
+
let code = [];
|
|
64
|
+
let inBlock = false;
|
|
65
|
+
let lines = source.split("\n");
|
|
66
|
+
for (let index = 0; index < lines.length; index++) {
|
|
67
|
+
let raw = lines[index];
|
|
68
|
+
let trimmed = raw.trim();
|
|
69
|
+
if (inBlock) {
|
|
70
|
+
if (trimmed.includes("*/"))
|
|
71
|
+
inBlock = false;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (trimmed === "" || trimmed.startsWith("//"))
|
|
75
|
+
continue;
|
|
76
|
+
if (trimmed.startsWith("/*") || trimmed.startsWith("{/*")) {
|
|
77
|
+
if (!trimmed.includes("*/"))
|
|
78
|
+
inBlock = true;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
code.push({ line: index + 1, text: normalize(trimmed) });
|
|
82
|
+
}
|
|
83
|
+
let blocks = [];
|
|
84
|
+
for (let start = 0; start + minLines <= code.length; start++) {
|
|
85
|
+
let slice = code.slice(start, start + minLines);
|
|
86
|
+
let key = slice.map((line) => line.text).join("\n");
|
|
87
|
+
blocks.push({ key, file, line: slice[0].line });
|
|
88
|
+
}
|
|
89
|
+
return blocks;
|
|
90
|
+
}
|
|
91
|
+
/** Collapses whitespace so indent-only differences do not hide a paste. */
|
|
92
|
+
function normalize(line) {
|
|
93
|
+
return line.replace(/\s+/g, " ");
|
|
94
|
+
}
|
|
95
|
+
/** One hit per file (first line wins). */
|
|
96
|
+
function uniqueFiles(hits) {
|
|
97
|
+
let seen = new Set();
|
|
98
|
+
let unique = [];
|
|
99
|
+
for (let hit of hits) {
|
|
100
|
+
if (seen.has(hit.file))
|
|
101
|
+
continue;
|
|
102
|
+
seen.add(hit.file);
|
|
103
|
+
unique.push(hit);
|
|
104
|
+
}
|
|
105
|
+
return unique;
|
|
106
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `fan-out` check: no module imports more distinct project modules than
|
|
3
|
+
* `doctor.fanOut.max` (default 15), so one file cannot become a hub that couples half the app.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import type { Check } from "./check.js";
|
|
9
|
+
/**
|
|
10
|
+
* Warns on each module whose distinct import targets exceed the configured maximum.
|
|
11
|
+
*/
|
|
12
|
+
export declare const fanOutCheck: Check;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `fan-out` check: no module imports more distinct project modules than
|
|
3
|
+
* `doctor.fanOut.max` (default 15), so one file cannot become a hub that couples half the app.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import { projectGraph } from "../architecture/graph.js";
|
|
9
|
+
import { passed, skipped } from "./check.js";
|
|
10
|
+
/**
|
|
11
|
+
* Warns on each module whose distinct import targets exceed the configured maximum.
|
|
12
|
+
*/
|
|
13
|
+
export const fanOutCheck = {
|
|
14
|
+
name: "fan-out",
|
|
15
|
+
async run(context) {
|
|
16
|
+
let max = context.config.doctor.fanOut.max;
|
|
17
|
+
let graph = await projectGraph(context.root);
|
|
18
|
+
if (graph.modules.length === 0)
|
|
19
|
+
return skipped("No app source to measure fan-out");
|
|
20
|
+
let counts = new Map();
|
|
21
|
+
for (let edge of graph.edges) {
|
|
22
|
+
let set = counts.get(edge.from) ?? new Set();
|
|
23
|
+
set.add(edge.to);
|
|
24
|
+
counts.set(edge.from, set);
|
|
25
|
+
}
|
|
26
|
+
let findings = [...counts.entries()]
|
|
27
|
+
.filter(([, targets]) => targets.size > max)
|
|
28
|
+
.sort((a, b) => b[1].size - a[1].size || a[0].localeCompare(b[0]))
|
|
29
|
+
.map(([file, targets]) => `${file}: imports ${targets.size} modules (max ${max})`);
|
|
30
|
+
if (findings.length === 0) {
|
|
31
|
+
return passed(`Every module imports at most ${max} others`);
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
status: "warn",
|
|
35
|
+
message: findings.length === 1
|
|
36
|
+
? "Found 1 module above the fan-out limit"
|
|
37
|
+
: `Found ${findings.length} modules above the fan-out limit`,
|
|
38
|
+
findings,
|
|
39
|
+
fix: `Split the module or extract a narrower facade, or raise doctor.fanOut.max in .config/sdxc.jsonc`,
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `feature-scatter` check: a feature folder name that appears under one app layer should
|
|
3
|
+
* also appear under the others that already use feature folders, so a glob on that name finds
|
|
4
|
+
* the whole feature.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Check } from "./check.js";
|
|
10
|
+
/**
|
|
11
|
+
* Warns when a feature name already spans two or more layers but is missing from another
|
|
12
|
+
* layer that also hosts multi-layer features. A folder that exists in only one layer is left
|
|
13
|
+
* alone — not every feature crosses every kind.
|
|
14
|
+
*/
|
|
15
|
+
export declare const featureScatterCheck: Check;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `feature-scatter` check: a feature folder name that appears under one app layer should
|
|
3
|
+
* also appear under the others that already use feature folders, so a glob on that name finds
|
|
4
|
+
* the whole feature.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readdir } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { passed, skipped } from "./check.js";
|
|
13
|
+
/** Layers that group by feature with the same folder name. */
|
|
14
|
+
const FEATURE_LAYERS = [
|
|
15
|
+
"app/http/controllers",
|
|
16
|
+
"app/http/middleware",
|
|
17
|
+
"app/http/validators",
|
|
18
|
+
"app/jobs",
|
|
19
|
+
"app/services",
|
|
20
|
+
"app/policies",
|
|
21
|
+
"app/emails",
|
|
22
|
+
"app/commands",
|
|
23
|
+
"app/mcp/tools",
|
|
24
|
+
"app/mcp/resources",
|
|
25
|
+
];
|
|
26
|
+
/**
|
|
27
|
+
* Warns when a feature name already spans two or more layers but is missing from another
|
|
28
|
+
* layer that also hosts multi-layer features. A folder that exists in only one layer is left
|
|
29
|
+
* alone — not every feature crosses every kind.
|
|
30
|
+
*/
|
|
31
|
+
export const featureScatterCheck = {
|
|
32
|
+
name: "feature-scatter",
|
|
33
|
+
async run(context) {
|
|
34
|
+
let byLayer = new Map();
|
|
35
|
+
for (let layer of FEATURE_LAYERS) {
|
|
36
|
+
let names = await featureFolders(context.root, layer);
|
|
37
|
+
if (names.size > 0)
|
|
38
|
+
byLayer.set(layer, names);
|
|
39
|
+
}
|
|
40
|
+
if (byLayer.size < 2) {
|
|
41
|
+
return skipped("Fewer than two layers use feature folders");
|
|
42
|
+
}
|
|
43
|
+
let layersFor = new Map();
|
|
44
|
+
for (let [layer, names] of byLayer) {
|
|
45
|
+
for (let name of names) {
|
|
46
|
+
let list = layersFor.get(name) ?? [];
|
|
47
|
+
list.push(layer);
|
|
48
|
+
layersFor.set(name, list);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
let multi = [...layersFor.entries()].filter(([, layers]) => layers.length >= 2);
|
|
52
|
+
if (multi.length === 0) {
|
|
53
|
+
return skipped("No feature folder name appears in more than one layer");
|
|
54
|
+
}
|
|
55
|
+
let peerLayers = new Set(multi.flatMap(([, layers]) => layers));
|
|
56
|
+
let findings = [];
|
|
57
|
+
for (let [feature, present] of multi.sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
58
|
+
let missing = [...peerLayers].filter((layer) => !present.includes(layer)).sort();
|
|
59
|
+
if (missing.length === 0)
|
|
60
|
+
continue;
|
|
61
|
+
findings.push(`${feature}: in ${present.sort().join(", ")} but missing from ${missing.join(", ")}`);
|
|
62
|
+
}
|
|
63
|
+
if (findings.length === 0) {
|
|
64
|
+
return passed("Multi-layer feature folders line up across their peer layers");
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
status: "warn",
|
|
68
|
+
message: findings.length === 1
|
|
69
|
+
? "Found 1 feature scattered across layers"
|
|
70
|
+
: `Found ${findings.length} features scattered across layers`,
|
|
71
|
+
findings,
|
|
72
|
+
fix: "Add the missing feature folder (or move the files) so the shared name groups the feature in every peer layer",
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
/** Immediate subdirectories of `layer` that look like feature groups (not files). */
|
|
77
|
+
async function featureFolders(root, layer) {
|
|
78
|
+
let entries = await wrap(() => readdir(join(root, layer), { withFileTypes: true }));
|
|
79
|
+
if (isFailure(entries))
|
|
80
|
+
return new Set();
|
|
81
|
+
return new Set(entries.data
|
|
82
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."))
|
|
83
|
+
.map((entry) => entry.name));
|
|
84
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `module-size` check: every app-source module stays within the configured code-line budget
|
|
3
|
+
* (default 200), counting like `sdxc stats` so blank lines and comments do not inflate the size.
|
|
4
|
+
* File-level size is outside oxlint's per-function rules.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Check } from "./check.js";
|
|
10
|
+
/**
|
|
11
|
+
* Warns on each module whose code lines exceed `doctor.moduleSize.maxLines`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const moduleSizeCheck: Check;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `module-size` check: every app-source module stays within the configured code-line budget
|
|
3
|
+
* (default 200), counting like `sdxc stats` so blank lines and comments do not inflate the size.
|
|
4
|
+
* File-level size is outside oxlint's per-function rules.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { codeLines, PROJECT_FOLDERS } from "../stats.js";
|
|
13
|
+
import { passed, skipped } from "./check.js";
|
|
14
|
+
import { isAppSource, listFiles } from "./files.js";
|
|
15
|
+
/**
|
|
16
|
+
* Warns on each module whose code lines exceed `doctor.moduleSize.maxLines`.
|
|
17
|
+
*/
|
|
18
|
+
export const moduleSizeCheck = {
|
|
19
|
+
name: "module-size",
|
|
20
|
+
async run(context) {
|
|
21
|
+
let max = context.config.doctor.moduleSize.maxLines;
|
|
22
|
+
let findings = [];
|
|
23
|
+
let files = 0;
|
|
24
|
+
for (let folder of PROJECT_FOLDERS) {
|
|
25
|
+
for (let file of (await listFiles(context.root, folder)).filter(isAppSource)) {
|
|
26
|
+
files += 1;
|
|
27
|
+
let source = await wrap(() => readFile(join(context.root, file), "utf8"));
|
|
28
|
+
if (isFailure(source))
|
|
29
|
+
continue;
|
|
30
|
+
let lines = codeLines(source.data, false);
|
|
31
|
+
if (lines <= max)
|
|
32
|
+
continue;
|
|
33
|
+
findings.push(`${file}: ${lines} code lines (max ${max})`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (files === 0)
|
|
37
|
+
return skipped("No app source to measure module size");
|
|
38
|
+
if (findings.length === 0) {
|
|
39
|
+
return passed(`Every module stays within ${max} code lines`);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
status: "warn",
|
|
43
|
+
message: findings.length === 1
|
|
44
|
+
? "Found 1 module above the size limit"
|
|
45
|
+
: `Found ${findings.length} modules above the size limit`,
|
|
46
|
+
findings,
|
|
47
|
+
fix: `Extract helpers or split the module, or raise doctor.moduleSize.maxLines in .config/sdxc.jsonc`,
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `mutation` check: surviving mutants in a mutation-testing report stay at or under
|
|
3
|
+
* `doctor.mutation.maxSurvived`. Without the report file the check skips — the same honesty
|
|
4
|
+
* rule as `crap` and coverage.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Check } from "./check.js";
|
|
10
|
+
/**
|
|
11
|
+
* Warns when the configured mutation report lists more survivors than allowed. Understands a
|
|
12
|
+
* Stryker-like `{ files: { …: { mutants: [{ status }] } } }` shape and a flat
|
|
13
|
+
* `{ survivors: [...] }` / `{ survived: number }` summary.
|
|
14
|
+
*/
|
|
15
|
+
export declare const mutationCheck: Check;
|
|
16
|
+
/** Counts and names survivors from common mutation report shapes. */
|
|
17
|
+
export declare function survivingMutants(report: unknown): {
|
|
18
|
+
count: number;
|
|
19
|
+
findings: string[];
|
|
20
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `mutation` check: surviving mutants in a mutation-testing report stay at or under
|
|
3
|
+
* `doctor.mutation.maxSurvived`. Without the report file the check skips — the same honesty
|
|
4
|
+
* rule as `crap` and coverage.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { passed, skipped } from "./check.js";
|
|
13
|
+
/**
|
|
14
|
+
* Warns when the configured mutation report lists more survivors than allowed. Understands a
|
|
15
|
+
* Stryker-like `{ files: { …: { mutants: [{ status }] } } }` shape and a flat
|
|
16
|
+
* `{ survivors: [...] }` / `{ survived: number }` summary.
|
|
17
|
+
*/
|
|
18
|
+
export const mutationCheck = {
|
|
19
|
+
name: "mutation",
|
|
20
|
+
async run(context) {
|
|
21
|
+
let { report: relative, maxSurvived } = context.config.doctor.mutation;
|
|
22
|
+
let absolute = join(context.root, relative);
|
|
23
|
+
let text = await wrap(() => readFile(absolute, "utf8"));
|
|
24
|
+
if (isFailure(text)) {
|
|
25
|
+
return skipped(`No mutation report at ${relative}; run mutation tests then re-run doctor`);
|
|
26
|
+
}
|
|
27
|
+
let parsed = await wrap(() => Promise.resolve(JSON.parse(text.data)));
|
|
28
|
+
if (isFailure(parsed)) {
|
|
29
|
+
return {
|
|
30
|
+
status: "warn",
|
|
31
|
+
message: `Mutation report at ${relative} is not valid JSON`,
|
|
32
|
+
findings: [parsed.error.message],
|
|
33
|
+
fix: `Regenerate ${relative} from the mutation runner`,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
let survivors = survivingMutants(parsed.data);
|
|
37
|
+
if (survivors.count <= maxSurvived) {
|
|
38
|
+
return passed(survivors.count === 0
|
|
39
|
+
? "No surviving mutants in the mutation report"
|
|
40
|
+
: `${survivors.count} surviving mutant(s), within max ${maxSurvived}`);
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
status: "warn",
|
|
44
|
+
message: `${survivors.count} surviving mutant(s), above max ${maxSurvived}`,
|
|
45
|
+
findings: survivors.findings.slice(0, 20),
|
|
46
|
+
fix: `Kill the survivors with stronger tests, or raise doctor.mutation.maxSurvived in .config/sdxc.jsonc`,
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
/** Counts and names survivors from common mutation report shapes. */
|
|
51
|
+
export function survivingMutants(report) {
|
|
52
|
+
if (report === null || typeof report !== "object") {
|
|
53
|
+
return { count: 0, findings: [] };
|
|
54
|
+
}
|
|
55
|
+
let record = report;
|
|
56
|
+
if (typeof record.survived === "number") {
|
|
57
|
+
return { count: record.survived, findings: [`survived: ${record.survived}`] };
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(record.survivors)) {
|
|
60
|
+
let findings = record.survivors.map((item, index) => formatSurvivor(item, index));
|
|
61
|
+
return { count: findings.length, findings };
|
|
62
|
+
}
|
|
63
|
+
if (record.files && typeof record.files === "object") {
|
|
64
|
+
let findings = [];
|
|
65
|
+
for (let [file, entry] of Object.entries(record.files)) {
|
|
66
|
+
if (!entry || typeof entry !== "object")
|
|
67
|
+
continue;
|
|
68
|
+
let mutants = entry.mutants;
|
|
69
|
+
if (!Array.isArray(mutants))
|
|
70
|
+
continue;
|
|
71
|
+
for (let mutant of mutants) {
|
|
72
|
+
if (!mutant || typeof mutant !== "object")
|
|
73
|
+
continue;
|
|
74
|
+
let fields = mutant;
|
|
75
|
+
let status = typeof fields.status === "string" ? fields.status : "";
|
|
76
|
+
if (status.toLowerCase() !== "survived")
|
|
77
|
+
continue;
|
|
78
|
+
let mutator = typeof fields.mutatorName === "string" ? fields.mutatorName : "mutant";
|
|
79
|
+
let id = typeof fields.id === "string" || typeof fields.id === "number" ? String(fields.id) : null;
|
|
80
|
+
findings.push(`${file}: ${mutator}${id !== null ? ` #${id}` : ""}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { count: findings.length, findings };
|
|
84
|
+
}
|
|
85
|
+
return { count: 0, findings: [] };
|
|
86
|
+
}
|
|
87
|
+
function formatSurvivor(item, index) {
|
|
88
|
+
if (typeof item === "string")
|
|
89
|
+
return item;
|
|
90
|
+
if (item && typeof item === "object") {
|
|
91
|
+
let record = item;
|
|
92
|
+
let file = typeof record.file === "string" ? record.file : `survivor[${index}]`;
|
|
93
|
+
let mutator = typeof record.mutatorName === "string" ? record.mutatorName : "mutant";
|
|
94
|
+
return `${file}: ${mutator}`;
|
|
95
|
+
}
|
|
96
|
+
return `survivor[${index}]`;
|
|
97
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `orphans` check: every app-source module is imported by another, or is an entry the layout
|
|
3
|
+
* loads from outside the graph (bootstrap, routes, initializers, migrations, factories). Dead
|
|
4
|
+
* agent leftovers show up as unreferenced files.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Check } from "./check.js";
|
|
10
|
+
/**
|
|
11
|
+
* Warns on each non-entry module that nothing in the project imports.
|
|
12
|
+
*/
|
|
13
|
+
export declare const orphansCheck: Check;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `orphans` check: every app-source module is imported by another, or is an entry the layout
|
|
3
|
+
* loads from outside the graph (bootstrap, routes, initializers, migrations, factories). Dead
|
|
4
|
+
* agent leftovers show up as unreferenced files.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { projectGraph } from "../architecture/graph.js";
|
|
10
|
+
import { passed, skipped } from "./check.js";
|
|
11
|
+
/** Prefixes that may have no importers inside the project graph. */
|
|
12
|
+
const ENTRY_PREFIXES = [
|
|
13
|
+
"bootstrap/",
|
|
14
|
+
"routes/",
|
|
15
|
+
"app/initializers/",
|
|
16
|
+
"database/migrations/",
|
|
17
|
+
"database/factories/",
|
|
18
|
+
"config/",
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* Warns on each non-entry module that nothing in the project imports.
|
|
22
|
+
*/
|
|
23
|
+
export const orphansCheck = {
|
|
24
|
+
name: "orphans",
|
|
25
|
+
async run(context) {
|
|
26
|
+
let graph = await projectGraph(context.root);
|
|
27
|
+
if (graph.modules.length === 0)
|
|
28
|
+
return skipped("No app source to find orphans");
|
|
29
|
+
let imported = new Set(graph.edges.map((edge) => edge.to));
|
|
30
|
+
let findings = graph.modules
|
|
31
|
+
.filter((module) => !imported.has(module) && !isEntry(module))
|
|
32
|
+
.sort()
|
|
33
|
+
.map((module) => `${module}: nothing in the project imports it`);
|
|
34
|
+
if (findings.length === 0) {
|
|
35
|
+
return passed("Every non-entry module is imported somewhere");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
status: "warn",
|
|
39
|
+
message: findings.length === 1
|
|
40
|
+
? "Found 1 orphaned module"
|
|
41
|
+
: `Found ${findings.length} orphaned modules`,
|
|
42
|
+
findings,
|
|
43
|
+
fix: "Delete the file, or wire it from an initializer, router, job map or another module that needs it",
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
/** Whether `path` is an entry the layout loads without a same-project importer. */
|
|
48
|
+
function isEntry(path) {
|
|
49
|
+
return ENTRY_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
50
|
+
}
|
package/dist/doctor/project.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { readdir } from "node:fs/promises";
|
|
10
10
|
import { join } from "node:path";
|
|
11
|
+
import { iife } from "@sdxc/iife";
|
|
11
12
|
import { isFailure, wrap } from "@sdxc/result";
|
|
12
13
|
/** The prefix of the line the runner prints its reports on, apart from anything a check logs. */
|
|
13
14
|
const MARKER = "sdxc-doctor-checks:";
|
|
@@ -174,11 +175,13 @@ export async function runProjectChecks(ctx, options) {
|
|
|
174
175
|
let ran = await ctx.exec(command, { cwd: options.root, stdio: "collect", input });
|
|
175
176
|
let entries = isFailure(ran) ? null : parseEntries(ran.data.stdout);
|
|
176
177
|
if (entries === null) {
|
|
177
|
-
let detail =
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
178
|
+
let detail = iife(() => {
|
|
179
|
+
if (isFailure(ran))
|
|
180
|
+
return "stderr" in ran.error && ran.error.stderr.trim() !== ""
|
|
181
|
+
? ran.error.stderr.trim()
|
|
182
|
+
: ran.error.message;
|
|
183
|
+
return "The runner printed no report";
|
|
184
|
+
});
|
|
182
185
|
return {
|
|
183
186
|
reports: [
|
|
184
187
|
failed(folder, `Could not run the checks in ${folder} with ${options.runtime}`, detail, folder),
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `result-throws` check: app source does not `throw`. Errors travel as `@sdxc/result`
|
|
3
|
+
* failures so handlers stay typed; a bare throw is a convention the project strays from.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import type { Check } from "./check.js";
|
|
9
|
+
/**
|
|
10
|
+
* Warns on each `throw` statement in app source. Tests and declaration files are left out by
|
|
11
|
+
* {@link isAppSource}.
|
|
12
|
+
*/
|
|
13
|
+
export declare const resultThrowsCheck: Check;
|
|
14
|
+
/**
|
|
15
|
+
* 1-based lines where `throw` appears as an identifier token (not inside a string or comment).
|
|
16
|
+
*/
|
|
17
|
+
export declare function throwLines(file: string, source: string): number[];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `result-throws` check: app source does not `throw`. Errors travel as `@sdxc/result`
|
|
3
|
+
* failures so handlers stay typed; a bare throw is a convention the project strays from.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import { readFile } from "node:fs/promises";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { tokenize } from "@sdxc/codemod";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { PROJECT_FOLDERS } from "../stats.js";
|
|
13
|
+
import { passed, skipped } from "./check.js";
|
|
14
|
+
import { isAppSource, listFiles } from "./files.js";
|
|
15
|
+
/**
|
|
16
|
+
* Warns on each `throw` statement in app source. Tests and declaration files are left out by
|
|
17
|
+
* {@link isAppSource}.
|
|
18
|
+
*/
|
|
19
|
+
export const resultThrowsCheck = {
|
|
20
|
+
name: "result-throws",
|
|
21
|
+
async run(context) {
|
|
22
|
+
let findings = [];
|
|
23
|
+
let files = 0;
|
|
24
|
+
for (let folder of PROJECT_FOLDERS) {
|
|
25
|
+
for (let file of (await listFiles(context.root, folder)).filter(isAppSource)) {
|
|
26
|
+
files += 1;
|
|
27
|
+
let source = await wrap(() => readFile(join(context.root, file), "utf8"));
|
|
28
|
+
if (isFailure(source))
|
|
29
|
+
continue;
|
|
30
|
+
for (let line of throwLines(file, source.data)) {
|
|
31
|
+
findings.push(`${file}:${line}: throw`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (files === 0)
|
|
36
|
+
return skipped("No app source to scan for throw");
|
|
37
|
+
if (findings.length === 0) {
|
|
38
|
+
return passed("App source returns Result failures instead of throw");
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
status: "warn",
|
|
42
|
+
message: findings.length === 1
|
|
43
|
+
? "Found 1 throw in app source"
|
|
44
|
+
: `Found ${findings.length} throws in app source`,
|
|
45
|
+
findings,
|
|
46
|
+
fix: "Return failure(...) from @sdxc/result (or a typed error) instead of throw",
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* 1-based lines where `throw` appears as an identifier token (not inside a string or comment).
|
|
52
|
+
*/
|
|
53
|
+
export function throwLines(file, source) {
|
|
54
|
+
let tokens = tokenize(file, source);
|
|
55
|
+
if (isFailure(tokens))
|
|
56
|
+
return [];
|
|
57
|
+
let lines = [];
|
|
58
|
+
for (let token of tokens.data) {
|
|
59
|
+
if (token.type !== "identifier" || token.value !== "throw")
|
|
60
|
+
continue;
|
|
61
|
+
lines.push(lineOf(source, token.start));
|
|
62
|
+
}
|
|
63
|
+
return lines;
|
|
64
|
+
}
|
|
65
|
+
function lineOf(source, index) {
|
|
66
|
+
let line = 1;
|
|
67
|
+
for (let i = 0; i < index && i < source.length; i++) {
|
|
68
|
+
if (source.charCodeAt(i) === 10)
|
|
69
|
+
line += 1;
|
|
70
|
+
}
|
|
71
|
+
return line;
|
|
72
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `test-ratio` check: the code-to-test ratio from `sdxc stats` stays at or above
|
|
3
|
+
* `doctor.testRatio.min` (default 0.3 → `1:0.3`), so agents keep writing tests with the code.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import type { Check } from "./check.js";
|
|
9
|
+
/**
|
|
10
|
+
* Warns when `tests / code` is below the configured minimum. A project with no app code skips.
|
|
11
|
+
*/
|
|
12
|
+
export declare const testRatioCheck: Check;
|