supercov 0.0.44 → 0.0.45

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.
Files changed (37) hide show
  1. package/README.md +21 -12
  2. package/docs/agent-loop.md +12 -8
  3. package/docs/assertion-agent.md +156 -0
  4. package/docs/assertion-evidence.md +9 -694
  5. package/docs/assertion-maps.md +252 -0
  6. package/docs/assertions.md +82 -0
  7. package/docs/cli.md +23 -8
  8. package/docs/coverage-model.md +12 -0
  9. package/package.json +34 -35
  10. package/runtime/javascript/runtime.mjs +18 -33
  11. package/schemas/assertions.schema.json +276 -0
  12. package/analyzers/typescript/README.md +0 -59
  13. package/analyzers/typescript/bin/compiler-identity.mjs +0 -78
  14. package/analyzers/typescript/bin/identity.mjs +0 -71
  15. package/analyzers/typescript/bin/query.mjs +0 -29
  16. package/analyzers/typescript/dist/analyze.js +0 -5273
  17. package/analyzers/typescript/dist/archive.js +0 -337
  18. package/analyzers/typescript/dist/awaited-observations.js +0 -376
  19. package/analyzers/typescript/dist/build-identity.json +0 -1
  20. package/analyzers/typescript/dist/compiler.js +0 -32
  21. package/analyzers/typescript/dist/frontend.js +0 -75
  22. package/analyzers/typescript/dist/mock-counts.js +0 -2517
  23. package/analyzers/typescript/dist/native-frontend.js +0 -271
  24. package/analyzers/typescript/dist/pragmas.js +0 -186
  25. package/analyzers/typescript/dist/types.js +0 -1
  26. package/analyzers/typescript/package.json +0 -27
  27. package/analyzers/typescript/src/analyze.ts +0 -6180
  28. package/analyzers/typescript/src/archive.ts +0 -471
  29. package/analyzers/typescript/src/awaited-observations.ts +0 -561
  30. package/analyzers/typescript/src/compiler.ts +0 -49
  31. package/analyzers/typescript/src/frontend.ts +0 -136
  32. package/analyzers/typescript/src/mock-counts.ts +0 -3219
  33. package/analyzers/typescript/src/native-frontend.ts +0 -315
  34. package/analyzers/typescript/src/pragmas.ts +0 -284
  35. package/analyzers/typescript/src/types.ts +0 -45
  36. package/analyzers/typescript/tsconfig.json +0 -12
  37. package/docs/code-verification.md +0 -4
@@ -1,271 +0,0 @@
1
- import { createRequire, isBuiltin } from "node:module";
2
- import { existsSync } from "node:fs";
3
- import { dirname, resolve } from "node:path";
4
- import { pathToFileURL } from "node:url";
5
- import { analysisPath } from "./compiler.js";
6
- import { selectedRootFiles, } from "./frontend.js";
7
- export function nativeFrontend(entry, projectRoot) {
8
- const [major, minor] = process.versions.node.split(".").map(Number);
9
- if (major < 22 || (major === 22 && minor < 12))
10
- throw new Error("The TypeScript 7 frontend requires Node 22.12 or newer (Node 24 is tested)");
11
- const require = createRequire(pathToFileURL(entry));
12
- const ast = require("typescript/unstable/ast");
13
- const native = require("typescript/unstable/sync");
14
- const root = analysisPath(projectRoot);
15
- const key = (file) => analysisPath(file).replaceAll("\\", "/");
16
- const virtual = new Map();
17
- let parseSerial = 0;
18
- const opened = new Set();
19
- const limitations = new Set();
20
- let api;
21
- let snapshot;
22
- let closed = false;
23
- function client() {
24
- if (closed)
25
- throw new Error("TypeScript 7 frontend is closed");
26
- return (api ??= new native.API({
27
- cwd: root,
28
- fs: {
29
- readFile: (file) => virtual.get(key(file)),
30
- fileExists: (file) => virtual.has(key(file)) ? true : undefined,
31
- },
32
- }));
33
- }
34
- function update(config) {
35
- const previous = snapshot;
36
- snapshot = client().updateSnapshot({
37
- ...(!opened.has(config) ? { openProjects: [config] } : {}),
38
- fileChanges: { invalidateAll: true },
39
- });
40
- opened.add(config);
41
- previous?.dispose();
42
- const project = snapshot.getProject(config);
43
- if (!project)
44
- throw new Error(`TypeScript 7 did not open the analysis project: ${config}`);
45
- return project;
46
- }
47
- function overlayConfig(path, config) {
48
- if (existsSync(path))
49
- throw new Error(`Reserved analysis config already exists: ${path}`);
50
- virtual.set(key(path), JSON.stringify(config));
51
- }
52
- const kinds = ast.SyntaxKind;
53
- const functionKinds = new Set([
54
- "FunctionDeclaration",
55
- "MethodDeclaration",
56
- "Constructor",
57
- "GetAccessor",
58
- "SetAccessor",
59
- "FunctionExpression",
60
- "ArrowFunction",
61
- "MethodSignature",
62
- "CallSignature",
63
- "JSDocSignature",
64
- "ConstructSignature",
65
- "IndexSignature",
66
- "FunctionType",
67
- "JSDocFunctionType",
68
- "ConstructorType",
69
- ]
70
- .map((name) => kinds[name])
71
- .filter((value) => typeof value === "number"));
72
- const loopKinds = new Set([
73
- "ForStatement",
74
- "ForInStatement",
75
- "ForOfStatement",
76
- "WhileStatement",
77
- "DoStatement",
78
- ].map((name) => kinds[name]));
79
- // These adapters preserve native nodes and native enum values. No TS5 predicate
80
- // may inspect a TS7 node: syntax-kind numeric values are different.
81
- const syntax = {
82
- ...ast,
83
- SymbolFlags: native.SymbolFlags,
84
- forEachChild: (node, visit, visitArray) => node.forEachChild(visit, visitArray),
85
- isFunctionLike: (node) => !!node && functionKinds.has(node.kind),
86
- isIterationStatement: (node, lookInLabeledStatements) => !!node &&
87
- (loopKinds.has(node.kind) ||
88
- (lookInLabeledStatements &&
89
- node.kind === kinds.LabeledStatement &&
90
- syntax.isIterationStatement(node.statement, true))),
91
- isMethodSignature: ast.isMethodSignatureDeclaration,
92
- isParameter: ast.isParameterDeclaration,
93
- isTypeAssertionExpression: ast.isTypeAssertion,
94
- isStringLiteralLike: (node) => !!node &&
95
- (node.kind === kinds.StringLiteral ||
96
- node.kind === kinds.NoSubstitutionTemplateLiteral),
97
- canHaveModifiers: (node) => !!node && "modifiers" in node,
98
- getModifiers: (node) => node.modifiers?.filter((m) => m.kind !== kinds.Decorator),
99
- };
100
- for (const name of [
101
- "isMethodSignature",
102
- "isParameter",
103
- "isTypeAssertionExpression",
104
- "getLeadingCommentRanges",
105
- "getTrailingCommentRanges",
106
- ])
107
- if (typeof syntax[name] !== "function")
108
- throw new Error(`Unsupported TypeScript 7 syntax API: ${name}`);
109
- const frontend = {
110
- kind: "typescript-native-7",
111
- version: "7.0.2",
112
- syntax,
113
- limitations,
114
- close() {
115
- if (closed)
116
- return;
117
- closed = true;
118
- try {
119
- snapshot?.dispose();
120
- }
121
- finally {
122
- api?.close();
123
- }
124
- },
125
- parseSource(file, text) {
126
- const absolute = resolve(root, file);
127
- virtual.set(key(absolute), text);
128
- // Do not rely on 7.0.2 reloading an edited virtual config's root-file list.
129
- const parseConfig = resolve(root, `.supercov-asserted-parse-${++parseSerial}.tsconfig.json`);
130
- overlayConfig(parseConfig, {
131
- files: [absolute],
132
- compilerOptions: {
133
- allowJs: true,
134
- checkJs: true,
135
- noResolve: true,
136
- noLib: true,
137
- noEmit: true,
138
- types: [],
139
- },
140
- });
141
- const project = update(parseConfig);
142
- const source = project.program.getSourceFile(absolute);
143
- if (!source || source.text !== text)
144
- throw new Error(`Native parser source mismatch: ${file}`);
145
- const errors = project.program.getSyntacticDiagnostics(absolute);
146
- if (errors.length)
147
- throw new Error(`Native parser rejected ${file}: ${JSON.stringify(errors)}`);
148
- return source;
149
- },
150
- openProgram(options) {
151
- const configPath = resolve(root, options.tsconfig ?? "tsconfig.json");
152
- const hasConfig = existsSync(configPath);
153
- if (!hasConfig && !options.sourceFiles)
154
- throw new Error(`Missing TypeScript config: ${configPath}`);
155
- const configured = hasConfig
156
- ? client().parseConfigFile(configPath).fileNames
157
- : [];
158
- const files = selectedRootFiles(options, configured);
159
- const overlay = resolve(dirname(configPath), ".supercov-asserted-query.tsconfig.json");
160
- overlayConfig(overlay, {
161
- ...(hasConfig ? { extends: configPath } : {}),
162
- files,
163
- include: [],
164
- exclude: [],
165
- compilerOptions: {
166
- ...(options.sourceFiles ? { allowJs: true, checkJs: true } : {}),
167
- noEmit: true,
168
- },
169
- });
170
- const project = update(overlay);
171
- const program = project.program;
172
- const configErrors = program.getConfigFileParsingDiagnostics();
173
- if (configErrors.length)
174
- throw new Error(`TypeScript 7 configuration is invalid: ${JSON.stringify(configErrors)}`);
175
- const checker = project.checker;
176
- const symbols = new WeakMap();
177
- const nativeSymbols = new WeakMap();
178
- function wrapSymbol(symbol) {
179
- if (!symbol)
180
- return undefined;
181
- let wrapped = symbols.get(symbol);
182
- if (!wrapped) {
183
- wrapped = {
184
- flags: symbol.flags,
185
- name: symbol.name,
186
- escapedName: symbol.escapedName,
187
- get valueDeclaration() {
188
- return symbol.valueDeclaration?.resolve(project);
189
- },
190
- get declarations() {
191
- return symbol.declarations.map((handle) => {
192
- const node = handle.resolve(project);
193
- if (!node)
194
- throw new Error("TypeScript 7 declaration handle did not resolve");
195
- return node;
196
- });
197
- },
198
- };
199
- symbols.set(symbol, wrapped);
200
- nativeSymbols.set(wrapped, symbol);
201
- }
202
- return wrapped;
203
- }
204
- const allFiles = program
205
- .getSourceFileNames()
206
- .map((file) => {
207
- const source = program.getSourceFile(file);
208
- if (!source)
209
- throw new Error(`Native source disappeared: ${file}`);
210
- return source;
211
- });
212
- const resolutionCache = new Map();
213
- return {
214
- files: allFiles,
215
- checker: {
216
- getSymbolAtLocation: (node) => wrapSymbol(checker.getSymbolAtLocation(node)),
217
- getShorthandAssignmentValueSymbol: (node) => wrapSymbol(checker.getShorthandAssignmentValueSymbol(node)),
218
- getAliasedSymbol: (symbol) => {
219
- const original = nativeSymbols.get(symbol);
220
- if (!original)
221
- throw new Error("Symbol belongs to a different compiler session");
222
- return wrapSymbol(checker.getAliasedSymbol(original));
223
- },
224
- getResolvedSignature: (node) => {
225
- const signature = checker.getResolvedSignature(node);
226
- return signature
227
- ? {
228
- getParameters: () => signature.getParameters().map(wrapSymbol),
229
- }
230
- : undefined;
231
- },
232
- },
233
- resolveModule(specifier, from) {
234
- const cacheKey = `${key(from)}\0${specifier}`;
235
- if (resolutionCache.has(cacheKey))
236
- return resolutionCache.get(cacheKey);
237
- const source = program.getSourceFile(from);
238
- const paths = new Set();
239
- const visit = (node) => {
240
- if (syntax.isStringLiteralLike(node) && node.text === specifier) {
241
- // Ask the native checker about actual module-reference syntax. A
242
- // vi.mock string is not a TypeScript import and is not guessed.
243
- const p = node.parent;
244
- if (p &&
245
- (ast.isImportDeclaration(p) ||
246
- ast.isExportDeclaration(p) ||
247
- (ast.isCallExpression(p) &&
248
- p.expression.kind === kinds.ImportKeyword))) {
249
- const symbol = checker.getSymbolAtLocation(node);
250
- for (const handle of symbol?.declarations ?? []) {
251
- const declaration = handle.resolve(project);
252
- if (declaration && syntax.isSourceFile(declaration))
253
- paths.add(declaration.fileName);
254
- }
255
- }
256
- }
257
- node.forEachChild(visit);
258
- };
259
- if (source)
260
- visit(source);
261
- const result = paths.size === 1 ? [...paths][0] : undefined;
262
- if (!result && !isBuiltin(specifier))
263
- limitations.add(`native-module-resolution: ${key(from)} → ${specifier}`);
264
- resolutionCache.set(cacheKey, result);
265
- return result;
266
- },
267
- };
268
- },
269
- };
270
- return frontend;
271
- }
@@ -1,186 +0,0 @@
1
- export function assertionWitnessIssue(phases, source, method) {
2
- const matches = (phases ?? []).filter((p) => p.source === source && p.op.split(".").pop() === method);
3
- if (source && matches.length && matches.every((p) => p.status === "passed"))
4
- return undefined;
5
- const statuses = new Set(matches.map((p) => p.status ?? "unknown"));
6
- return !phases
7
- ? "capture-unavailable"
8
- : !source
9
- ? "uninstrumented-observation"
10
- : !matches.length
11
- ? "call-not-recorded"
12
- : statuses.size > 1
13
- ? "mixed-call-outcomes"
14
- : statuses.has("failed")
15
- ? "call-failed"
16
- : "call-incomplete";
17
- }
18
- /** Source hints are kept OUT of observations. A comment never adds a boundary. */
19
- export function collectPragmas(compiler, files, relativeFile, sites) {
20
- const comments = new Map();
21
- const key = (sf, pos) => `${relativeFile(sf)}:${pos}`;
22
- const location = (sf, pos) => {
23
- const p = sf.getLineAndCharacterOfPosition(pos);
24
- return `${relativeFile(sf)}:${p.line + 1}:${p.character + 1}`;
25
- };
26
- for (const sf of files) {
27
- const collect = (ranges) => {
28
- for (const range of ranges ?? []) {
29
- const raw = sf.text.slice(range.pos, range.end);
30
- if (!/^\/\/\s*observes:/.test(raw) || comments.has(key(sf, range.pos)))
31
- continue;
32
- const parts = raw.split(/;\s*check\s+/);
33
- const check = parts.length === 2 && parts[1].trim() === "missing call"
34
- ? "missing-call"
35
- : parts.length === 2 && parts[1].trim() === "count"
36
- ? "count"
37
- : parts.length === 2 && parts[1].trim() === "value"
38
- ? "value"
39
- : parts.length === 2 && parts[1].trim() === "completion"
40
- ? "completion"
41
- : undefined;
42
- const checkIssue = parts.length > 1 && !check ? "unsupported-check-recipe" : undefined;
43
- const parsed = /^\/\/\s*observes:\s*(\S+?)#(\S+)(?:\s+(.+?))?\s*$/.exec(parts[0]);
44
- const suffix = parsed?.[3]?.split(/(?:^|\s+)via\s+/);
45
- const target = parsed
46
- ? {
47
- file: parsed[1],
48
- function: parsed[2],
49
- snippet: suffix?.[0]?.trim() || undefined,
50
- via: suffix?.slice(1).join(" via ").trim() || undefined,
51
- }
52
- : undefined;
53
- const validPath = target &&
54
- !/[\\:]/.test(target.file) &&
55
- target.file
56
- .split("/")
57
- .every((part) => part && part !== "." && part !== "..");
58
- const matchingSites = validPath
59
- ? sites.filter((s) => s.file === target.file &&
60
- // The recipe selects the emission, not a containing callback-return site.
61
- (!check ||
62
- (check === "missing-call"
63
- ? s.category === "log"
64
- : check === "completion"
65
- ? s.category === "return" || s.category === "throw"
66
- : s.kind === "decision" || s.category === "return")) &&
67
- (s.owner === target.function || s.fn === target.function) &&
68
- (!target.snippet || s.text.includes(target.snippet)))
69
- : [];
70
- // An exact whole-site selector is more specific than a containing return
71
- // whose display text happens to include that expression. Equal sibling
72
- // sites remain ambiguous. This only selects a node; it proves no behavior.
73
- const exact = check && target?.snippet
74
- ? matchingSites.filter((s) => s.text.trim() === target.snippet.trim())
75
- : [];
76
- const candidates = (exact.length ? exact : matchingSites).map((s) => s.id);
77
- comments.set(key(sf, range.pos), {
78
- id: location(sf, range.pos),
79
- where: location(sf, range.pos),
80
- raw,
81
- target,
82
- ...(check ? { check } : {}),
83
- candidateSites: candidates,
84
- issue: checkIssue ??
85
- (!target
86
- ? "invalid-syntax"
87
- : !validPath
88
- ? "invalid-target-path"
89
- : !candidates.length
90
- ? "target-not-in-inventory"
91
- : candidates.length > 1
92
- ? "ambiguous-target"
93
- : undefined),
94
- attachments: [],
95
- });
96
- }
97
- };
98
- const visit = (node) => {
99
- collect(compiler.getLeadingCommentRanges(sf.text, node.getFullStart()));
100
- collect(compiler.getTrailingCommentRanges(sf.text, node.getEnd()));
101
- compiler.forEachChild(node, visit);
102
- };
103
- visit(sf);
104
- }
105
- return {
106
- hasHint(node) {
107
- let statement = node;
108
- while (statement.parent && !compiler.isStatement(statement))
109
- statement = statement.parent;
110
- if (!compiler.isExpressionStatement(statement))
111
- return false;
112
- const sf = node.getSourceFile();
113
- return (compiler.getLeadingCommentRanges(sf.text, statement.getFullStart()) ??
114
- []).some((range) => comments.has(key(sf, range.pos)));
115
- },
116
- register(node, method, testKey, inert, awaitedObservation) {
117
- let statement = node;
118
- while (statement.parent && !compiler.isStatement(statement))
119
- statement = statement.parent;
120
- // A comment on an if/block/declaration is not a claim about every assertion inside it.
121
- if (!compiler.isExpressionStatement(statement))
122
- return;
123
- const sf = node.getSourceFile();
124
- for (const range of compiler.getLeadingCommentRanges(sf.text, statement.getFullStart()) ?? []) {
125
- const comment = comments.get(key(sf, range.pos));
126
- if (!comment)
127
- continue;
128
- const attachment = {
129
- testKey,
130
- source: location(sf, node.getStart(sf)),
131
- method,
132
- inert,
133
- ...(awaitedObservation ? { awaitedObservation } : {}),
134
- };
135
- if (!comment.attachments.some((a) => a.testKey === testKey &&
136
- a.source === attachment.source &&
137
- a.method === method))
138
- comment.attachments.push(attachment);
139
- }
140
- },
141
- finish(bindings) {
142
- return [...comments.values()].flatMap(({ attachments, ...comment }) => {
143
- const sources = new Set(attachments.map((a) => `${a.source}#${a.method}`));
144
- const issue = comment.issue ??
145
- (sources.size > 1
146
- ? "ambiguous-assertion"
147
- : !sources.size
148
- ? "unattached-assertion"
149
- : undefined);
150
- const matched = bindings.flatMap((b) => attachments
151
- .filter((a) => a.testKey === b.testKey && !a.inert)
152
- .map((a) => ({ a, b })));
153
- if (sources.size !== 1 || !matched.length)
154
- return [
155
- {
156
- ...comment,
157
- issue: issue ?? "no-owning-passed-test",
158
- witness: "unavailable",
159
- },
160
- ];
161
- return matched.map(({ a, b }) => {
162
- // A source-checked poll is not an explicit assertion phase. Neither
163
- // a matching phase name nor a passing test can supply its read receipt.
164
- const witnessIssue = a.awaitedObservation
165
- ? "observation-capture-unavailable"
166
- : assertionWitnessIssue(b.phases, a.source, a.method);
167
- return {
168
- ...comment,
169
- id: `${comment.id}@${b.id}`,
170
- issue,
171
- test: b.id,
172
- assertionSource: a.source,
173
- assertionMethod: a.method,
174
- ...(a.awaitedObservation
175
- ? { awaitedObservation: a.awaitedObservation }
176
- : {}),
177
- witness: witnessIssue
178
- ? "unavailable"
179
- : "passed",
180
- witnessIssue,
181
- };
182
- });
183
- });
184
- },
185
- };
186
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
1
- {
2
- "name": "@supercov/asserted-typescript",
3
- "version": "0.0.0",
4
- "private": true,
5
- "description": "TypeScript assertion and value-flow facts for Supercov's language-neutral join",
6
- "type": "module",
7
- "engines": {
8
- "node": ">=22"
9
- },
10
- "files": [
11
- "dist",
12
- "bin",
13
- "src",
14
- "tsconfig.json",
15
- "README.md"
16
- ],
17
- "scripts": {
18
- "build": "node node_modules/typescript/bin/tsc -p tsconfig.json && node bin/identity.mjs",
19
- "test": "npm run build && node --test tests/*.test.mjs",
20
- "prepack": "npm run build"
21
- },
22
- "devDependencies": {
23
- "@types/node": "24.0.0",
24
- "typescript": "5.8.3",
25
- "typescript-native": "npm:typescript@7.0.2"
26
- }
27
- }