@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,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `jsdoc` check: every comment in the app's source is a JSDoc block whose description stays
|
|
3
|
+
* within three prose lines, so an edit cannot leave a long narrative or a `//` aside beside the
|
|
4
|
+
* code it describes. Tags are left out of the line budget; `@param`, `@returns` and `@throws`
|
|
5
|
+
* stay on one line, and `@description` is refused.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { readFile } from "node:fs/promises";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { tokenize } from "@sdxc/codemod";
|
|
13
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
14
|
+
import { PROJECT_FOLDERS } from "../stats.js";
|
|
15
|
+
import { passed, skipped } from "./check.js";
|
|
16
|
+
import { isAppSource, listFiles } from "./files.js";
|
|
17
|
+
/** Tags whose description must stay on the same line as the tag. */
|
|
18
|
+
const ONE_LINE_TAGS = new Set([
|
|
19
|
+
"param",
|
|
20
|
+
"arg",
|
|
21
|
+
"argument",
|
|
22
|
+
"returns",
|
|
23
|
+
"return",
|
|
24
|
+
"throws",
|
|
25
|
+
"exception",
|
|
26
|
+
]);
|
|
27
|
+
/** Tags that belong in the comment's prose, not as a block tag. */
|
|
28
|
+
const BANNED_TAGS = new Set(["description", "desc"]);
|
|
29
|
+
/**
|
|
30
|
+
* Tooling directives the repository keeps as the only non-JSDoc comments, matched on the
|
|
31
|
+
* comment body after its markers.
|
|
32
|
+
*/
|
|
33
|
+
const TOOLING = /(?:@ts-(?:expect-error|ignore|nocheck)\b|oxlint-disable(?:-next-line)?\b|biome-ignore\b|eslint-disable(?:-next-line)?\b|prettier-ignore\b|@jsxImportSource\b|@vitest-environment\b|v8 ignore\b|c8 ignore\b|istanbul ignore\b|#__PURE__\b|\/?\s*<reference\b)/;
|
|
34
|
+
/** The most prose lines a JSDoc description may hold. */
|
|
35
|
+
export const MAX_DESCRIPTION_LINES = 3;
|
|
36
|
+
/**
|
|
37
|
+
* Warns on each non-JSDoc comment that is not a tooling directive, each description longer than
|
|
38
|
+
* three prose lines, each banned `@description`, and each `@param` / `@returns` / `@throws` that
|
|
39
|
+
* spills onto a second line.
|
|
40
|
+
*/
|
|
41
|
+
export const jsdocCheck = {
|
|
42
|
+
name: "jsdoc",
|
|
43
|
+
async run(context) {
|
|
44
|
+
let findings = [];
|
|
45
|
+
let files = 0;
|
|
46
|
+
for (let folder of PROJECT_FOLDERS) {
|
|
47
|
+
for (let file of (await listFiles(context.root, folder)).filter(isAppSource)) {
|
|
48
|
+
files += 1;
|
|
49
|
+
let source = await wrap(() => readFile(join(context.root, file), "utf8"));
|
|
50
|
+
if (isFailure(source))
|
|
51
|
+
continue;
|
|
52
|
+
for (let finding of inspectComments(file, source.data)) {
|
|
53
|
+
findings.push(`${finding.file}:${finding.line}: ${finding.message}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (files === 0)
|
|
58
|
+
return skipped("No app source to read for comments");
|
|
59
|
+
if (findings.length === 0) {
|
|
60
|
+
return passed("Every comment is JSDoc within three description lines");
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
status: "warn",
|
|
64
|
+
message: findings.length === 1
|
|
65
|
+
? "Found 1 comment outside the JSDoc rules"
|
|
66
|
+
: `Found ${findings.length} comments outside the JSDoc rules`,
|
|
67
|
+
findings,
|
|
68
|
+
fix: "Write comments as /** … */, keep the description to 3 lines, put @param/@returns/@throws on one line, and drop @description",
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Every rule breach in `source`, in source order. A file the scanner cannot tokenize is left
|
|
74
|
+
* alone so the type checker names the syntax error.
|
|
75
|
+
*
|
|
76
|
+
* @param file - Path reported in each finding.
|
|
77
|
+
* @param source - The module's text.
|
|
78
|
+
*/
|
|
79
|
+
export function inspectComments(file, source) {
|
|
80
|
+
let tokens = tokenize(file, source);
|
|
81
|
+
if (isFailure(tokens))
|
|
82
|
+
return [];
|
|
83
|
+
let findings = [];
|
|
84
|
+
for (let token of tokens.data) {
|
|
85
|
+
if (token.type !== "comment")
|
|
86
|
+
continue;
|
|
87
|
+
let line = lineOf(source, token.start);
|
|
88
|
+
let value = token.value;
|
|
89
|
+
if (value.startsWith("#!"))
|
|
90
|
+
continue;
|
|
91
|
+
if (isJsdoc(value)) {
|
|
92
|
+
findings.push(...inspectJsdoc(file, line, value));
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (isToolingDirective(value))
|
|
96
|
+
continue;
|
|
97
|
+
let kind = value.startsWith("//") ? "//" : "/* … */";
|
|
98
|
+
findings.push({
|
|
99
|
+
file,
|
|
100
|
+
line,
|
|
101
|
+
message: `${kind} comment; write JSDoc (/** … */) or a tooling directive`,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return findings;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Whether `value` is a JSDoc block (`/** … *\/`), the only comment shape a symbol may carry.
|
|
108
|
+
*/
|
|
109
|
+
export function isJsdoc(value) {
|
|
110
|
+
return value.startsWith("/**");
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Whether `value` is a shebang, a triple-slash reference, or a tooling directive the repository
|
|
114
|
+
* keeps as the only non-JSDoc comments.
|
|
115
|
+
*/
|
|
116
|
+
export function isToolingDirective(value) {
|
|
117
|
+
if (value.startsWith("#!"))
|
|
118
|
+
return true;
|
|
119
|
+
return TOOLING.test(value);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Findings inside one JSDoc block: a description longer than three prose lines, a banned
|
|
123
|
+
* `@description`, or a one-line tag that continues onto further lines.
|
|
124
|
+
*/
|
|
125
|
+
function inspectJsdoc(file, line, value) {
|
|
126
|
+
let blocks = splitBlocks(commentLines(value));
|
|
127
|
+
let findings = [];
|
|
128
|
+
let prose = (blocks[0] ?? []).filter((entry) => entry.trim() !== "");
|
|
129
|
+
if (prose.length > MAX_DESCRIPTION_LINES) {
|
|
130
|
+
findings.push({
|
|
131
|
+
file,
|
|
132
|
+
line,
|
|
133
|
+
message: `description has ${prose.length} lines; keep it to ${MAX_DESCRIPTION_LINES}`,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
for (let tagLines of blocks.slice(1)) {
|
|
137
|
+
let first = tagLines[0] ?? "";
|
|
138
|
+
let match = /^@(\S+)/.exec(first);
|
|
139
|
+
let name = match?.[1] ?? "";
|
|
140
|
+
if (BANNED_TAGS.has(name)) {
|
|
141
|
+
findings.push({
|
|
142
|
+
file,
|
|
143
|
+
line,
|
|
144
|
+
message: `@${name} is not allowed; put the prose before the tags`,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (ONE_LINE_TAGS.has(name) && tagLines.slice(1).some((entry) => entry.trim() !== "")) {
|
|
148
|
+
findings.push({
|
|
149
|
+
file,
|
|
150
|
+
line,
|
|
151
|
+
message: `@${name} spans ${tagLines.filter((entry) => entry.trim() !== "").length} lines; keep it on one line`,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return findings;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Strip the comment markers and the per-line `*` gutter, keeping relative indentation that a
|
|
159
|
+
* fenced `@example` may need.
|
|
160
|
+
*/
|
|
161
|
+
function commentLines(raw) {
|
|
162
|
+
let text = raw.trim();
|
|
163
|
+
if (text.startsWith("/**"))
|
|
164
|
+
text = text.slice(3);
|
|
165
|
+
else if (text.startsWith("/*"))
|
|
166
|
+
text = text.slice(2);
|
|
167
|
+
if (text.endsWith("*/"))
|
|
168
|
+
text = text.slice(0, -2);
|
|
169
|
+
return text.split("\n").map((entry) => entry.replace(/^[ \t]*\*? ?/, "").trimEnd());
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Cut the lines at every block tag that starts a line outside a fenced code block, so the first
|
|
173
|
+
* group is the description and each later group is one tag.
|
|
174
|
+
*/
|
|
175
|
+
function splitBlocks(lines) {
|
|
176
|
+
let blocks = [[]];
|
|
177
|
+
let fence = null;
|
|
178
|
+
for (let line of lines) {
|
|
179
|
+
let fenced = /^\s*(```+|~~~+)/.exec(line);
|
|
180
|
+
if (fenced?.[1]) {
|
|
181
|
+
if (fence === null)
|
|
182
|
+
fence = fenced[1][0] ?? null;
|
|
183
|
+
else if (fenced[1].startsWith(fence))
|
|
184
|
+
fence = null;
|
|
185
|
+
}
|
|
186
|
+
if (fence === null && /^@\S/.test(line))
|
|
187
|
+
blocks.push([line]);
|
|
188
|
+
else
|
|
189
|
+
blocks.at(-1)?.push(line);
|
|
190
|
+
}
|
|
191
|
+
return blocks;
|
|
192
|
+
}
|
|
193
|
+
/** The 1-based line number of `offset` in `source`. */
|
|
194
|
+
function lineOf(source, offset) {
|
|
195
|
+
let line = 1;
|
|
196
|
+
for (let index = 0; index < offset; index++) {
|
|
197
|
+
if (source.charCodeAt(index) === 10)
|
|
198
|
+
line += 1;
|
|
199
|
+
}
|
|
200
|
+
return line;
|
|
201
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,31 @@
|
|
|
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 { projectStats } from "../stats.js";
|
|
9
|
+
import { passed, skipped } from "./check.js";
|
|
10
|
+
/**
|
|
11
|
+
* Warns when `tests / code` is below the configured minimum. A project with no app code skips.
|
|
12
|
+
*/
|
|
13
|
+
export const testRatioCheck = {
|
|
14
|
+
name: "test-ratio",
|
|
15
|
+
async run(context) {
|
|
16
|
+
let min = context.config.doctor.testRatio.min;
|
|
17
|
+
let stats = await projectStats(context.root);
|
|
18
|
+
if (stats.code === 0)
|
|
19
|
+
return skipped("No app code to compare with tests");
|
|
20
|
+
let ratio = stats.tests / stats.code;
|
|
21
|
+
if (ratio >= min) {
|
|
22
|
+
return passed(`Code to test ratio is 1:${ratio.toFixed(1)} (min 1:${min.toFixed(1)})`);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
status: "warn",
|
|
26
|
+
message: `Code to test ratio is 1:${ratio.toFixed(1)}, below 1:${min.toFixed(1)}`,
|
|
27
|
+
findings: [`code ${stats.code}, tests ${stats.tests}`],
|
|
28
|
+
fix: `Add tests beside the code, or lower doctor.testRatio.min in .config/sdxc.jsonc`,
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command-shaped `sdxc fix` repairs: re-run `typegen`, `ui update`, package install and
|
|
3
|
+
* `upgrade` when those are already the doctor check's `fix`, without new mutation logic.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import type { AnyCommandContext } from "@sdxc/cli";
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Config } from "../config.js";
|
|
11
|
+
import type { CheckReport } from "../doctor/check.js";
|
|
12
|
+
import type { PackageManager } from "../package-manager.js";
|
|
13
|
+
import type { Manifest } from "../upgrade/project.js";
|
|
14
|
+
/** What one command-shaped fix ran or would run. */
|
|
15
|
+
export interface CommandFix {
|
|
16
|
+
check: string;
|
|
17
|
+
command: string[];
|
|
18
|
+
}
|
|
19
|
+
/** Context `fix` needs to exec and print. */
|
|
20
|
+
export type FixContext = Pick<AnyCommandContext, "stdout" | "exec">;
|
|
21
|
+
/**
|
|
22
|
+
* Re-runs the command-shaped doctor checks and executes each repairable `fix`. Under
|
|
23
|
+
* `dryRun` it prints the commands and changes nothing.
|
|
24
|
+
*/
|
|
25
|
+
export declare function fixCommands(ctx: FixContext, options: {
|
|
26
|
+
root: string;
|
|
27
|
+
packageRoot: string;
|
|
28
|
+
manager: PackageManager;
|
|
29
|
+
config: Config;
|
|
30
|
+
manifest: Manifest;
|
|
31
|
+
dryRun: boolean;
|
|
32
|
+
}): Promise<Result<CommandFix[], Error>>;
|
|
33
|
+
/**
|
|
34
|
+
* Turns a doctor `fix` string into an argv the project can run. Only command-shaped fixes
|
|
35
|
+
* are accepted; prose instructions return `null`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseFixCommand(report: CheckReport, manager: PackageManager, bin: string): string[] | null;
|