Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/istanbul-lib-instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export { createInstrumenter, readInitialCoverage, defaultOpts };
export type { Instrumenter };
export type { InstrumenterCallback, InstrumenterOptions, SourceMap } from "./instrumenter";
export type { InitialCoverage } from "./read-coverage";
export type { InputSourceMap, SourceCoverageData } from "./source-coverage";
export type { DecisionMapping, InputSourceMap, SourceCoverageData } from "./source-coverage";
3 changes: 3 additions & 0 deletions packages/istanbul-lib-instrument/src/instrumenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface InstrumenterOptions {
coverageVariable: string;
/** report boolean value of logical expressions (default `false`) */
reportLogic: boolean;
/** track modified condition/decision coverage (MC/DC) of logical expressions (default `false`) */
mcdc: boolean;
/**
* @deprecated has no effect, comments of the original code are always preserved
*/
Expand Down Expand Up @@ -117,6 +119,7 @@ class Instrumenter {
const output = instrumentProgram(s, program, comments, filename, {
coverageVariable: opts.coverageVariable,
reportLogic: opts.reportLogic,
mcdc: opts.mcdc,
coverageGlobalScope: opts.coverageGlobalScope,
coverageGlobalScopeFunc: opts.coverageGlobalScopeFunc,
ignoreClassMethods: opts.ignoreClassMethods,
Expand Down
41 changes: 39 additions & 2 deletions packages/istanbul-lib-instrument/src/source-coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ export type InputSourceMap = EncodedSourceMap;
*/
export interface SourceCoverageData extends FileCoverageData {
inputSourceMap?: InputSourceMap;
/** MC/DC decisions; only present when `mcdc` is enabled */
decisionMap?: Record<string, DecisionMapping>;
/** hit counts of the MC/DC test vectors of each decision; only present when `mcdc` is enabled */
d?: Record<string, number[]>;
}

/** metadata for a single MC/DC decision in the instrumented source */
export interface DecisionMapping {
/** location of the whole decision */
loc: Range;
/** locations of the conditions of the decision, in evaluation order */
conditions: Range[];
/**
* the test vectors the decision can evaluate, indexed like the hit counts.
* One character per condition: `T` (truthy), `F` (falsy) or `-` (not evaluated).
* The outcome of the decision is the value of the last evaluated condition.
*/
vectors: string[];
}

function cloneLocation(loc?: Range | null): Range {
Expand All @@ -40,17 +58,22 @@ function cloneLocation(loc?: Range | null): Range {
*/
class SourceCoverage extends classes.FileCoverage {
declare data: SourceCoverageData;
meta: { last: { s: number; f: number; b: number } };
meta: { last: { s: number; f: number; b: number; d: number } };

constructor(pathOrObj: string | FileCoverageData) {
constructor(pathOrObj: string | FileCoverageData, mcdc = false) {
super(pathOrObj);
this.meta = {
last: {
s: 0,
f: 0,
b: 0,
d: 0,
},
};
if (mcdc) {
this.data.decisionMap = {};
this.data.d = {};
}
}

newStatement(loc: Range): number {
Expand Down Expand Up @@ -131,6 +154,20 @@ class SourceCoverage extends classes.FileCoverage {
countsTrue.push(0);
}

newDecision(loc: Range, conditions: Range[], vectors: string[]): number {
const d = this.meta.last.d;

this.data.decisionMap![d] = {
loc: cloneLocation(loc),
conditions: conditions.map(cloneLocation),
vectors,
};
this.data.d![d] = vectors.map(() => 0);
this.meta.last.d += 1;

return d;
}

/**
* Assigns an input source map to the coverage that can be used
* to remap the coverage output to the original source
Expand Down
Loading
Loading