specdx 0.3.0-alpha.2 → 0.3.0-alpha.3
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/.claude-plugin/plugin.json +1 -1
- package/dist/{changelog-URU2TFNM.js → changelog-OUP2RXWN.js} +4 -4
- package/dist/check-3XQYO3HU.js +109 -0
- package/dist/check-3XQYO3HU.js.map +1 -0
- package/dist/chunk-6BWZEAST.js +1051 -0
- package/dist/chunk-6BWZEAST.js.map +1 -0
- package/dist/{chunk-LU6M5O63.js → chunk-DDFLMRPC.js} +2 -2
- package/dist/{chunk-276J7LR6.js → chunk-PHR7AV6I.js} +2 -2
- package/dist/{chunk-ATLVRGHI.js → chunk-WTFWNYJA.js} +27 -3
- package/dist/chunk-WTFWNYJA.js.map +1 -0
- package/dist/{chunk-XLEN6ZYM.js → chunk-ZX4HVL5M.js} +92 -4
- package/dist/chunk-ZX4HVL5M.js.map +1 -0
- package/dist/{diff-55PTEVB3.js → diff-275PLXYH.js} +4 -4
- package/dist/dist-PQCG2XXF.js +11 -0
- package/dist/dist-PQCG2XXF.js.map +1 -0
- package/dist/{explain-YXUILNEA.js → explain-I7AQMEEH.js} +3 -3
- package/dist/generate-PC7BIB7G.js +13 -0
- package/dist/generate-PC7BIB7G.js.map +1 -0
- package/dist/generate-story-7WHPPFHY.js +192 -0
- package/dist/generate-story-7WHPPFHY.js.map +1 -0
- package/dist/generate-test-plan-U5LMWGKX.js +154 -0
- package/dist/generate-test-plan-U5LMWGKX.js.map +1 -0
- package/dist/{graph-OWQJWVSN.js → graph-RCAPF55A.js} +3 -3
- package/dist/index.js +158 -13
- package/dist/index.js.map +1 -1
- package/dist/{lint-U7VLGS75.js → lint-6Z2TVWAQ.js} +4 -4
- package/dist/main.js +22 -14
- package/dist/main.js.map +1 -1
- package/dist/migrate-L7W5RHSS.js +56 -0
- package/dist/migrate-L7W5RHSS.js.map +1 -0
- package/dist/{pack-OJAC7JF2.js → pack-PI6V6OR2.js} +52 -9
- package/dist/{pack-OJAC7JF2.js.map → pack-PI6V6OR2.js.map} +1 -1
- package/dist/{ready-3OIF4QHM.js → ready-B7IFPL3S.js} +5 -5
- package/dist/skills/specdx-check-drift.md +71 -0
- package/dist/skills/specdx-review-spec.md +77 -0
- package/dist/skills/specdx-verify.md +70 -0
- package/dist/{skills-XJYTWGDK.js → skills-3GBBWZW2.js} +5 -2
- package/dist/skills-3GBBWZW2.js.map +1 -0
- package/dist/{status-LYZUO6AJ.js → status-AV5GSWVI.js} +5 -5
- package/dist/update-HIWDDEZQ.js +132 -0
- package/dist/update-HIWDDEZQ.js.map +1 -0
- package/dist/{validate-DOZZEZFC.js → validate-N2WSG75W.js} +3 -3
- package/package.json +2 -1
- package/dist/chunk-ATLVRGHI.js.map +0 -1
- package/dist/chunk-XLEN6ZYM.js.map +0 -1
- package/dist/skills-XJYTWGDK.js.map +0 -1
- /package/dist/{changelog-URU2TFNM.js.map → changelog-OUP2RXWN.js.map} +0 -0
- /package/dist/{chunk-LU6M5O63.js.map → chunk-DDFLMRPC.js.map} +0 -0
- /package/dist/{chunk-276J7LR6.js.map → chunk-PHR7AV6I.js.map} +0 -0
- /package/dist/{diff-55PTEVB3.js.map → diff-275PLXYH.js.map} +0 -0
- /package/dist/{explain-YXUILNEA.js.map → explain-I7AQMEEH.js.map} +0 -0
- /package/dist/{graph-OWQJWVSN.js.map → graph-RCAPF55A.js.map} +0 -0
- /package/dist/{lint-U7VLGS75.js.map → lint-6Z2TVWAQ.js.map} +0 -0
- /package/dist/{ready-3OIF4QHM.js.map → ready-B7IFPL3S.js.map} +0 -0
- /package/dist/{status-LYZUO6AJ.js.map → status-AV5GSWVI.js.map} +0 -0
- /package/dist/{validate-DOZZEZFC.js.map → validate-N2WSG75W.js.map} +0 -0
|
@@ -0,0 +1,1051 @@
|
|
|
1
|
+
// ../check/dist/spec-parsers.js
|
|
2
|
+
function extractSection(content, heading) {
|
|
3
|
+
const headingPattern = new RegExp(`^##\\s+${escapeRegex(heading)}\\s*$`, "im");
|
|
4
|
+
const match = headingPattern.exec(content);
|
|
5
|
+
if (!match)
|
|
6
|
+
return null;
|
|
7
|
+
const start = match.index + match[0].length;
|
|
8
|
+
const rest = content.slice(start);
|
|
9
|
+
const nextSection = /^##\s+/m.exec(rest);
|
|
10
|
+
return nextSection ? rest.slice(0, nextSection.index) : rest;
|
|
11
|
+
}
|
|
12
|
+
function escapeRegex(str) {
|
|
13
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14
|
+
}
|
|
15
|
+
var HTTP_METHODS = /* @__PURE__ */ new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
|
|
16
|
+
function parseEndpoints(content) {
|
|
17
|
+
const section = extractSection(content, "Endpoints");
|
|
18
|
+
if (!section)
|
|
19
|
+
return [];
|
|
20
|
+
const results = [];
|
|
21
|
+
const headingPattern = /^###\s+(GET|POST|PUT|PATCH|DELETE)\s+(\S+)/im;
|
|
22
|
+
const blocks = section.split(/^(?=###\s)/m);
|
|
23
|
+
for (const block of blocks) {
|
|
24
|
+
const headingMatch = headingPattern.exec(block);
|
|
25
|
+
if (!headingMatch)
|
|
26
|
+
continue;
|
|
27
|
+
const rawMethod = headingMatch[1].toUpperCase();
|
|
28
|
+
if (!HTTP_METHODS.has(rawMethod))
|
|
29
|
+
continue;
|
|
30
|
+
const method = rawMethod;
|
|
31
|
+
const path = headingMatch[2];
|
|
32
|
+
const params = (path.match(/:([A-Za-z_][A-Za-z0-9_]*)/g) ?? []).map((p) => p.slice(1));
|
|
33
|
+
const afterHeading = block.slice(headingMatch.index + headingMatch[0].length);
|
|
34
|
+
const descriptionLine = afterHeading.split("\n").map((l) => l.trim()).find((l) => l.length > 0 && !l.startsWith("#") && !l.startsWith("-"));
|
|
35
|
+
results.push({
|
|
36
|
+
method,
|
|
37
|
+
path,
|
|
38
|
+
params,
|
|
39
|
+
description: descriptionLine
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return results;
|
|
43
|
+
}
|
|
44
|
+
function parseTypeDefinitions(content) {
|
|
45
|
+
const section = extractSection(content, "Data Model");
|
|
46
|
+
if (!section)
|
|
47
|
+
return [];
|
|
48
|
+
const results = [];
|
|
49
|
+
const blocks = section.split(/^(?=###\s)/m);
|
|
50
|
+
for (const block of blocks) {
|
|
51
|
+
const headingMatch = /^###\s+(\S+)/m.exec(block);
|
|
52
|
+
if (!headingMatch)
|
|
53
|
+
continue;
|
|
54
|
+
const name = headingMatch[1];
|
|
55
|
+
const fields = [];
|
|
56
|
+
const fieldPattern = /^-\s+`([^`]+)`\s*:\s*(.+)/gm;
|
|
57
|
+
let fieldMatch;
|
|
58
|
+
while ((fieldMatch = fieldPattern.exec(block)) !== null) {
|
|
59
|
+
const rawName = fieldMatch[1];
|
|
60
|
+
const optional = rawName.endsWith("?");
|
|
61
|
+
const fieldName = optional ? rawName.slice(0, -1) : rawName;
|
|
62
|
+
const rawType = fieldMatch[2].trim();
|
|
63
|
+
const type = rawType.replace(/\s*\(.*?\)\s*$/, "").trim();
|
|
64
|
+
fields.push({ name: fieldName, type, optional });
|
|
65
|
+
}
|
|
66
|
+
results.push({ name, fields });
|
|
67
|
+
}
|
|
68
|
+
return results;
|
|
69
|
+
}
|
|
70
|
+
function parseTestCases(content) {
|
|
71
|
+
const section = extractSection(content, "Test Cases");
|
|
72
|
+
if (!section)
|
|
73
|
+
return [];
|
|
74
|
+
const results = [];
|
|
75
|
+
let currentSection;
|
|
76
|
+
for (const rawLine of section.split("\n")) {
|
|
77
|
+
const line = rawLine.trim();
|
|
78
|
+
const subHeading = /^###\s+(.+)/.exec(line);
|
|
79
|
+
if (subHeading) {
|
|
80
|
+
currentSection = subHeading[1].trim();
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const bulletMatch = /^-\s+(.+)/.exec(line);
|
|
84
|
+
if (!bulletMatch)
|
|
85
|
+
continue;
|
|
86
|
+
const bulletText = bulletMatch[1].trim();
|
|
87
|
+
const colonIdx = bulletText.indexOf(":");
|
|
88
|
+
if (colonIdx !== -1) {
|
|
89
|
+
const label = bulletText.slice(0, colonIdx).trim();
|
|
90
|
+
const rest = bulletText.slice(colonIdx + 1).trim();
|
|
91
|
+
const items = rest.split(/,\s*/);
|
|
92
|
+
for (const item of items) {
|
|
93
|
+
const trimmed = item.trim();
|
|
94
|
+
if (trimmed) {
|
|
95
|
+
results.push({ description: `${label}: ${trimmed}`, section: currentSection });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
results.push({ description: bulletText, section: currentSection });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return results;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ../check/dist/extractors/express.js
|
|
106
|
+
import { existsSync } from "fs";
|
|
107
|
+
import { join } from "path";
|
|
108
|
+
var HTTP_METHODS2 = /* @__PURE__ */ new Set(["get", "post", "put", "patch", "delete"]);
|
|
109
|
+
async function extractExpressRoutes(projectDir, routesDir) {
|
|
110
|
+
const scanDir = routesDir ? join(projectDir, routesDir) : projectDir;
|
|
111
|
+
if (!existsSync(scanDir)) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
let tsMorph;
|
|
115
|
+
try {
|
|
116
|
+
tsMorph = await import("ts-morph");
|
|
117
|
+
} catch {
|
|
118
|
+
throw new Error("ts-morph is required for route extraction. Install it: pnpm add -D ts-morph");
|
|
119
|
+
}
|
|
120
|
+
const { Project, SyntaxKind } = tsMorph;
|
|
121
|
+
const project = new Project({
|
|
122
|
+
skipAddingFilesFromTsConfig: true,
|
|
123
|
+
compilerOptions: { allowJs: true }
|
|
124
|
+
});
|
|
125
|
+
project.addSourceFilesAtPaths([
|
|
126
|
+
join(scanDir, "**/*.ts"),
|
|
127
|
+
join(scanDir, "**/*.js"),
|
|
128
|
+
`!${join(scanDir, "**/*.test.ts")}`,
|
|
129
|
+
`!${join(scanDir, "**/*.test.js")}`,
|
|
130
|
+
`!${join(scanDir, "**/*.spec.ts")}`,
|
|
131
|
+
`!${join(scanDir, "**/*.spec.js")}`,
|
|
132
|
+
`!${join(scanDir, "**/node_modules/**")}`
|
|
133
|
+
]);
|
|
134
|
+
const routes = [];
|
|
135
|
+
for (const sourceFile of project.getSourceFiles()) {
|
|
136
|
+
const filePath = sourceFile.getFilePath();
|
|
137
|
+
const mountPrefixes = /* @__PURE__ */ new Map();
|
|
138
|
+
const callExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression);
|
|
139
|
+
for (const callExpr of callExpressions) {
|
|
140
|
+
const expr = callExpr.getExpression();
|
|
141
|
+
if (expr.getKind() !== SyntaxKind.PropertyAccessExpression)
|
|
142
|
+
continue;
|
|
143
|
+
const propAccess = expr.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
144
|
+
const methodName = propAccess.getName();
|
|
145
|
+
if (methodName !== "use")
|
|
146
|
+
continue;
|
|
147
|
+
const args = callExpr.getArguments();
|
|
148
|
+
if (args.length < 2)
|
|
149
|
+
continue;
|
|
150
|
+
const firstArg = args[0];
|
|
151
|
+
if (!firstArg)
|
|
152
|
+
continue;
|
|
153
|
+
const secondArg = args[1];
|
|
154
|
+
if (!secondArg)
|
|
155
|
+
continue;
|
|
156
|
+
if (firstArg.getKind() !== SyntaxKind.StringLiteral)
|
|
157
|
+
continue;
|
|
158
|
+
const prefix = firstArg.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralValue();
|
|
159
|
+
if (secondArg.getKind() === SyntaxKind.Identifier) {
|
|
160
|
+
const routerVarName = secondArg.asKindOrThrow(SyntaxKind.Identifier).getText();
|
|
161
|
+
mountPrefixes.set(routerVarName, prefix);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const callExpr of callExpressions) {
|
|
165
|
+
const expr = callExpr.getExpression();
|
|
166
|
+
if (expr.getKind() !== SyntaxKind.PropertyAccessExpression)
|
|
167
|
+
continue;
|
|
168
|
+
const propAccess = expr.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
169
|
+
const methodName = propAccess.getName().toLowerCase();
|
|
170
|
+
if (!HTTP_METHODS2.has(methodName))
|
|
171
|
+
continue;
|
|
172
|
+
const args = callExpr.getArguments();
|
|
173
|
+
if (args.length < 1)
|
|
174
|
+
continue;
|
|
175
|
+
const firstArg = args[0];
|
|
176
|
+
if (!firstArg)
|
|
177
|
+
continue;
|
|
178
|
+
if (firstArg.getKind() !== SyntaxKind.StringLiteral)
|
|
179
|
+
continue;
|
|
180
|
+
const routePath = firstArg.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralValue();
|
|
181
|
+
const receiverExpr = propAccess.getExpression();
|
|
182
|
+
let prefix = "";
|
|
183
|
+
if (receiverExpr.getKind() === SyntaxKind.Identifier) {
|
|
184
|
+
const receiverName = receiverExpr.asKindOrThrow(SyntaxKind.Identifier).getText();
|
|
185
|
+
prefix = mountPrefixes.get(receiverName) ?? "";
|
|
186
|
+
}
|
|
187
|
+
const fullPath = prefix + routePath;
|
|
188
|
+
const params = (fullPath.match(/:([^/]+)/g) ?? []).map((p) => p.slice(1));
|
|
189
|
+
const lineNumber = sourceFile.getLineAndColumnAtPos(callExpr.getStart()).line;
|
|
190
|
+
routes.push({
|
|
191
|
+
method: methodName.toUpperCase(),
|
|
192
|
+
path: fullPath,
|
|
193
|
+
params,
|
|
194
|
+
file: filePath,
|
|
195
|
+
line: lineNumber
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return routes;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ../check/dist/extractors/hono.js
|
|
203
|
+
import { existsSync as existsSync2 } from "fs";
|
|
204
|
+
import { join as join2 } from "path";
|
|
205
|
+
var HTTP_METHODS3 = /* @__PURE__ */ new Set(["get", "post", "put", "patch", "delete"]);
|
|
206
|
+
async function extractHonoRoutes(projectDir, routesDir) {
|
|
207
|
+
const scanDir = routesDir ? join2(projectDir, routesDir) : projectDir;
|
|
208
|
+
if (!existsSync2(scanDir)) {
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
let tsMorph;
|
|
212
|
+
try {
|
|
213
|
+
tsMorph = await import("ts-morph");
|
|
214
|
+
} catch {
|
|
215
|
+
throw new Error("ts-morph is required for route extraction. Install it: pnpm add -D ts-morph");
|
|
216
|
+
}
|
|
217
|
+
const { Project, SyntaxKind } = tsMorph;
|
|
218
|
+
const project = new Project({
|
|
219
|
+
skipAddingFilesFromTsConfig: true,
|
|
220
|
+
compilerOptions: { allowJs: true }
|
|
221
|
+
});
|
|
222
|
+
project.addSourceFilesAtPaths([
|
|
223
|
+
join2(scanDir, "**/*.ts"),
|
|
224
|
+
join2(scanDir, "**/*.js"),
|
|
225
|
+
`!${join2(scanDir, "**/*.test.ts")}`,
|
|
226
|
+
`!${join2(scanDir, "**/*.test.js")}`,
|
|
227
|
+
`!${join2(scanDir, "**/*.spec.ts")}`,
|
|
228
|
+
`!${join2(scanDir, "**/*.spec.js")}`,
|
|
229
|
+
`!${join2(scanDir, "**/node_modules/**")}`
|
|
230
|
+
]);
|
|
231
|
+
const routes = [];
|
|
232
|
+
for (const sourceFile of project.getSourceFiles()) {
|
|
233
|
+
const filePath = sourceFile.getFilePath();
|
|
234
|
+
const mountPrefixes = /* @__PURE__ */ new Map();
|
|
235
|
+
const callExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression);
|
|
236
|
+
for (const callExpr of callExpressions) {
|
|
237
|
+
const expr = callExpr.getExpression();
|
|
238
|
+
if (expr.getKind() !== SyntaxKind.PropertyAccessExpression)
|
|
239
|
+
continue;
|
|
240
|
+
const propAccess = expr.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
241
|
+
const methodName = propAccess.getName();
|
|
242
|
+
if (methodName !== "route")
|
|
243
|
+
continue;
|
|
244
|
+
const args = callExpr.getArguments();
|
|
245
|
+
if (args.length < 2)
|
|
246
|
+
continue;
|
|
247
|
+
const firstArg = args[0];
|
|
248
|
+
if (!firstArg)
|
|
249
|
+
continue;
|
|
250
|
+
const secondArg = args[1];
|
|
251
|
+
if (!secondArg)
|
|
252
|
+
continue;
|
|
253
|
+
if (firstArg.getKind() !== SyntaxKind.StringLiteral)
|
|
254
|
+
continue;
|
|
255
|
+
const prefix = firstArg.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralValue();
|
|
256
|
+
if (secondArg.getKind() === SyntaxKind.Identifier) {
|
|
257
|
+
const subAppVarName = secondArg.asKindOrThrow(SyntaxKind.Identifier).getText();
|
|
258
|
+
mountPrefixes.set(subAppVarName, prefix);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
for (const callExpr of callExpressions) {
|
|
262
|
+
const expr = callExpr.getExpression();
|
|
263
|
+
if (expr.getKind() !== SyntaxKind.PropertyAccessExpression)
|
|
264
|
+
continue;
|
|
265
|
+
const propAccess = expr.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
266
|
+
const methodName = propAccess.getName().toLowerCase();
|
|
267
|
+
if (!HTTP_METHODS3.has(methodName))
|
|
268
|
+
continue;
|
|
269
|
+
const args = callExpr.getArguments();
|
|
270
|
+
if (args.length < 1)
|
|
271
|
+
continue;
|
|
272
|
+
const firstArg = args[0];
|
|
273
|
+
if (!firstArg)
|
|
274
|
+
continue;
|
|
275
|
+
if (firstArg.getKind() !== SyntaxKind.StringLiteral)
|
|
276
|
+
continue;
|
|
277
|
+
const routePath = firstArg.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralValue();
|
|
278
|
+
const receiverExpr = propAccess.getExpression();
|
|
279
|
+
let prefix = "";
|
|
280
|
+
if (receiverExpr.getKind() === SyntaxKind.Identifier) {
|
|
281
|
+
const receiverName = receiverExpr.asKindOrThrow(SyntaxKind.Identifier).getText();
|
|
282
|
+
prefix = mountPrefixes.get(receiverName) ?? "";
|
|
283
|
+
}
|
|
284
|
+
const normalizedPath = routePath === "/" ? prefix : prefix + routePath;
|
|
285
|
+
const params = (normalizedPath.match(/:([^/]+)/g) ?? []).map((p) => p.slice(1));
|
|
286
|
+
const lineNumber = sourceFile.getLineAndColumnAtPos(callExpr.getStart()).line;
|
|
287
|
+
routes.push({
|
|
288
|
+
method: methodName.toUpperCase(),
|
|
289
|
+
path: normalizedPath,
|
|
290
|
+
params,
|
|
291
|
+
file: filePath,
|
|
292
|
+
line: lineNumber
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return routes;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ../check/dist/extractors/nextjs.js
|
|
300
|
+
import { existsSync as existsSync3 } from "fs";
|
|
301
|
+
import { readdir, stat } from "fs/promises";
|
|
302
|
+
import { join as join3 } from "path";
|
|
303
|
+
var HTTP_METHODS4 = /* @__PURE__ */ new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
|
|
304
|
+
function segmentToPath(segment) {
|
|
305
|
+
if (segment.startsWith("(") && segment.endsWith(")")) {
|
|
306
|
+
return "";
|
|
307
|
+
}
|
|
308
|
+
const catchAll = segment.match(/^\[\.\.\.([^\]]+)\]$/);
|
|
309
|
+
if (catchAll) {
|
|
310
|
+
return `:${catchAll[1]}*`;
|
|
311
|
+
}
|
|
312
|
+
const dynamic = segment.match(/^\[([^\]]+)\]$/);
|
|
313
|
+
if (dynamic) {
|
|
314
|
+
return `:${dynamic[1]}`;
|
|
315
|
+
}
|
|
316
|
+
return segment;
|
|
317
|
+
}
|
|
318
|
+
async function walkAppDir(dir, pathPrefix, results) {
|
|
319
|
+
let entries;
|
|
320
|
+
try {
|
|
321
|
+
entries = await readdir(dir);
|
|
322
|
+
} catch {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
for (const entry of entries) {
|
|
326
|
+
const fullPath = join3(dir, entry);
|
|
327
|
+
let entryStat;
|
|
328
|
+
try {
|
|
329
|
+
entryStat = await stat(fullPath);
|
|
330
|
+
} catch {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
if (entryStat.isDirectory()) {
|
|
334
|
+
const pathSegment = segmentToPath(entry);
|
|
335
|
+
const childPrefix = pathSegment === "" ? pathPrefix : `${pathPrefix}/${pathSegment}`;
|
|
336
|
+
await walkAppDir(fullPath, childPrefix, results);
|
|
337
|
+
} else if (entry === "route.ts" || entry === "route.js") {
|
|
338
|
+
results.push({ file: fullPath, routePath: pathPrefix || "/" });
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async function extractNextjsRoutes(projectDir, appDir = "app") {
|
|
343
|
+
const scanDir = join3(projectDir, appDir);
|
|
344
|
+
if (!existsSync3(scanDir)) {
|
|
345
|
+
return [];
|
|
346
|
+
}
|
|
347
|
+
let tsMorph;
|
|
348
|
+
try {
|
|
349
|
+
tsMorph = await import("ts-morph");
|
|
350
|
+
} catch {
|
|
351
|
+
throw new Error("ts-morph is required for route extraction. Install it: pnpm add -D ts-morph");
|
|
352
|
+
}
|
|
353
|
+
const { Project, SyntaxKind } = tsMorph;
|
|
354
|
+
const routeFiles = [];
|
|
355
|
+
await walkAppDir(scanDir, "", routeFiles);
|
|
356
|
+
if (routeFiles.length === 0) {
|
|
357
|
+
return [];
|
|
358
|
+
}
|
|
359
|
+
const project = new Project({
|
|
360
|
+
skipAddingFilesFromTsConfig: true,
|
|
361
|
+
compilerOptions: { allowJs: true }
|
|
362
|
+
});
|
|
363
|
+
for (const { file } of routeFiles) {
|
|
364
|
+
project.addSourceFileAtPath(file);
|
|
365
|
+
}
|
|
366
|
+
const routes = [];
|
|
367
|
+
for (const { file, routePath } of routeFiles) {
|
|
368
|
+
const sourceFile = project.getSourceFile(file);
|
|
369
|
+
if (!sourceFile)
|
|
370
|
+
continue;
|
|
371
|
+
const params = (routePath.match(/:[a-zA-Z_][a-zA-Z0-9_]*\*?/g) ?? []).map((p) => p.replace(/^:/, "").replace(/\*$/, ""));
|
|
372
|
+
const exportedFunctions = sourceFile.getFunctions().filter((fn) => {
|
|
373
|
+
const name = fn.getName();
|
|
374
|
+
return name !== void 0 && HTTP_METHODS4.has(name) && fn.isExported();
|
|
375
|
+
});
|
|
376
|
+
for (const fn of exportedFunctions) {
|
|
377
|
+
const name = fn.getName();
|
|
378
|
+
const lineNumber = sourceFile.getLineAndColumnAtPos(fn.getStart()).line;
|
|
379
|
+
routes.push({
|
|
380
|
+
method: name,
|
|
381
|
+
path: routePath || "/",
|
|
382
|
+
params,
|
|
383
|
+
file,
|
|
384
|
+
line: lineNumber
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
const exportedVarDecls = sourceFile.getDescendantsOfKind(SyntaxKind.VariableDeclaration).filter((decl) => {
|
|
388
|
+
const name = decl.getName();
|
|
389
|
+
if (!HTTP_METHODS4.has(name))
|
|
390
|
+
return false;
|
|
391
|
+
const varStatement = decl.getVariableStatement();
|
|
392
|
+
return varStatement !== void 0 && varStatement.isExported();
|
|
393
|
+
});
|
|
394
|
+
for (const decl of exportedVarDecls) {
|
|
395
|
+
const name = decl.getName();
|
|
396
|
+
const lineNumber = sourceFile.getLineAndColumnAtPos(decl.getStart()).line;
|
|
397
|
+
routes.push({
|
|
398
|
+
method: name,
|
|
399
|
+
path: routePath || "/",
|
|
400
|
+
params,
|
|
401
|
+
file,
|
|
402
|
+
line: lineNumber
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return routes;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ../check/dist/extractors/typescript.js
|
|
410
|
+
import { existsSync as existsSync4 } from "fs";
|
|
411
|
+
import { join as join4 } from "path";
|
|
412
|
+
async function extractTypeScriptTypes(projectDir, typesDir) {
|
|
413
|
+
const scanDir = typesDir ? join4(projectDir, typesDir) : projectDir;
|
|
414
|
+
if (!existsSync4(scanDir)) {
|
|
415
|
+
return [];
|
|
416
|
+
}
|
|
417
|
+
let tsMorph;
|
|
418
|
+
try {
|
|
419
|
+
tsMorph = await import("ts-morph");
|
|
420
|
+
} catch {
|
|
421
|
+
throw new Error("ts-morph is required for type extraction. Install it: pnpm add -D ts-morph");
|
|
422
|
+
}
|
|
423
|
+
const { Project, SyntaxKind } = tsMorph;
|
|
424
|
+
const project = new Project({
|
|
425
|
+
skipAddingFilesFromTsConfig: true,
|
|
426
|
+
compilerOptions: { allowJs: true }
|
|
427
|
+
});
|
|
428
|
+
project.addSourceFilesAtPaths([
|
|
429
|
+
join4(scanDir, "**/*.ts"),
|
|
430
|
+
`!${join4(scanDir, "**/*.test.ts")}`,
|
|
431
|
+
`!${join4(scanDir, "**/*.spec.ts")}`,
|
|
432
|
+
`!${join4(scanDir, "**/node_modules/**")}`
|
|
433
|
+
]);
|
|
434
|
+
const types = [];
|
|
435
|
+
for (const sourceFile of project.getSourceFiles()) {
|
|
436
|
+
const filePath = sourceFile.getFilePath();
|
|
437
|
+
for (const iface of sourceFile.getInterfaces()) {
|
|
438
|
+
const name = iface.getName();
|
|
439
|
+
const line = sourceFile.getLineAndColumnAtPos(iface.getStart()).line;
|
|
440
|
+
const fields = extractProperties(iface.getProperties());
|
|
441
|
+
types.push({ name, fields, file: filePath, line });
|
|
442
|
+
}
|
|
443
|
+
for (const typeAlias of sourceFile.getTypeAliases()) {
|
|
444
|
+
const typeNode = typeAlias.getTypeNode();
|
|
445
|
+
if (!typeNode)
|
|
446
|
+
continue;
|
|
447
|
+
if (typeNode.getKind() !== SyntaxKind.TypeLiteral)
|
|
448
|
+
continue;
|
|
449
|
+
const typeLiteral = typeNode.asKindOrThrow(SyntaxKind.TypeLiteral);
|
|
450
|
+
const properties = typeLiteral.getProperties();
|
|
451
|
+
if (properties.length === 0)
|
|
452
|
+
continue;
|
|
453
|
+
const name = typeAlias.getName();
|
|
454
|
+
const line = sourceFile.getLineAndColumnAtPos(typeAlias.getStart()).line;
|
|
455
|
+
const fields = extractProperties(properties);
|
|
456
|
+
types.push({ name, fields, file: filePath, line });
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return types;
|
|
460
|
+
}
|
|
461
|
+
function extractProperties(properties) {
|
|
462
|
+
return properties.map((prop) => {
|
|
463
|
+
const name = prop.getName();
|
|
464
|
+
const optional = prop.hasQuestionToken();
|
|
465
|
+
let type = prop.getTypeNode()?.getText() ?? prop.getType().getText();
|
|
466
|
+
type = type.replace(/import\([^)]+\)\./g, "");
|
|
467
|
+
return { name, type, optional };
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// ../check/dist/extractors/zod.js
|
|
472
|
+
import { existsSync as existsSync5 } from "fs";
|
|
473
|
+
import { join as join5 } from "path";
|
|
474
|
+
async function extractZodSchemas(projectDir, typesDir) {
|
|
475
|
+
const scanDir = typesDir ? join5(projectDir, typesDir) : projectDir;
|
|
476
|
+
if (!existsSync5(scanDir)) {
|
|
477
|
+
return [];
|
|
478
|
+
}
|
|
479
|
+
let tsMorph;
|
|
480
|
+
try {
|
|
481
|
+
tsMorph = await import("ts-morph");
|
|
482
|
+
} catch {
|
|
483
|
+
throw new Error("ts-morph is required for Zod schema extraction. Install it: pnpm add -D ts-morph");
|
|
484
|
+
}
|
|
485
|
+
const { Project, SyntaxKind } = tsMorph;
|
|
486
|
+
const project = new Project({
|
|
487
|
+
skipAddingFilesFromTsConfig: true,
|
|
488
|
+
compilerOptions: { allowJs: true }
|
|
489
|
+
});
|
|
490
|
+
project.addSourceFilesAtPaths([
|
|
491
|
+
join5(scanDir, "**/*.ts"),
|
|
492
|
+
join5(scanDir, "**/*.js"),
|
|
493
|
+
`!${join5(scanDir, "**/*.test.ts")}`,
|
|
494
|
+
`!${join5(scanDir, "**/*.test.js")}`,
|
|
495
|
+
`!${join5(scanDir, "**/*.spec.ts")}`,
|
|
496
|
+
`!${join5(scanDir, "**/*.spec.js")}`,
|
|
497
|
+
`!${join5(scanDir, "**/node_modules/**")}`
|
|
498
|
+
]);
|
|
499
|
+
const types = [];
|
|
500
|
+
for (const sourceFile of project.getSourceFiles()) {
|
|
501
|
+
const filePath = sourceFile.getFilePath();
|
|
502
|
+
const varDeclarations = sourceFile.getDescendantsOfKind(SyntaxKind.VariableDeclaration);
|
|
503
|
+
for (const varDecl of varDeclarations) {
|
|
504
|
+
const initializer = varDecl.getInitializer();
|
|
505
|
+
if (!initializer)
|
|
506
|
+
continue;
|
|
507
|
+
if (initializer.getKind() !== SyntaxKind.CallExpression)
|
|
508
|
+
continue;
|
|
509
|
+
const zObjectCall = findZObjectCall(initializer, SyntaxKind);
|
|
510
|
+
if (!zObjectCall)
|
|
511
|
+
continue;
|
|
512
|
+
const args = zObjectCall.asKindOrThrow(SyntaxKind.CallExpression).getArguments();
|
|
513
|
+
if (args.length === 0)
|
|
514
|
+
continue;
|
|
515
|
+
const firstArg = args[0];
|
|
516
|
+
if (!firstArg)
|
|
517
|
+
continue;
|
|
518
|
+
if (firstArg.getKind() !== SyntaxKind.ObjectLiteralExpression)
|
|
519
|
+
continue;
|
|
520
|
+
const objLiteral = firstArg.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
|
|
521
|
+
const rawName = varDecl.getName();
|
|
522
|
+
const typeName = rawName.endsWith("Schema") ? rawName.slice(0, -"Schema".length) : rawName;
|
|
523
|
+
const fields = [];
|
|
524
|
+
for (const prop of objLiteral.getProperties()) {
|
|
525
|
+
if (prop.getKind() !== SyntaxKind.PropertyAssignment)
|
|
526
|
+
continue;
|
|
527
|
+
const propAssign = prop.asKindOrThrow(SyntaxKind.PropertyAssignment);
|
|
528
|
+
const fieldName = propAssign.getName();
|
|
529
|
+
const fieldInit = propAssign.getInitializer();
|
|
530
|
+
if (!fieldInit)
|
|
531
|
+
continue;
|
|
532
|
+
const { type: fieldType, optional } = inferZodType(fieldInit, SyntaxKind);
|
|
533
|
+
fields.push({ name: fieldName, type: fieldType, optional });
|
|
534
|
+
}
|
|
535
|
+
const lineNumber = sourceFile.getLineAndColumnAtPos(varDecl.getStart()).line;
|
|
536
|
+
types.push({ name: typeName, fields, file: filePath, line: lineNumber });
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return types;
|
|
540
|
+
}
|
|
541
|
+
function findZObjectCall(node, SyntaxKind) {
|
|
542
|
+
if (node.getKind() !== SyntaxKind.CallExpression)
|
|
543
|
+
return void 0;
|
|
544
|
+
const callExpr = node.asKindOrThrow(SyntaxKind.CallExpression);
|
|
545
|
+
const expr = callExpr.getExpression();
|
|
546
|
+
if (isZObjectAccess(expr, SyntaxKind)) {
|
|
547
|
+
return node;
|
|
548
|
+
}
|
|
549
|
+
if (expr.getKind() === SyntaxKind.PropertyAccessExpression) {
|
|
550
|
+
const propAccess = expr.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
551
|
+
const innerExpr = propAccess.getExpression();
|
|
552
|
+
if (innerExpr.getKind() === SyntaxKind.CallExpression) {
|
|
553
|
+
return findZObjectCall(innerExpr, SyntaxKind);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return void 0;
|
|
557
|
+
}
|
|
558
|
+
function isZObjectAccess(node, SyntaxKind) {
|
|
559
|
+
if (node.getKind() !== SyntaxKind.PropertyAccessExpression)
|
|
560
|
+
return false;
|
|
561
|
+
const propAccess = node.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
562
|
+
if (propAccess.getName() !== "object")
|
|
563
|
+
return false;
|
|
564
|
+
const receiver = propAccess.getExpression();
|
|
565
|
+
return receiver.getKind() === SyntaxKind.Identifier && receiver.getText() === "z";
|
|
566
|
+
}
|
|
567
|
+
function inferZodType(node, SyntaxKind) {
|
|
568
|
+
const chainMethods = [];
|
|
569
|
+
let baseCallNode;
|
|
570
|
+
let current = node;
|
|
571
|
+
while (current.getKind() === SyntaxKind.CallExpression) {
|
|
572
|
+
const callExpr = current.asKindOrThrow(SyntaxKind.CallExpression);
|
|
573
|
+
const expr = callExpr.getExpression();
|
|
574
|
+
if (expr.getKind() === SyntaxKind.PropertyAccessExpression) {
|
|
575
|
+
const propAccess = expr.asKindOrThrow(SyntaxKind.PropertyAccessExpression);
|
|
576
|
+
const methodName = propAccess.getName();
|
|
577
|
+
const receiver = propAccess.getExpression();
|
|
578
|
+
if (receiver.getKind() === SyntaxKind.Identifier && receiver.getText() === "z") {
|
|
579
|
+
baseCallNode = current;
|
|
580
|
+
chainMethods.push(methodName);
|
|
581
|
+
break;
|
|
582
|
+
} else {
|
|
583
|
+
chainMethods.push(methodName);
|
|
584
|
+
current = receiver;
|
|
585
|
+
}
|
|
586
|
+
} else {
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const optional = chainMethods.includes("optional");
|
|
591
|
+
const baseMethodName = chainMethods[chainMethods.length - 1];
|
|
592
|
+
let type = "unknown";
|
|
593
|
+
if (baseMethodName === "string") {
|
|
594
|
+
type = "string";
|
|
595
|
+
} else if (baseMethodName === "number") {
|
|
596
|
+
type = "number";
|
|
597
|
+
} else if (baseMethodName === "boolean") {
|
|
598
|
+
type = "boolean";
|
|
599
|
+
} else if (baseMethodName === "date") {
|
|
600
|
+
type = "Date";
|
|
601
|
+
} else if (baseMethodName === "array") {
|
|
602
|
+
type = "array";
|
|
603
|
+
} else if (baseMethodName === "enum" && baseCallNode) {
|
|
604
|
+
const enumCall = baseCallNode.asKindOrThrow(SyntaxKind.CallExpression);
|
|
605
|
+
const args = enumCall.getArguments();
|
|
606
|
+
if (args.length > 0) {
|
|
607
|
+
const firstArg = args[0];
|
|
608
|
+
if (firstArg && firstArg.getKind() === SyntaxKind.ArrayLiteralExpression) {
|
|
609
|
+
const arrayLiteral = firstArg.asKindOrThrow(SyntaxKind.ArrayLiteralExpression);
|
|
610
|
+
const elements = arrayLiteral.getElements().filter((el) => el.getKind() === SyntaxKind.StringLiteral).map((el) => el.asKindOrThrow(SyntaxKind.StringLiteral).getText());
|
|
611
|
+
if (elements.length > 0) {
|
|
612
|
+
type = elements.join(" | ");
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return { type, optional };
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// ../check/dist/extractors/prisma.js
|
|
621
|
+
import { readFile } from "fs/promises";
|
|
622
|
+
import { join as join6 } from "path";
|
|
623
|
+
var PRISMA_TYPE_MAP = {
|
|
624
|
+
String: "string",
|
|
625
|
+
Int: "number",
|
|
626
|
+
Float: "number",
|
|
627
|
+
Decimal: "number",
|
|
628
|
+
BigInt: "number",
|
|
629
|
+
Boolean: "boolean",
|
|
630
|
+
DateTime: "Date",
|
|
631
|
+
Json: "unknown",
|
|
632
|
+
Bytes: "Buffer"
|
|
633
|
+
};
|
|
634
|
+
async function extractPrismaModels(projectDir) {
|
|
635
|
+
const schemaPath = join6(projectDir, "schema.prisma");
|
|
636
|
+
let content;
|
|
637
|
+
try {
|
|
638
|
+
content = await readFile(schemaPath, "utf-8");
|
|
639
|
+
} catch {
|
|
640
|
+
return [];
|
|
641
|
+
}
|
|
642
|
+
const models = [];
|
|
643
|
+
const modelRe = /^model\s+(\w+)\s*\{([^}]+)\}/gm;
|
|
644
|
+
let modelMatch;
|
|
645
|
+
while ((modelMatch = modelRe.exec(content)) !== null) {
|
|
646
|
+
const name = modelMatch[1];
|
|
647
|
+
const body = modelMatch[2];
|
|
648
|
+
const fields = [];
|
|
649
|
+
const lineOffset = content.slice(0, modelMatch.index).split("\n").length;
|
|
650
|
+
for (const line of body.split("\n")) {
|
|
651
|
+
const fieldMatch = /^\s+(\w+)\s+(\w+)(\??)/.exec(line);
|
|
652
|
+
if (!fieldMatch)
|
|
653
|
+
continue;
|
|
654
|
+
const fieldName = fieldMatch[1];
|
|
655
|
+
const prismaType = fieldMatch[2];
|
|
656
|
+
const optional = fieldMatch[3] === "?";
|
|
657
|
+
if (line.includes("@relation") || prismaType.endsWith("[]") || line.trim().endsWith("[]"))
|
|
658
|
+
continue;
|
|
659
|
+
if (!PRISMA_TYPE_MAP[prismaType] && /^[A-Z]/.test(prismaType))
|
|
660
|
+
continue;
|
|
661
|
+
const tsType = PRISMA_TYPE_MAP[prismaType] ?? prismaType.toLowerCase();
|
|
662
|
+
fields.push({ name: fieldName, type: tsType, optional });
|
|
663
|
+
}
|
|
664
|
+
models.push({ name, fields, file: schemaPath, line: lineOffset });
|
|
665
|
+
}
|
|
666
|
+
return models;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// ../check/dist/extractors/test-extractor.js
|
|
670
|
+
import { existsSync as existsSync6 } from "fs";
|
|
671
|
+
import { join as join7 } from "path";
|
|
672
|
+
async function extractTestDescriptions(projectDir, testsDir) {
|
|
673
|
+
const scanDir = testsDir ? join7(projectDir, testsDir) : projectDir;
|
|
674
|
+
if (!existsSync6(scanDir)) {
|
|
675
|
+
return [];
|
|
676
|
+
}
|
|
677
|
+
let tsMorph;
|
|
678
|
+
try {
|
|
679
|
+
tsMorph = await import("ts-morph");
|
|
680
|
+
} catch {
|
|
681
|
+
throw new Error("ts-morph is required for test extraction. Install it: pnpm add -D ts-morph");
|
|
682
|
+
}
|
|
683
|
+
const { Project, SyntaxKind } = tsMorph;
|
|
684
|
+
const project = new Project({
|
|
685
|
+
skipAddingFilesFromTsConfig: true,
|
|
686
|
+
compilerOptions: { allowJs: true }
|
|
687
|
+
});
|
|
688
|
+
project.addSourceFilesAtPaths([
|
|
689
|
+
join7(scanDir, "**/*.ts"),
|
|
690
|
+
join7(scanDir, "**/*.js"),
|
|
691
|
+
`!${join7(scanDir, "**/node_modules/**")}`
|
|
692
|
+
]);
|
|
693
|
+
const tests = [];
|
|
694
|
+
for (const sourceFile of project.getSourceFiles()) {
|
|
695
|
+
const filePath = sourceFile.getFilePath();
|
|
696
|
+
const callExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression);
|
|
697
|
+
for (const callExpr of callExpressions) {
|
|
698
|
+
const expr = callExpr.getExpression();
|
|
699
|
+
if (expr.getKind() !== SyntaxKind.Identifier)
|
|
700
|
+
continue;
|
|
701
|
+
const calleeName = expr.asKindOrThrow(SyntaxKind.Identifier).getText();
|
|
702
|
+
if (calleeName !== "it" && calleeName !== "test")
|
|
703
|
+
continue;
|
|
704
|
+
const args = callExpr.getArguments();
|
|
705
|
+
if (args.length < 1)
|
|
706
|
+
continue;
|
|
707
|
+
const firstArg = args[0];
|
|
708
|
+
if (!firstArg)
|
|
709
|
+
continue;
|
|
710
|
+
if (firstArg.getKind() !== SyntaxKind.StringLiteral)
|
|
711
|
+
continue;
|
|
712
|
+
const description = firstArg.asKindOrThrow(SyntaxKind.StringLiteral).getLiteralValue();
|
|
713
|
+
const lineNumber = sourceFile.getLineAndColumnAtPos(callExpr.getStart()).line;
|
|
714
|
+
tests.push({
|
|
715
|
+
description,
|
|
716
|
+
file: filePath,
|
|
717
|
+
line: lineNumber
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return tests;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// ../check/dist/matchers/routes.js
|
|
725
|
+
function normalisePath(path) {
|
|
726
|
+
return "/" + path.replace(/^\/+/, "").replace(/\/+$/, "").replace(/\/+/g, "/");
|
|
727
|
+
}
|
|
728
|
+
function matchRoutes(specEndpoints, codeRoutes, specId) {
|
|
729
|
+
const findings = [];
|
|
730
|
+
const codeSet = new Set(codeRoutes.map((r) => `${r.method} ${normalisePath(r.path)}`));
|
|
731
|
+
const specSet = new Set(specEndpoints.map((e) => `${e.method} ${normalisePath(e.path)}`));
|
|
732
|
+
for (const endpoint of specEndpoints) {
|
|
733
|
+
const key = `${endpoint.method} ${normalisePath(endpoint.path)}`;
|
|
734
|
+
if (!codeSet.has(key)) {
|
|
735
|
+
findings.push({
|
|
736
|
+
type: "missing",
|
|
737
|
+
category: "route",
|
|
738
|
+
specId,
|
|
739
|
+
specSection: "Endpoints",
|
|
740
|
+
expected: key,
|
|
741
|
+
severity: "error",
|
|
742
|
+
suggestion: `Implement ${key} in your route handler`
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
for (const route of codeRoutes) {
|
|
747
|
+
const key = `${route.method} ${normalisePath(route.path)}`;
|
|
748
|
+
if (!specSet.has(key)) {
|
|
749
|
+
findings.push({
|
|
750
|
+
type: "extra",
|
|
751
|
+
category: "route",
|
|
752
|
+
specId,
|
|
753
|
+
codeLocation: { file: route.file, line: route.line },
|
|
754
|
+
expected: "(not in spec)",
|
|
755
|
+
actual: key,
|
|
756
|
+
severity: "info",
|
|
757
|
+
suggestion: `Route ${key} exists in code but not in spec \u2014 add to spec if intentional`
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return findings;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// ../check/dist/matchers/types.js
|
|
765
|
+
var STRIP_SUFFIXES = ["Schema", "Model", "Type", "Interface", "Entity"];
|
|
766
|
+
function normaliseTypeName(name) {
|
|
767
|
+
let normalised = name;
|
|
768
|
+
for (const suffix of STRIP_SUFFIXES) {
|
|
769
|
+
if (normalised.endsWith(suffix) && normalised.length > suffix.length) {
|
|
770
|
+
normalised = normalised.slice(0, -suffix.length);
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return normalised.toLowerCase();
|
|
775
|
+
}
|
|
776
|
+
function findMatchingCodeType(specType, codeTypes) {
|
|
777
|
+
const specNorm = normaliseTypeName(specType.name);
|
|
778
|
+
return codeTypes.find((ct) => normaliseTypeName(ct.name) === specNorm);
|
|
779
|
+
}
|
|
780
|
+
function matchTypes(specTypes, codeTypes, specId) {
|
|
781
|
+
const findings = [];
|
|
782
|
+
for (const specType of specTypes) {
|
|
783
|
+
const codeType = findMatchingCodeType(specType, codeTypes);
|
|
784
|
+
if (!codeType) {
|
|
785
|
+
findings.push({
|
|
786
|
+
type: "missing",
|
|
787
|
+
category: "type",
|
|
788
|
+
specId,
|
|
789
|
+
specSection: "Data Models",
|
|
790
|
+
expected: `Type: ${specType.name}`,
|
|
791
|
+
severity: "error",
|
|
792
|
+
suggestion: `Implement type "${specType.name}" in code`
|
|
793
|
+
});
|
|
794
|
+
continue;
|
|
795
|
+
}
|
|
796
|
+
const codeFieldNames = new Set(codeType.fields.map((f) => f.name));
|
|
797
|
+
const specFieldNames = new Set(specType.fields.map((f) => f.name));
|
|
798
|
+
for (const specField of specType.fields) {
|
|
799
|
+
if (!codeFieldNames.has(specField.name)) {
|
|
800
|
+
findings.push({
|
|
801
|
+
type: "missing",
|
|
802
|
+
category: "type",
|
|
803
|
+
specId,
|
|
804
|
+
specSection: "Data Models",
|
|
805
|
+
codeLocation: { file: codeType.file, line: codeType.line },
|
|
806
|
+
expected: `Field ${specType.name}.${specField.name} (${specField.type})`,
|
|
807
|
+
severity: "warn",
|
|
808
|
+
suggestion: `Add field "${specField.name}: ${specField.type}" to ${codeType.name}`
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
for (const codeField of codeType.fields) {
|
|
813
|
+
if (!specFieldNames.has(codeField.name)) {
|
|
814
|
+
findings.push({
|
|
815
|
+
type: "extra",
|
|
816
|
+
category: "type",
|
|
817
|
+
specId,
|
|
818
|
+
specSection: "Data Models",
|
|
819
|
+
codeLocation: { file: codeType.file, line: codeType.line },
|
|
820
|
+
expected: "(not in spec)",
|
|
821
|
+
actual: `Field ${codeType.name}.${codeField.name} (${codeField.type})`,
|
|
822
|
+
severity: "info",
|
|
823
|
+
suggestion: `Field "${codeField.name}" exists in code but not in spec \u2014 add to spec if intentional`
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return findings;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// ../check/dist/matchers/tests.js
|
|
832
|
+
function jaccard(a, b) {
|
|
833
|
+
const wordsA = new Set(a.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter(Boolean));
|
|
834
|
+
const wordsB = new Set(b.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter(Boolean));
|
|
835
|
+
const intersection = new Set([...wordsA].filter((w) => wordsB.has(w)));
|
|
836
|
+
const union = /* @__PURE__ */ new Set([...wordsA, ...wordsB]);
|
|
837
|
+
if (union.size === 0)
|
|
838
|
+
return 0;
|
|
839
|
+
return intersection.size / union.size;
|
|
840
|
+
}
|
|
841
|
+
var JACCARD_THRESHOLD = 0.4;
|
|
842
|
+
function matchTests(specCases, codeTests, specId) {
|
|
843
|
+
const findings = [];
|
|
844
|
+
for (const specCase of specCases) {
|
|
845
|
+
let bestScore = 0;
|
|
846
|
+
let bestMatch;
|
|
847
|
+
for (const codeTest of codeTests) {
|
|
848
|
+
const score = jaccard(specCase.description, codeTest.description);
|
|
849
|
+
if (score > bestScore) {
|
|
850
|
+
bestScore = score;
|
|
851
|
+
bestMatch = codeTest;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
if (bestScore < JACCARD_THRESHOLD || !bestMatch) {
|
|
855
|
+
findings.push({
|
|
856
|
+
type: "missing",
|
|
857
|
+
category: "test",
|
|
858
|
+
specId,
|
|
859
|
+
specSection: specCase.section,
|
|
860
|
+
expected: specCase.description,
|
|
861
|
+
severity: "warn",
|
|
862
|
+
suggestion: `Add a test matching: "${specCase.description}"`
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
return findings;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// ../check/dist/score.js
|
|
870
|
+
function computeScore(findings, totals) {
|
|
871
|
+
const missingByCategory = { route: 0, type: 0, test: 0 };
|
|
872
|
+
for (const f of findings) {
|
|
873
|
+
if (f.type === "missing" || f.type === "mismatch") {
|
|
874
|
+
missingByCategory[f.category] = (missingByCategory[f.category] ?? 0) + 1;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
const categoryMap = {
|
|
878
|
+
routes: { total: totals.routes, categoryKey: "route" },
|
|
879
|
+
types: { total: totals.types, categoryKey: "type" },
|
|
880
|
+
tests: { total: totals.tests, categoryKey: "test" }
|
|
881
|
+
};
|
|
882
|
+
const byCategory = {};
|
|
883
|
+
let totalItems = 0;
|
|
884
|
+
let totalMatched = 0;
|
|
885
|
+
for (const [name, { total, categoryKey }] of Object.entries(categoryMap)) {
|
|
886
|
+
const missing = missingByCategory[categoryKey] ?? 0;
|
|
887
|
+
const matched = Math.max(0, total - missing);
|
|
888
|
+
byCategory[name] = { matched, total };
|
|
889
|
+
totalItems += total;
|
|
890
|
+
totalMatched += matched;
|
|
891
|
+
}
|
|
892
|
+
const overall = totalItems === 0 ? 100 : Math.round(totalMatched / totalItems * 100);
|
|
893
|
+
return { overall, byCategory };
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// ../check/dist/detect-framework.js
|
|
897
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
898
|
+
import { join as join8 } from "path";
|
|
899
|
+
var FRAMEWORK_PACKAGES = [
|
|
900
|
+
["express", "express"],
|
|
901
|
+
["hono", "hono"],
|
|
902
|
+
["next", "nextjs"]
|
|
903
|
+
];
|
|
904
|
+
async function detectFramework(projectDir) {
|
|
905
|
+
let pkg;
|
|
906
|
+
try {
|
|
907
|
+
const content = await readFile2(join8(projectDir, "package.json"), "utf-8");
|
|
908
|
+
pkg = JSON.parse(content);
|
|
909
|
+
} catch {
|
|
910
|
+
return null;
|
|
911
|
+
}
|
|
912
|
+
const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
913
|
+
for (const [pkgName, framework] of FRAMEWORK_PACKAGES) {
|
|
914
|
+
if (pkgName in allDeps) {
|
|
915
|
+
return framework;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
return null;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// ../check/dist/check.js
|
|
922
|
+
async function runCheck(specs, projectDir, config = {}) {
|
|
923
|
+
const findings = [];
|
|
924
|
+
let routeTotal = 0;
|
|
925
|
+
let typeTotal = 0;
|
|
926
|
+
let testTotal = 0;
|
|
927
|
+
const apiContractSpecs = specs.filter((s) => s.frontmatter.type === "api-contract");
|
|
928
|
+
if (apiContractSpecs.length > 0) {
|
|
929
|
+
const codeRoutes = await extractRoutes(projectDir, config);
|
|
930
|
+
for (const spec of apiContractSpecs) {
|
|
931
|
+
const specEndpoints = parseEndpoints(spec.content);
|
|
932
|
+
routeTotal += specEndpoints.length;
|
|
933
|
+
findings.push(...matchRoutes(specEndpoints, codeRoutes, spec.frontmatter.id));
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
const designSpecs = specs.filter((s) => s.frontmatter.type === "technical-design");
|
|
937
|
+
if (designSpecs.length > 0) {
|
|
938
|
+
const codeTypes = [
|
|
939
|
+
...await extractTypeScriptTypes(projectDir, config.types_dir),
|
|
940
|
+
...await extractZodSchemas(projectDir, config.types_dir),
|
|
941
|
+
...await extractPrismaModels(projectDir)
|
|
942
|
+
];
|
|
943
|
+
for (const spec of designSpecs) {
|
|
944
|
+
const specTypes = parseTypeDefinitions(spec.content);
|
|
945
|
+
typeTotal += specTypes.reduce((sum, t) => sum + t.fields.length, 0);
|
|
946
|
+
findings.push(...matchTypes(specTypes, codeTypes, spec.frontmatter.id));
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
const testPlanSpecs = specs.filter((s) => s.frontmatter.type === "test-plan");
|
|
950
|
+
if (testPlanSpecs.length > 0) {
|
|
951
|
+
const codeTests = await extractTestDescriptions(projectDir, config.tests_dir);
|
|
952
|
+
for (const spec of testPlanSpecs) {
|
|
953
|
+
const specCases = parseTestCases(spec.content);
|
|
954
|
+
testTotal += specCases.length;
|
|
955
|
+
findings.push(...matchTests(specCases, codeTests, spec.frontmatter.id));
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
const score = computeScore(findings, { routes: routeTotal, types: typeTotal, tests: testTotal });
|
|
959
|
+
const errors = findings.filter((f) => f.severity === "error").length;
|
|
960
|
+
const warnings = findings.filter((f) => f.severity === "warn").length;
|
|
961
|
+
const summary = `${score.overall}% implementation coverage \u2014 ${errors} errors, ${warnings} warnings`;
|
|
962
|
+
return { findings, score, summary };
|
|
963
|
+
}
|
|
964
|
+
async function extractRoutes(projectDir, config) {
|
|
965
|
+
const framework = config.framework ?? "auto";
|
|
966
|
+
if (framework === "express") {
|
|
967
|
+
return extractExpressRoutes(projectDir, config.routes_dir);
|
|
968
|
+
}
|
|
969
|
+
if (framework === "hono") {
|
|
970
|
+
return extractHonoRoutes(projectDir, config.routes_dir);
|
|
971
|
+
}
|
|
972
|
+
if (framework === "nextjs") {
|
|
973
|
+
return extractNextjsRoutes(projectDir, config.app_dir);
|
|
974
|
+
}
|
|
975
|
+
const detected = await detectFramework(projectDir);
|
|
976
|
+
if (detected === "express") {
|
|
977
|
+
return extractExpressRoutes(projectDir, config.routes_dir);
|
|
978
|
+
}
|
|
979
|
+
if (detected === "hono") {
|
|
980
|
+
return extractHonoRoutes(projectDir, config.routes_dir);
|
|
981
|
+
}
|
|
982
|
+
if (detected === "nextjs") {
|
|
983
|
+
return extractNextjsRoutes(projectDir, config.app_dir);
|
|
984
|
+
}
|
|
985
|
+
const [express, hono, nextjs] = await Promise.all([
|
|
986
|
+
extractExpressRoutes(projectDir, config.routes_dir),
|
|
987
|
+
extractHonoRoutes(projectDir, config.routes_dir),
|
|
988
|
+
extractNextjsRoutes(projectDir, config.app_dir)
|
|
989
|
+
]);
|
|
990
|
+
return [...express, ...hono, ...nextjs];
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// ../check/dist/ai.js
|
|
994
|
+
async function analyzeWithAi(findings, context) {
|
|
995
|
+
const apiKey = process.env["ANTHROPIC_API_KEY"];
|
|
996
|
+
if (!apiKey) {
|
|
997
|
+
throw new Error("ANTHROPIC_API_KEY environment variable is required for --ai mode. Set it or use the sdx:verify skill instead (no API key needed).");
|
|
998
|
+
}
|
|
999
|
+
if (findings.length === 0) {
|
|
1000
|
+
return { findings, assessments: [], summary: "No findings to analyze." };
|
|
1001
|
+
}
|
|
1002
|
+
let Anthropic;
|
|
1003
|
+
try {
|
|
1004
|
+
const mod = await import("@anthropic-ai/sdk");
|
|
1005
|
+
Anthropic = mod.default;
|
|
1006
|
+
} catch {
|
|
1007
|
+
throw new Error("@anthropic-ai/sdk is required for --ai mode. Install it: pnpm add -D @anthropic-ai/sdk");
|
|
1008
|
+
}
|
|
1009
|
+
const client = new Anthropic({ apiKey });
|
|
1010
|
+
const findingsSummary = findings.map((f, i) => `[${i}] ${f.type} (${f.severity}): ${f.expected}${f.actual ? ` \u2014 actual: ${f.actual}` : ""}${f.suggestion ? ` \u2014 suggestion: ${f.suggestion}` : ""}`).join("\n");
|
|
1011
|
+
const prompt = `You are reviewing static analysis findings from a spec-to-implementation check.
|
|
1012
|
+
|
|
1013
|
+
Context: ${context}
|
|
1014
|
+
|
|
1015
|
+
Findings:
|
|
1016
|
+
${findingsSummary}
|
|
1017
|
+
|
|
1018
|
+
For each finding (by index), assess:
|
|
1019
|
+
1. Is this a real issue or a false positive?
|
|
1020
|
+
2. How confident are you? (high/medium/low)
|
|
1021
|
+
3. Brief reasoning (1-2 sentences)
|
|
1022
|
+
4. Suggested fix if it's a real issue
|
|
1023
|
+
|
|
1024
|
+
Respond with a JSON array of objects:
|
|
1025
|
+
[{ "findingIndex": 0, "isRealIssue": true, "confidence": "high", "reasoning": "...", "suggestedFix": "..." }]
|
|
1026
|
+
|
|
1027
|
+
Only output the JSON array, no other text.`;
|
|
1028
|
+
const response = await client.messages.create({
|
|
1029
|
+
model: "claude-sonnet-4-20250514",
|
|
1030
|
+
max_tokens: 4e3,
|
|
1031
|
+
messages: [{ role: "user", content: prompt }]
|
|
1032
|
+
});
|
|
1033
|
+
const text = response.content[0]?.type === "text" ? response.content[0].text : "";
|
|
1034
|
+
let assessments;
|
|
1035
|
+
try {
|
|
1036
|
+
assessments = JSON.parse(text);
|
|
1037
|
+
} catch {
|
|
1038
|
+
assessments = [];
|
|
1039
|
+
}
|
|
1040
|
+
const realIssues = assessments.filter((a) => a.isRealIssue).length;
|
|
1041
|
+
const falsePositives = assessments.filter((a) => !a.isRealIssue).length;
|
|
1042
|
+
const summary = `AI analysis: ${realIssues} real issues, ${falsePositives} false positives out of ${findings.length} findings`;
|
|
1043
|
+
return { findings, assessments, summary };
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
export {
|
|
1047
|
+
detectFramework,
|
|
1048
|
+
runCheck,
|
|
1049
|
+
analyzeWithAi
|
|
1050
|
+
};
|
|
1051
|
+
//# sourceMappingURL=chunk-6BWZEAST.js.map
|