supercov 0.0.0 → 0.0.2

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 (97) hide show
  1. package/bin/supercov.js +2 -5
  2. package/dist/analyze.d.ts +12 -0
  3. package/dist/analyze.d.ts.map +1 -0
  4. package/dist/analyze.js +699 -0
  5. package/dist/analyze.js.map +1 -0
  6. package/dist/cli.d.ts +2 -0
  7. package/dist/cli.d.ts.map +1 -0
  8. package/dist/cli.js +245 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/instrumenter.d.ts +11 -0
  11. package/dist/instrumenter.d.ts.map +1 -0
  12. package/dist/instrumenter.js +940 -0
  13. package/dist/instrumenter.js.map +1 -0
  14. package/dist/integrity.d.ts +10 -0
  15. package/dist/integrity.d.ts.map +1 -0
  16. package/dist/integrity.js +153 -0
  17. package/dist/integrity.js.map +1 -0
  18. package/dist/playwright.d.ts +13 -0
  19. package/dist/playwright.d.ts.map +1 -0
  20. package/dist/playwright.js +794 -0
  21. package/dist/playwright.js.map +1 -0
  22. package/dist/playwrightReporter.d.ts +6 -0
  23. package/dist/playwrightReporter.d.ts.map +1 -0
  24. package/dist/playwrightReporter.js +40 -0
  25. package/dist/playwrightReporter.js.map +1 -0
  26. package/dist/project.d.ts +11 -0
  27. package/dist/project.d.ts.map +1 -0
  28. package/dist/project.js +112 -0
  29. package/dist/project.js.map +1 -0
  30. package/dist/provenance.d.ts +9 -0
  31. package/dist/provenance.d.ts.map +1 -0
  32. package/dist/provenance.js +51 -0
  33. package/dist/provenance.js.map +1 -0
  34. package/dist/query.d.ts +9 -0
  35. package/dist/query.d.ts.map +1 -0
  36. package/dist/query.js +911 -0
  37. package/dist/query.js.map +1 -0
  38. package/dist/queueAdapters.d.ts +15 -0
  39. package/dist/queueAdapters.d.ts.map +1 -0
  40. package/dist/queueAdapters.js +55 -0
  41. package/dist/queueAdapters.js.map +1 -0
  42. package/dist/register.d.mts +2 -0
  43. package/dist/register.d.mts.map +1 -0
  44. package/dist/register.mjs +112 -0
  45. package/dist/register.mjs.map +1 -0
  46. package/dist/reporter.d.ts +17 -0
  47. package/dist/reporter.d.ts.map +1 -0
  48. package/dist/reporter.js +311 -0
  49. package/dist/reporter.js.map +1 -0
  50. package/dist/resolve-loader.d.mts +2 -0
  51. package/dist/resolve-loader.d.mts.map +1 -0
  52. package/dist/resolve-loader.mjs +40 -0
  53. package/dist/resolve-loader.mjs.map +1 -0
  54. package/dist/runtime.d.ts +46 -0
  55. package/dist/runtime.d.ts.map +1 -0
  56. package/dist/runtime.js +480 -0
  57. package/dist/runtime.js.map +1 -0
  58. package/dist/transport.d.ts +15 -0
  59. package/dist/transport.d.ts.map +1 -0
  60. package/dist/transport.js +100 -0
  61. package/dist/transport.js.map +1 -0
  62. package/dist/types.d.ts +353 -0
  63. package/dist/types.d.ts.map +1 -0
  64. package/dist/types.js +2 -0
  65. package/dist/types.js.map +1 -0
  66. package/dist/vitePlugin.d.ts +8 -0
  67. package/dist/vitePlugin.d.ts.map +1 -0
  68. package/dist/vitePlugin.js +83 -0
  69. package/dist/vitePlugin.js.map +1 -0
  70. package/dist/vitest.d.ts +2 -0
  71. package/dist/vitest.d.ts.map +1 -0
  72. package/dist/vitest.js +89 -0
  73. package/dist/vitest.js.map +1 -0
  74. package/dist/vitestReporter.d.ts +28 -0
  75. package/dist/vitestReporter.d.ts.map +1 -0
  76. package/dist/vitestReporter.js +48 -0
  77. package/dist/vitestReporter.js.map +1 -0
  78. package/package.json +21 -9
  79. package/src/analyze.ts +0 -993
  80. package/src/cli.ts +0 -336
  81. package/src/instrumenter.ts +0 -1254
  82. package/src/integrity.ts +0 -187
  83. package/src/playwright.ts +0 -1009
  84. package/src/playwrightReporter.ts +0 -55
  85. package/src/project.ts +0 -149
  86. package/src/provenance.ts +0 -69
  87. package/src/query.ts +0 -1354
  88. package/src/queueAdapters.ts +0 -104
  89. package/src/register.mjs +0 -123
  90. package/src/reporter.ts +0 -431
  91. package/src/resolve-loader.mjs +0 -45
  92. package/src/runtime.ts +0 -656
  93. package/src/transport.ts +0 -132
  94. package/src/types.ts +0 -412
  95. package/src/vitePlugin.ts +0 -121
  96. package/src/vitest.ts +0 -101
  97. package/src/vitestReporter.ts +0 -72
@@ -0,0 +1,940 @@
1
+ import { createHash } from "node:crypto";
2
+ import generate from "@babel/generator";
3
+ import { parse } from "@babel/parser";
4
+ import traverse from "@babel/traverse";
5
+ import * as t from "@babel/types";
6
+ const RUNTIME_MODULE = "virtual:supercov-runtime";
7
+ const BEGIN = "__supercovMcdcBegin";
8
+ const CONDITION = "__supercovMcdcCondition";
9
+ const END = "__supercovMcdcEnd";
10
+ const HIT = "__supercovCoverageHit";
11
+ const SELECTION_BEGIN = "__supercovSelectionBegin";
12
+ const SELECTION_RIGHT = "__supercovSelectionRight";
13
+ const SELECTION_END = "__supercovSelectionEnd";
14
+ const WITH_REQUEST_PHASE = "__supercovWithRequestPhase";
15
+ const OPTIONAL_SELECT = "__supercovOptionalSelect";
16
+ const DEFAULT_SELECTED = "__supercovDefaultSelected";
17
+ const DEFAULT_ENTERED = "__supercovDefaultEntered";
18
+ const TRY_BEGIN = "__supercovTryBegin";
19
+ const TRY_CATCH = "__supercovTryCatch";
20
+ const TRY_END = "__supercovTryEnd";
21
+ const LOOP_BEGIN = "__supercovLoopBegin";
22
+ const LOOP_ENTERED = "__supercovLoopEntered";
23
+ const LOOP_END = "__supercovLoopEnd";
24
+ function isRemixRequestHandlerName(name) {
25
+ return name === "loader" || name === "action";
26
+ }
27
+ function sourceFor(code, node) {
28
+ if (node.start !== null &&
29
+ node.start !== undefined &&
30
+ node.end !== null &&
31
+ node.end !== undefined) {
32
+ return code.slice(node.start, node.end);
33
+ }
34
+ return generate(node).code;
35
+ }
36
+ function stableId(file, kind, node, suffix = "") {
37
+ return createHash("sha256")
38
+ .update(`${file}:${kind}:${node.start ?? 0}:${node.end ?? 0}:${suffix}`)
39
+ .digest("hex")
40
+ .slice(0, 16);
41
+ }
42
+ function hasCompoundBooleanDecision(node) {
43
+ if (t.isLogicalExpression(node) &&
44
+ (node.operator === "&&" || node.operator === "||")) {
45
+ return true;
46
+ }
47
+ return (t.isUnaryExpression(node, { operator: "!" }) &&
48
+ hasCompoundBooleanDecision(node.argument));
49
+ }
50
+ function collectConditions(node, conditions) {
51
+ if (t.isLogicalExpression(node) &&
52
+ (node.operator === "&&" || node.operator === "||")) {
53
+ collectConditions(node.left, conditions);
54
+ collectConditions(node.right, conditions);
55
+ return;
56
+ }
57
+ if (t.isUnaryExpression(node, { operator: "!" }) &&
58
+ hasCompoundBooleanDecision(node.argument)) {
59
+ collectConditions(node.argument, conditions);
60
+ return;
61
+ }
62
+ conditions.push(node);
63
+ }
64
+ function instrumentConditions(node, frameId, nextIndex, decisionLogicalNodes) {
65
+ if (t.isLogicalExpression(node) &&
66
+ (node.operator === "&&" || node.operator === "||")) {
67
+ const logical = t.logicalExpression(node.operator, instrumentConditions(node.left, frameId, nextIndex, decisionLogicalNodes), instrumentConditions(node.right, frameId, nextIndex, decisionLogicalNodes));
68
+ decisionLogicalNodes.add(logical);
69
+ return logical;
70
+ }
71
+ if (t.isUnaryExpression(node, { operator: "!" }) &&
72
+ hasCompoundBooleanDecision(node.argument)) {
73
+ return t.unaryExpression("!", instrumentConditions(node.argument, frameId, nextIndex, decisionLogicalNodes), true);
74
+ }
75
+ const index = nextIndex.value;
76
+ nextIndex.value += 1;
77
+ return t.callExpression(t.identifier(CONDITION), [
78
+ t.cloneNode(frameId),
79
+ t.numericLiteral(index),
80
+ t.cloneNode(node, true),
81
+ ]);
82
+ }
83
+ function hitStatement(id) {
84
+ return t.expressionStatement(t.callExpression(t.identifier(HIT), [t.stringLiteral(id)]));
85
+ }
86
+ function functionLabel(path) {
87
+ if ("id" in path.node && t.isIdentifier(path.node.id))
88
+ return path.node.id.name;
89
+ const parent = path.parentPath;
90
+ if (parent.isObjectProperty() ||
91
+ parent.isObjectMethod() ||
92
+ parent.isClassMethod()) {
93
+ const key = parent.node.key;
94
+ if (t.isIdentifier(key))
95
+ return key.name;
96
+ if (t.isStringLiteral(key))
97
+ return key.value;
98
+ }
99
+ if (parent.isVariableDeclarator() && t.isIdentifier(parent.node.id))
100
+ return parent.node.id.name;
101
+ return undefined;
102
+ }
103
+ function isExecutableStatement(node) {
104
+ if (t.isBlockStatement(node) ||
105
+ t.isEmptyStatement(node) ||
106
+ t.isFunctionDeclaration(node) ||
107
+ node.type.startsWith("TS")) {
108
+ return false;
109
+ }
110
+ return !("declare" in node && node.declare === true);
111
+ }
112
+ export function instrumentMcdc(code, file) {
113
+ const parserPlugins = [
114
+ "typescript",
115
+ "decorators-legacy",
116
+ ...(file.endsWith("x") ? ["jsx"] : []),
117
+ ];
118
+ const ast = parse(code, {
119
+ sourceType: "unambiguous",
120
+ sourceFilename: file,
121
+ errorRecovery: true,
122
+ plugins: parserPlugins,
123
+ });
124
+ const decisions = [];
125
+ const points = [];
126
+ const branches = [];
127
+ const limitations = [];
128
+ const generatedStatements = new WeakSet();
129
+ const decisionLogicalNodes = new WeakSet();
130
+ let usesRequestPhaseHandler = false;
131
+ // Record executable statements before adding any instrumentation statements.
132
+ traverse(ast, {
133
+ Statement(path) {
134
+ const node = path.node;
135
+ if (!node.loc ||
136
+ generatedStatements.has(node) ||
137
+ !isExecutableStatement(node))
138
+ return;
139
+ if (path.parentPath.isLabeledStatement())
140
+ return;
141
+ const id = stableId(file, "statement", node);
142
+ points.push({
143
+ id,
144
+ kind: "statement",
145
+ file,
146
+ line: node.loc.start.line,
147
+ column: node.loc.start.column + 1,
148
+ source: sourceFor(code, node),
149
+ });
150
+ const probe = hitStatement(id);
151
+ generatedStatements.add(probe);
152
+ if (path.parentPath.isProgram() ||
153
+ path.parentPath.isBlockStatement() ||
154
+ path.parentPath.isSwitchCase()) {
155
+ path.insertBefore(probe);
156
+ return;
157
+ }
158
+ // Bare control-flow bodies become blocks. This preserves dangling-else,
159
+ // break, continue, return, and throw semantics while giving the body an
160
+ // independently observable statement entry.
161
+ if (path.key === "body" ||
162
+ path.key === "consequent" ||
163
+ path.key === "alternate") {
164
+ path.replaceWith(t.blockStatement([probe, node]));
165
+ path.skip();
166
+ }
167
+ },
168
+ });
169
+ // Optional links are measured at the exact nullish operand. Unlike checking
170
+ // the chain's final value, this remains accurate when a successful property
171
+ // access or function call legitimately returns undefined.
172
+ const instrumentOptionalOperand = (path, operand) => {
173
+ const node = path.node;
174
+ if (!node.loc)
175
+ return operand;
176
+ const id = stableId(file, "optional-chain", node);
177
+ const shortId = `${id}:short`;
178
+ const continuedId = `${id}:continued`;
179
+ branches.push({
180
+ id,
181
+ kind: "optional-chain",
182
+ file,
183
+ line: node.loc.start.line,
184
+ column: node.loc.start.column + 1,
185
+ source: sourceFor(code, node),
186
+ alternatives: [
187
+ { id: shortId, label: "nullish / short-circuited" },
188
+ { id: continuedId, label: "non-nullish / continued" },
189
+ ],
190
+ });
191
+ return t.callExpression(t.identifier(OPTIONAL_SELECT), [
192
+ t.stringLiteral(shortId),
193
+ t.stringLiteral(continuedId),
194
+ operand,
195
+ ]);
196
+ };
197
+ traverse(ast, {
198
+ OptionalMemberExpression(path) {
199
+ if (!path.node.optional || !t.isExpression(path.node.object))
200
+ return;
201
+ path.node.object = instrumentOptionalOperand(path, path.node.object);
202
+ },
203
+ OptionalCallExpression(path) {
204
+ if (!path.node.optional || !t.isExpression(path.node.callee))
205
+ return;
206
+ const callee = path.node.callee;
207
+ if (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) {
208
+ // Preserve the receiver of `object.method?.()` by evaluating the
209
+ // object and method once and calling through Function#call.
210
+ if (t.isSuper(callee.object) || !t.isExpression(callee.object)) {
211
+ if (path.node.loc) {
212
+ limitations.push({
213
+ id: stableId(file, "dynamic-code", path.node, "optional-super"),
214
+ kind: "dynamic-code",
215
+ file,
216
+ line: path.node.loc.start.line,
217
+ column: path.node.loc.start.column + 1,
218
+ source: sourceFor(code, path.node),
219
+ reason: "optional calls through super cannot be probed without changing receiver semantics",
220
+ });
221
+ }
222
+ return;
223
+ }
224
+ const objectId = path.scope.generateUidIdentifier("supercovOptionalObject");
225
+ const callableId = path.scope.generateUidIdentifier("supercovOptionalCallable");
226
+ const frameScope = path.scope.getFunctionParent() ?? path.scope.getProgramParent();
227
+ frameScope.push({ id: t.cloneNode(objectId), kind: "let" });
228
+ frameScope.push({ id: t.cloneNode(callableId), kind: "let" });
229
+ const objectAssignment = t.assignmentExpression("=", t.cloneNode(objectId), callee.object);
230
+ const member = t.memberExpression(t.cloneNode(objectId), t.cloneNode(callee.property), callee.computed);
231
+ const callableAssignment = t.assignmentExpression("=", t.cloneNode(callableId), member);
232
+ const selected = instrumentOptionalOperand(path, t.cloneNode(callableId));
233
+ const call = t.optionalCallExpression(t.optionalMemberExpression(selected, t.identifier("call"), false, true), [t.cloneNode(objectId), ...path.node.arguments.map((argument) => t.cloneNode(argument))], false);
234
+ path.replaceWith(t.sequenceExpression([objectAssignment, callableAssignment, call]));
235
+ path.skip();
236
+ return;
237
+ }
238
+ path.node.callee = instrumentOptionalOperand(path, callee);
239
+ },
240
+ });
241
+ // Logical assignments have the same short/right split as value-selection
242
+ // expressions, but wrapping only the RHS preserves one-time LHS evaluation.
243
+ traverse(ast, {
244
+ AssignmentExpression: {
245
+ exit(path) {
246
+ const node = path.node;
247
+ if (!node.loc ||
248
+ (node.operator !== "&&=" &&
249
+ node.operator !== "||=" &&
250
+ node.operator !== "??="))
251
+ return;
252
+ const id = stableId(file, "logical-assignment", node, node.operator);
253
+ const shortId = `${id}:short`;
254
+ const rightId = `${id}:right`;
255
+ branches.push({
256
+ id,
257
+ kind: "logical-assignment",
258
+ file,
259
+ line: node.loc.start.line,
260
+ column: node.loc.start.column + 1,
261
+ source: sourceFor(code, node),
262
+ alternatives: [
263
+ { id: shortId, label: "assignment skipped" },
264
+ { id: rightId, label: "right evaluated / assigned" },
265
+ ],
266
+ });
267
+ const frameId = path.scope.generateUidIdentifier("supercovSelectionFrame");
268
+ const frameScope = path.scope.getFunctionParent() ?? path.scope.getProgramParent();
269
+ frameScope.push({ id: t.cloneNode(frameId), kind: "let" });
270
+ const assignFrame = t.assignmentExpression("=", t.cloneNode(frameId), t.callExpression(t.identifier(SELECTION_BEGIN), [
271
+ t.stringLiteral(shortId),
272
+ t.stringLiteral(rightId),
273
+ ]));
274
+ const assignment = t.assignmentExpression(node.operator, t.cloneNode(node.left), t.callExpression(t.identifier(SELECTION_RIGHT), [
275
+ t.cloneNode(frameId),
276
+ node.right,
277
+ ]));
278
+ path.replaceWith(t.sequenceExpression([
279
+ assignFrame,
280
+ t.callExpression(t.identifier(SELECTION_END), [
281
+ t.cloneNode(frameId),
282
+ assignment,
283
+ ]),
284
+ ]));
285
+ path.skip();
286
+ },
287
+ },
288
+ });
289
+ // Parameter defaults execute before a function body. A tiny per-default
290
+ // token lets the body distinguish an evaluated default from a supplied
291
+ // value without comparing values or evaluating either expression twice.
292
+ traverse(ast, {
293
+ Function(path) {
294
+ if (!t.isBlockStatement(path.node.body))
295
+ return;
296
+ const entries = [];
297
+ const visitPattern = (pattern) => {
298
+ if (t.isTSParameterProperty(pattern)) {
299
+ visitPattern(pattern.parameter);
300
+ return;
301
+ }
302
+ if (t.isAssignmentPattern(pattern)) {
303
+ if (!pattern.loc)
304
+ return;
305
+ const id = stableId(file, "default-value", pattern);
306
+ const defaultId = `${id}:default`;
307
+ const providedId = `${id}:provided`;
308
+ branches.push({
309
+ id,
310
+ kind: "default-value",
311
+ file,
312
+ line: pattern.loc.start.line,
313
+ column: pattern.loc.start.column + 1,
314
+ source: sourceFor(code, pattern),
315
+ alternatives: [
316
+ { id: defaultId, label: "default evaluated" },
317
+ { id: providedId, label: "value provided" },
318
+ ],
319
+ });
320
+ pattern.right = t.callExpression(t.identifier(DEFAULT_SELECTED), [
321
+ t.stringLiteral(defaultId),
322
+ pattern.right,
323
+ ]);
324
+ entries.push(t.expressionStatement(t.callExpression(t.identifier(DEFAULT_ENTERED), [
325
+ t.stringLiteral(defaultId),
326
+ t.stringLiteral(providedId),
327
+ ])));
328
+ visitPattern(pattern.left);
329
+ return;
330
+ }
331
+ if (t.isRestElement(pattern))
332
+ return visitPattern(pattern.argument);
333
+ if (t.isObjectPattern(pattern)) {
334
+ for (const property of pattern.properties) {
335
+ if (t.isRestElement(property))
336
+ visitPattern(property.argument);
337
+ else
338
+ visitPattern(property.value);
339
+ }
340
+ return;
341
+ }
342
+ if (t.isArrayPattern(pattern)) {
343
+ for (const element of pattern.elements)
344
+ if (element)
345
+ visitPattern(element);
346
+ }
347
+ };
348
+ for (const parameter of path.node.params)
349
+ visitPattern(parameter);
350
+ path.node.body.body.unshift(...entries);
351
+ },
352
+ });
353
+ // Destructuring declarations use the same token protocol. For loop-binding
354
+ // declarations consume the token at the start of each iteration, directly
355
+ // after JavaScript has completed the binding operation.
356
+ traverse(ast, {
357
+ VariableDeclaration(path) {
358
+ const parent = path.parentPath;
359
+ if (parent.isForStatement() && path.key === "init") {
360
+ let hasDefault = false;
361
+ t.traverseFast(path.node, (node) => {
362
+ if (t.isAssignmentPattern(node))
363
+ hasDefault = true;
364
+ });
365
+ const loc = path.node.loc;
366
+ if (hasDefault && loc) {
367
+ const node = path.node;
368
+ limitations.push({
369
+ id: stableId(file, "dynamic-code", node, "for-init-default"),
370
+ kind: "dynamic-code",
371
+ file,
372
+ line: loc.start.line,
373
+ column: loc.start.column + 1,
374
+ source: sourceFor(code, node),
375
+ reason: "destructuring defaults in a classic for initializer cannot yet be finalized without restructuring control flow",
376
+ });
377
+ }
378
+ return;
379
+ }
380
+ const entries = [];
381
+ const visitPattern = (pattern) => {
382
+ if (t.isAssignmentPattern(pattern)) {
383
+ if (!pattern.loc)
384
+ return;
385
+ const id = stableId(file, "default-value", pattern);
386
+ const defaultId = `${id}:default`;
387
+ const providedId = `${id}:provided`;
388
+ branches.push({
389
+ id,
390
+ kind: "default-value",
391
+ file,
392
+ line: pattern.loc.start.line,
393
+ column: pattern.loc.start.column + 1,
394
+ source: sourceFor(code, pattern),
395
+ alternatives: [
396
+ { id: defaultId, label: "default evaluated" },
397
+ { id: providedId, label: "value provided" },
398
+ ],
399
+ });
400
+ pattern.right = t.callExpression(t.identifier(DEFAULT_SELECTED), [
401
+ t.stringLiteral(defaultId),
402
+ pattern.right,
403
+ ]);
404
+ entries.push(t.expressionStatement(t.callExpression(t.identifier(DEFAULT_ENTERED), [
405
+ t.stringLiteral(defaultId),
406
+ t.stringLiteral(providedId),
407
+ ])));
408
+ visitPattern(pattern.left);
409
+ return;
410
+ }
411
+ if (t.isRestElement(pattern))
412
+ return visitPattern(pattern.argument);
413
+ if (t.isObjectPattern(pattern)) {
414
+ for (const property of pattern.properties)
415
+ visitPattern(t.isRestElement(property) ? property.argument : property.value);
416
+ return;
417
+ }
418
+ if (t.isArrayPattern(pattern)) {
419
+ for (const element of pattern.elements)
420
+ if (element)
421
+ visitPattern(element);
422
+ }
423
+ };
424
+ for (const declaration of path.node.declarations)
425
+ visitPattern(declaration.id);
426
+ if (entries.length === 0)
427
+ return;
428
+ if ((parent.isForOfStatement() || parent.isForInStatement()) &&
429
+ path.key === "left") {
430
+ const body = parent.node.body;
431
+ parent.node.body = t.isBlockStatement(body)
432
+ ? body
433
+ : t.blockStatement([body]);
434
+ parent.node.body.body.unshift(...entries);
435
+ return;
436
+ }
437
+ if (parent.isExportNamedDeclaration())
438
+ parent.insertAfter(entries);
439
+ else
440
+ path.insertAfter(entries);
441
+ },
442
+ });
443
+ // Try/catch and enumeration loops use frames finalized in `finally`, so
444
+ // return, break, continue, rejection, and throw paths remain observable.
445
+ traverse(ast, {
446
+ TryStatement(path) {
447
+ const node = path.node;
448
+ if (!node.loc || !node.handler)
449
+ return;
450
+ const id = stableId(file, "try-catch", node);
451
+ const successId = `${id}:success`;
452
+ const catchId = `${id}:catch`;
453
+ branches.push({
454
+ id,
455
+ kind: "try-catch",
456
+ file,
457
+ line: node.loc.start.line,
458
+ column: node.loc.start.column + 1,
459
+ source: "try / catch",
460
+ alternatives: [
461
+ { id: successId, label: "try completed without catch" },
462
+ { id: catchId, label: "catch entered" },
463
+ ],
464
+ });
465
+ const frameId = path.scope.generateUidIdentifier("supercovTryFrame");
466
+ const frameScope = path.scope.getFunctionParent() ?? path.scope.getProgramParent();
467
+ frameScope.push({ id: t.cloneNode(frameId), kind: "let" });
468
+ path.insertBefore(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(frameId), t.callExpression(t.identifier(TRY_BEGIN), [
469
+ t.stringLiteral(successId),
470
+ t.stringLiteral(catchId),
471
+ ]))));
472
+ node.handler.body.body.unshift(t.expressionStatement(t.callExpression(t.identifier(TRY_CATCH), [
473
+ t.cloneNode(frameId),
474
+ t.identifier("undefined"),
475
+ ])));
476
+ const end = t.expressionStatement(t.callExpression(t.identifier(TRY_END), [t.cloneNode(frameId)]));
477
+ if (node.finalizer)
478
+ node.finalizer.body.unshift(end);
479
+ else
480
+ node.finalizer = t.blockStatement([end]);
481
+ path.skip();
482
+ },
483
+ "ForInStatement|ForOfStatement"(path) {
484
+ const node = path.node;
485
+ if (!node.loc)
486
+ return;
487
+ const kind = t.isForOfStatement(node) ? "for-of" : "for-in";
488
+ const id = stableId(file, kind, node);
489
+ const zeroId = `${id}:zero`;
490
+ const enteredId = `${id}:entered`;
491
+ branches.push({
492
+ id,
493
+ kind,
494
+ file,
495
+ line: node.loc.start.line,
496
+ column: node.loc.start.column + 1,
497
+ source: sourceFor(code, node.right),
498
+ alternatives: [
499
+ { id: zeroId, label: "zero iterations" },
500
+ { id: enteredId, label: "one or more iterations" },
501
+ ],
502
+ });
503
+ const frameId = path.scope.generateUidIdentifier("supercovLoopFrame");
504
+ const frameScope = path.scope.getFunctionParent() ?? path.scope.getProgramParent();
505
+ frameScope.push({ id: t.cloneNode(frameId), kind: "let" });
506
+ const assignment = t.expressionStatement(t.assignmentExpression("=", t.cloneNode(frameId), t.callExpression(t.identifier(LOOP_BEGIN), [
507
+ t.stringLiteral(zeroId),
508
+ t.stringLiteral(enteredId),
509
+ ])));
510
+ const loop = t.cloneNode(node, true);
511
+ loop.body = t.isBlockStatement(loop.body)
512
+ ? loop.body
513
+ : t.blockStatement([loop.body]);
514
+ loop.body.body.unshift(t.expressionStatement(t.callExpression(t.identifier(LOOP_ENTERED), [
515
+ t.cloneNode(frameId),
516
+ ])));
517
+ let loopStatement = loop;
518
+ let replacementPath = path;
519
+ while (replacementPath.parentPath?.isLabeledStatement() &&
520
+ replacementPath.parentPath.node.body === replacementPath.node) {
521
+ loopStatement = t.labeledStatement(t.cloneNode(replacementPath.parentPath.node.label), loopStatement);
522
+ replacementPath = replacementPath.parentPath;
523
+ }
524
+ const wrapped = t.tryStatement(t.blockStatement([loopStatement]), null, t.blockStatement([
525
+ t.expressionStatement(t.callExpression(t.identifier(LOOP_END), [t.cloneNode(frameId)])),
526
+ ]));
527
+ if (replacementPath === path) {
528
+ path.insertBefore(assignment);
529
+ path.replaceWith(wrapped);
530
+ path.skip();
531
+ }
532
+ else {
533
+ replacementPath.replaceWith(t.blockStatement([assignment, wrapped]));
534
+ replacementPath.skip();
535
+ }
536
+ },
537
+ });
538
+ // Function entry is separate from statement coverage so empty functions and
539
+ // expression-bodied arrows remain visible in the completeness denominator.
540
+ traverse(ast, {
541
+ Function(path) {
542
+ const node = path.node;
543
+ if (!node.loc || !node.body)
544
+ return;
545
+ const id = stableId(file, "function", node);
546
+ points.push({
547
+ id,
548
+ kind: "function",
549
+ file,
550
+ line: node.loc.start.line,
551
+ column: node.loc.start.column + 1,
552
+ source: sourceFor(code, node),
553
+ ...(functionLabel(path) ? { label: functionLabel(path) } : {}),
554
+ });
555
+ const probe = hitStatement(id);
556
+ generatedStatements.add(probe);
557
+ if (t.isBlockStatement(node.body)) {
558
+ node.body.body.unshift(probe);
559
+ }
560
+ else {
561
+ node.body = t.blockStatement([probe, t.returnStatement(node.body)]);
562
+ }
563
+ },
564
+ });
565
+ const instrumentDecision = (path, kind) => {
566
+ if (!path.node.loc)
567
+ return;
568
+ const originalConditions = [];
569
+ collectConditions(path.node, originalConditions);
570
+ if (originalConditions.length === 0)
571
+ return;
572
+ const id = stableId(file, "decision", path.node, kind);
573
+ const meta = {
574
+ id,
575
+ file,
576
+ line: path.node.loc.start.line,
577
+ column: path.node.loc.start.column + 1,
578
+ source: sourceFor(code, path.node),
579
+ conditions: originalConditions.map((condition) => sourceFor(code, condition)),
580
+ kind,
581
+ };
582
+ decisions.push(meta);
583
+ const frameId = path.scope.generateUidIdentifier("supercovMcdcFrame");
584
+ // A loop predicate's path scope may be represented by Babel as the loop
585
+ // body's block. Declaring the frame there puts it after the predicate that
586
+ // uses it (and is especially visible in async generators). Hoist scratch
587
+ // frames to the nearest function/program scope instead.
588
+ const frameScope = path.scope.getFunctionParent() ?? path.scope.getProgramParent();
589
+ frameScope.push({ id: t.cloneNode(frameId), kind: "let" });
590
+ const instrumented = instrumentConditions(path.node, frameId, { value: 0 }, decisionLogicalNodes);
591
+ const begin = t.callExpression(t.identifier(BEGIN), [
592
+ t.stringLiteral(id),
593
+ t.valueToNode(meta),
594
+ ]);
595
+ const assignFrame = t.assignmentExpression("=", t.cloneNode(frameId), begin);
596
+ const end = t.callExpression(t.identifier(END), [
597
+ t.cloneNode(frameId),
598
+ instrumented,
599
+ ]);
600
+ path.replaceWith(t.sequenceExpression([assignFrame, end]));
601
+ path.skip();
602
+ };
603
+ traverse(ast, {
604
+ IfStatement(path) {
605
+ instrumentDecision(path.get("test"), "if");
606
+ },
607
+ ConditionalExpression(path) {
608
+ instrumentDecision(path.get("test"), "ternary");
609
+ },
610
+ WhileStatement(path) {
611
+ instrumentDecision(path.get("test"), "while");
612
+ },
613
+ DoWhileStatement(path) {
614
+ instrumentDecision(path.get("test"), "do-while");
615
+ },
616
+ ForStatement(path) {
617
+ const test = path.get("test");
618
+ if (test.node)
619
+ instrumentDecision(test, "for");
620
+ },
621
+ });
622
+ // A logical expression outside a control predicate selects a value or
623
+ // render path. Record whether it short-circuited or evaluated its RHS;
624
+ // Boolean MC/DC would be misleading when both selected values are truthy.
625
+ traverse(ast, {
626
+ LogicalExpression: {
627
+ exit(path) {
628
+ const node = path.node;
629
+ if (!node.loc || decisionLogicalNodes.has(node))
630
+ return;
631
+ const id = stableId(file, "logical-value", node, node.operator);
632
+ const shortId = `${id}:short`;
633
+ const rightId = `${id}:right`;
634
+ branches.push({
635
+ id,
636
+ kind: "logical-value",
637
+ file,
638
+ line: node.loc.start.line,
639
+ column: node.loc.start.column + 1,
640
+ source: sourceFor(code, node),
641
+ alternatives: [
642
+ { id: shortId, label: "short-circuit / left selected" },
643
+ { id: rightId, label: "right evaluated / selected" },
644
+ ],
645
+ });
646
+ const frameId = path.scope.generateUidIdentifier("supercovSelectionFrame");
647
+ const frameScope = path.scope.getFunctionParent() ?? path.scope.getProgramParent();
648
+ frameScope.push({ id: t.cloneNode(frameId), kind: "let" });
649
+ const begin = t.callExpression(t.identifier(SELECTION_BEGIN), [
650
+ t.stringLiteral(shortId),
651
+ t.stringLiteral(rightId),
652
+ ]);
653
+ const assign = t.assignmentExpression("=", t.cloneNode(frameId), begin);
654
+ const right = t.callExpression(t.identifier(SELECTION_RIGHT), [
655
+ t.cloneNode(frameId),
656
+ node.right,
657
+ ]);
658
+ const selection = t.logicalExpression(node.operator, node.left, right);
659
+ const end = t.callExpression(t.identifier(SELECTION_END), [
660
+ t.cloneNode(frameId),
661
+ selection,
662
+ ]);
663
+ path.replaceWith(t.sequenceExpression([assign, end]));
664
+ path.skip();
665
+ },
666
+ },
667
+ });
668
+ // Switch alternatives are observable independently of whether their bodies
669
+ // are empty or fall through to another case. An implicit no-match probe must
670
+ // live after the switch rather than in a synthetic default: a matched case
671
+ // can legally fall through to the end and must not be counted as no-match.
672
+ traverse(ast, {
673
+ SwitchStatement(path) {
674
+ const node = path.node;
675
+ if (!node.loc)
676
+ return;
677
+ const id = stableId(file, "switch", node);
678
+ const hasDefault = node.cases.some((switchCase) => switchCase.test === null);
679
+ const enteredId = hasDefault
680
+ ? undefined
681
+ : path.scope.generateUidIdentifier("supercovSwitchEntered");
682
+ const alternatives = node.cases.map((switchCase, index) => {
683
+ const alternativeId = `${id}:case:${index}`;
684
+ const label = switchCase.test
685
+ ? `case ${sourceFor(code, switchCase.test)}`
686
+ : "default";
687
+ const probe = hitStatement(alternativeId);
688
+ generatedStatements.add(probe);
689
+ switchCase.consequent.unshift(...(enteredId
690
+ ? [
691
+ t.expressionStatement(t.assignmentExpression("=", t.cloneNode(enteredId), t.booleanLiteral(true))),
692
+ ]
693
+ : []), probe);
694
+ return { id: alternativeId, label };
695
+ });
696
+ let noMatchId;
697
+ if (!hasDefault) {
698
+ const alternativeId = `${id}:no-match`;
699
+ noMatchId = alternativeId;
700
+ alternatives.push({
701
+ id: alternativeId,
702
+ label: "no matching case",
703
+ });
704
+ }
705
+ branches.push({
706
+ id,
707
+ kind: "switch",
708
+ file,
709
+ line: node.loc.start.line,
710
+ column: node.loc.start.column + 1,
711
+ source: sourceFor(code, node.discriminant),
712
+ alternatives,
713
+ });
714
+ if (!enteredId || !noMatchId)
715
+ return;
716
+ let switchStatement = node;
717
+ let replacementPath = path;
718
+ while (replacementPath.parentPath?.isLabeledStatement() &&
719
+ replacementPath.parentPath.node.body === replacementPath.node) {
720
+ switchStatement = t.labeledStatement(t.cloneNode(replacementPath.parentPath.node.label), switchStatement);
721
+ replacementPath = replacementPath.parentPath;
722
+ }
723
+ const noMatchProbe = hitStatement(noMatchId);
724
+ generatedStatements.add(noMatchProbe);
725
+ replacementPath.replaceWith(t.blockStatement([
726
+ t.variableDeclaration("let", [
727
+ t.variableDeclarator(t.cloneNode(enteredId), t.booleanLiteral(false)),
728
+ ]),
729
+ switchStatement,
730
+ t.ifStatement(t.unaryExpression("!", t.cloneNode(enteredId)), noMatchProbe),
731
+ ]));
732
+ replacementPath.skip();
733
+ },
734
+ });
735
+ // Runtime-generated source cannot be assigned a truthful static
736
+ // denominator without parsing and instrumenting the generated program in
737
+ // its own execution realm. Discover it and block a completeness verdict
738
+ // instead of silently claiming 100% for only the surrounding file.
739
+ traverse(ast, {
740
+ CallExpression(path) {
741
+ const callee = path.node.callee;
742
+ if (!path.node.loc || !t.isIdentifier(callee, { name: "eval" }))
743
+ return;
744
+ limitations.push({
745
+ id: stableId(file, "dynamic-code", path.node, "eval"),
746
+ kind: "dynamic-code",
747
+ file,
748
+ line: path.node.loc.start.line,
749
+ column: path.node.loc.start.column + 1,
750
+ source: sourceFor(code, path.node),
751
+ reason: "eval-generated source has no stable pre-run coverage denominator",
752
+ });
753
+ },
754
+ NewExpression(path) {
755
+ if (!path.node.loc ||
756
+ !t.isIdentifier(path.node.callee, { name: "Function" }))
757
+ return;
758
+ limitations.push({
759
+ id: stableId(file, "dynamic-code", path.node, "Function"),
760
+ kind: "dynamic-code",
761
+ file,
762
+ line: path.node.loc.start.line,
763
+ column: path.node.loc.start.column + 1,
764
+ source: sourceFor(code, path.node),
765
+ reason: "Function-generated source has no stable pre-run coverage denominator",
766
+ });
767
+ },
768
+ });
769
+ // Route requests carry the Playwright phase as a private test-only header.
770
+ // Wrap Remix request entry points so Node's AsyncLocalStorage can propagate
771
+ // that exact phase through every awaited helper without application edits.
772
+ // This runs after source instrumentation so generated wrappers never become
773
+ // coverage obligations themselves.
774
+ if (/^app\/routes\//.test(file)) {
775
+ traverse(ast, {
776
+ ExportNamedDeclaration(path) {
777
+ const declaration = path.node.declaration;
778
+ if (t.isVariableDeclaration(declaration)) {
779
+ for (const declarator of declaration.declarations) {
780
+ if (t.isIdentifier(declarator.id) &&
781
+ isRemixRequestHandlerName(declarator.id.name) &&
782
+ declarator.init) {
783
+ declarator.init = t.callExpression(t.identifier(WITH_REQUEST_PHASE), [declarator.init]);
784
+ usesRequestPhaseHandler = true;
785
+ }
786
+ }
787
+ return;
788
+ }
789
+ if (t.isFunctionDeclaration(declaration) &&
790
+ declaration.id &&
791
+ isRemixRequestHandlerName(declaration.id.name)) {
792
+ const exportedName = declaration.id.name;
793
+ const originalId = path.scope.generateUidIdentifier(`${exportedName}CoverageOriginal`);
794
+ declaration.id = originalId;
795
+ const wrappedExport = t.exportNamedDeclaration(t.variableDeclaration("const", [
796
+ t.variableDeclarator(t.identifier(exportedName), t.callExpression(t.identifier(WITH_REQUEST_PHASE), [
797
+ t.cloneNode(originalId),
798
+ ])),
799
+ ]));
800
+ path.replaceWithMultiple([declaration, wrappedExport]);
801
+ usesRequestPhaseHandler = true;
802
+ return;
803
+ }
804
+ if (!path.node.source)
805
+ return;
806
+ const handlerSpecifiers = path.node.specifiers.filter((specifier) => t.isExportSpecifier(specifier) &&
807
+ t.isIdentifier(specifier.exported) &&
808
+ isRemixRequestHandlerName(specifier.exported.name));
809
+ if (handlerSpecifiers.length === 0)
810
+ return;
811
+ const replacements = [];
812
+ const untouched = path.node.specifiers.filter((specifier) => !handlerSpecifiers.includes(specifier));
813
+ if (untouched.length > 0) {
814
+ replacements.push(t.exportNamedDeclaration(null, untouched, t.cloneNode(path.node.source)));
815
+ }
816
+ for (const specifier of handlerSpecifiers) {
817
+ const exportedName = specifier.exported.name;
818
+ const importedId = path.scope.generateUidIdentifier(`${exportedName}CoverageOriginal`);
819
+ replacements.push(t.importDeclaration([
820
+ t.importSpecifier(t.cloneNode(importedId), t.cloneNode(specifier.local)),
821
+ ], t.cloneNode(path.node.source)), t.exportNamedDeclaration(t.variableDeclaration("const", [
822
+ t.variableDeclarator(t.identifier(exportedName), t.callExpression(t.identifier(WITH_REQUEST_PHASE), [
823
+ t.cloneNode(importedId),
824
+ ])),
825
+ ])));
826
+ }
827
+ path.replaceWithMultiple(replacements);
828
+ usesRequestPhaseHandler = true;
829
+ },
830
+ });
831
+ }
832
+ if (/^app\/entry\.server\.[cm]?[jt]sx?$/.test(file)) {
833
+ traverse(ast, {
834
+ ExportDefaultDeclaration(path) {
835
+ const declaration = path.node.declaration;
836
+ if (t.isFunctionDeclaration(declaration)) {
837
+ const originalId = path.scope.generateUidIdentifier("handleRequestCoverageOriginal");
838
+ declaration.id = originalId;
839
+ path.replaceWithMultiple([
840
+ declaration,
841
+ t.exportDefaultDeclaration(t.callExpression(t.identifier(WITH_REQUEST_PHASE), [
842
+ t.cloneNode(originalId),
843
+ ])),
844
+ ]);
845
+ }
846
+ else {
847
+ path.node.declaration = t.callExpression(t.identifier(WITH_REQUEST_PHASE), [declaration]);
848
+ }
849
+ usesRequestPhaseHandler = true;
850
+ },
851
+ });
852
+ }
853
+ // HTTP servers and WebSocket libraries expose request-bearing callbacks
854
+ // outside framework route exports. Wrap well-known listener boundaries and
855
+ // let the runtime scan callback arguments for Fetch or Node request headers.
856
+ traverse(ast, {
857
+ CallExpression(path) {
858
+ const callee = path.node.callee;
859
+ const property = (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) &&
860
+ !callee.computed &&
861
+ t.isIdentifier(callee.property)
862
+ ? callee.property.name
863
+ : undefined;
864
+ const identifier = t.isIdentifier(callee) ? callee.name : property;
865
+ let callbackIndex = -1;
866
+ if ((property === "on" || property === "once" || property === "addListener") &&
867
+ t.isStringLiteral(path.node.arguments[0]) &&
868
+ ["request", "upgrade", "connection"].includes(path.node.arguments[0].value)) {
869
+ callbackIndex = 1;
870
+ }
871
+ else if (identifier === "createServer") {
872
+ for (let index = path.node.arguments.length - 1; index >= 0; index -= 1) {
873
+ const argument = path.node.arguments[index];
874
+ if (t.isFunctionExpression(argument) ||
875
+ t.isArrowFunctionExpression(argument) ||
876
+ t.isIdentifier(argument) ||
877
+ t.isMemberExpression(argument)) {
878
+ callbackIndex = index;
879
+ break;
880
+ }
881
+ }
882
+ }
883
+ if (callbackIndex < 0)
884
+ return;
885
+ const callback = path.node.arguments[callbackIndex];
886
+ if (!callback || !t.isExpression(callback))
887
+ return;
888
+ if (t.isCallExpression(callback) &&
889
+ t.isIdentifier(callback.callee, { name: WITH_REQUEST_PHASE }))
890
+ return;
891
+ path.node.arguments[callbackIndex] = t.callExpression(t.identifier(WITH_REQUEST_PHASE), [callback]);
892
+ usesRequestPhaseHandler = true;
893
+ },
894
+ });
895
+ const manifest = {
896
+ decisions,
897
+ points,
898
+ branches,
899
+ ...(limitations.length > 0 ? { limitations } : {}),
900
+ };
901
+ if (decisions.length > 0 ||
902
+ points.length > 0 ||
903
+ branches.length > 0 ||
904
+ usesRequestPhaseHandler) {
905
+ ast.program.body.unshift(t.importDeclaration([
906
+ t.importSpecifier(t.identifier(BEGIN), t.identifier("mcdcBegin")),
907
+ t.importSpecifier(t.identifier(CONDITION), t.identifier("mcdcCondition")),
908
+ t.importSpecifier(t.identifier(END), t.identifier("mcdcEnd")),
909
+ t.importSpecifier(t.identifier(HIT), t.identifier("coverageHit")),
910
+ t.importSpecifier(t.identifier(SELECTION_BEGIN), t.identifier("selectionBegin")),
911
+ t.importSpecifier(t.identifier(SELECTION_RIGHT), t.identifier("selectionRight")),
912
+ t.importSpecifier(t.identifier(SELECTION_END), t.identifier("selectionEnd")),
913
+ t.importSpecifier(t.identifier(OPTIONAL_SELECT), t.identifier("optionalSelect")),
914
+ t.importSpecifier(t.identifier(DEFAULT_SELECTED), t.identifier("defaultSelected")),
915
+ t.importSpecifier(t.identifier(DEFAULT_ENTERED), t.identifier("defaultEntered")),
916
+ t.importSpecifier(t.identifier(TRY_BEGIN), t.identifier("tryBegin")),
917
+ t.importSpecifier(t.identifier(TRY_CATCH), t.identifier("tryCatch")),
918
+ t.importSpecifier(t.identifier(TRY_END), t.identifier("tryEnd")),
919
+ t.importSpecifier(t.identifier(LOOP_BEGIN), t.identifier("loopBegin")),
920
+ t.importSpecifier(t.identifier(LOOP_ENTERED), t.identifier("loopEntered")),
921
+ t.importSpecifier(t.identifier(LOOP_END), t.identifier("loopEnd")),
922
+ ...(usesRequestPhaseHandler
923
+ ? [
924
+ t.importSpecifier(t.identifier(WITH_REQUEST_PHASE), t.identifier("withRequestPhase")),
925
+ ]
926
+ : []),
927
+ ], t.stringLiteral(RUNTIME_MODULE)));
928
+ }
929
+ const output = generate({
930
+ ...ast,
931
+ }, {
932
+ sourceMaps: true,
933
+ sourceFileName: file,
934
+ retainLines: true,
935
+ comments: true,
936
+ }, code);
937
+ return { code: output.code, map: output.map, manifest, decisions };
938
+ }
939
+ export const mcdcRuntimeModuleId = RUNTIME_MODULE;
940
+ //# sourceMappingURL=instrumenter.js.map