supercov 0.0.41 → 0.0.43

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 (35) hide show
  1. package/README.md +24 -4
  2. package/analyzers/typescript/README.md +55 -0
  3. package/analyzers/typescript/bin/compiler-identity.mjs +78 -0
  4. package/analyzers/typescript/bin/identity.mjs +67 -0
  5. package/analyzers/typescript/bin/query.mjs +29 -0
  6. package/analyzers/typescript/dist/analyze.js +3972 -0
  7. package/analyzers/typescript/dist/archive.js +309 -0
  8. package/analyzers/typescript/dist/build-identity.json +1 -0
  9. package/analyzers/typescript/dist/compiler.js +32 -0
  10. package/analyzers/typescript/dist/frontend.js +75 -0
  11. package/analyzers/typescript/dist/native-frontend.js +271 -0
  12. package/analyzers/typescript/dist/pragmas.js +143 -0
  13. package/analyzers/typescript/dist/types.js +1 -0
  14. package/analyzers/typescript/package.json +27 -0
  15. package/analyzers/typescript/src/analyze.ts +4538 -0
  16. package/analyzers/typescript/src/archive.ts +438 -0
  17. package/analyzers/typescript/src/compiler.ts +49 -0
  18. package/analyzers/typescript/src/frontend.ts +136 -0
  19. package/analyzers/typescript/src/native-frontend.ts +315 -0
  20. package/analyzers/typescript/src/pragmas.ts +218 -0
  21. package/analyzers/typescript/src/types.ts +45 -0
  22. package/analyzers/typescript/tsconfig.json +12 -0
  23. package/docs/agent-loop.md +13 -0
  24. package/docs/assertion-evidence.md +135 -0
  25. package/docs/cli.md +10 -0
  26. package/docs/code-verification.md +182 -0
  27. package/docs/supported-suites.md +44 -11
  28. package/docs/troubleshooting.md +13 -0
  29. package/docs/verification.md +12 -0
  30. package/package.json +33 -15
  31. package/runtime/javascript/jest.cjs +134 -0
  32. package/runtime/javascript/jest.config.mjs +39 -0
  33. package/runtime/javascript/jestReporter.mjs +77 -0
  34. package/runtime/javascript/register.mjs +22 -4
  35. package/runtime/javascript/runtime.mjs +51 -13
@@ -0,0 +1,271 @@
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
+ }
@@ -0,0 +1,143 @@
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 parsed = /^\/\/\s*observes:\s*(\S+?)#(\S+)(?:\s+(.+?))?\s*$/.exec(raw);
33
+ const suffix = parsed?.[3]?.split(/(?:^|\s+)via\s+/);
34
+ const target = parsed
35
+ ? {
36
+ file: parsed[1],
37
+ function: parsed[2],
38
+ snippet: suffix?.[0]?.trim() || undefined,
39
+ via: suffix?.slice(1).join(" via ").trim() || undefined,
40
+ }
41
+ : undefined;
42
+ const validPath = target &&
43
+ !/[\\:]/.test(target.file) &&
44
+ target.file
45
+ .split("/")
46
+ .every((part) => part && part !== "." && part !== "..");
47
+ const candidates = validPath
48
+ ? sites
49
+ .filter((s) => s.file === target.file &&
50
+ (s.owner === target.function || s.fn === target.function) &&
51
+ (!target.snippet || s.text.includes(target.snippet)))
52
+ .map((s) => s.id)
53
+ : [];
54
+ comments.set(key(sf, range.pos), {
55
+ id: location(sf, range.pos),
56
+ where: location(sf, range.pos),
57
+ raw,
58
+ target,
59
+ candidateSites: candidates,
60
+ issue: !target
61
+ ? "invalid-syntax"
62
+ : !validPath
63
+ ? "invalid-target-path"
64
+ : !candidates.length
65
+ ? "target-not-in-inventory"
66
+ : candidates.length > 1
67
+ ? "ambiguous-target"
68
+ : undefined,
69
+ attachments: [],
70
+ });
71
+ }
72
+ };
73
+ const visit = (node) => {
74
+ collect(compiler.getLeadingCommentRanges(sf.text, node.getFullStart()));
75
+ collect(compiler.getTrailingCommentRanges(sf.text, node.getEnd()));
76
+ compiler.forEachChild(node, visit);
77
+ };
78
+ visit(sf);
79
+ }
80
+ return {
81
+ register(node, method, testKey, inert) {
82
+ let statement = node;
83
+ while (statement.parent && !compiler.isStatement(statement))
84
+ statement = statement.parent;
85
+ // A comment on an if/block/declaration is not a claim about every assertion inside it.
86
+ if (!compiler.isExpressionStatement(statement))
87
+ return;
88
+ const sf = node.getSourceFile();
89
+ for (const range of compiler.getLeadingCommentRanges(sf.text, statement.getFullStart()) ?? []) {
90
+ const comment = comments.get(key(sf, range.pos));
91
+ if (!comment)
92
+ continue;
93
+ const attachment = {
94
+ testKey,
95
+ source: location(sf, node.getStart(sf)),
96
+ method,
97
+ inert,
98
+ };
99
+ if (!comment.attachments.some((a) => a.testKey === testKey &&
100
+ a.source === attachment.source &&
101
+ a.method === method))
102
+ comment.attachments.push(attachment);
103
+ }
104
+ },
105
+ finish(bindings) {
106
+ return [...comments.values()].flatMap(({ attachments, ...comment }) => {
107
+ const sources = new Set(attachments.map((a) => `${a.source}#${a.method}`));
108
+ const issue = comment.issue ??
109
+ (sources.size > 1
110
+ ? "ambiguous-assertion"
111
+ : !sources.size
112
+ ? "unattached-assertion"
113
+ : undefined);
114
+ const matched = bindings.flatMap((b) => attachments
115
+ .filter((a) => a.testKey === b.testKey && !a.inert)
116
+ .map((a) => ({ a, b })));
117
+ if (sources.size !== 1 || !matched.length)
118
+ return [
119
+ {
120
+ ...comment,
121
+ issue: issue ?? "no-owning-passed-test",
122
+ witness: "unavailable",
123
+ },
124
+ ];
125
+ return matched.map(({ a, b }) => {
126
+ const witnessIssue = assertionWitnessIssue(b.phases, a.source, a.method);
127
+ return {
128
+ ...comment,
129
+ id: `${comment.id}@${b.id}`,
130
+ issue,
131
+ test: b.id,
132
+ assertionSource: a.source,
133
+ assertionMethod: a.method,
134
+ witness: witnessIssue
135
+ ? "unavailable"
136
+ : "passed",
137
+ witnessIssue,
138
+ };
139
+ });
140
+ });
141
+ },
142
+ };
143
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
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
+ }