All files / istanbul-lib-instrument/src source-coverage.ts

95.83% Statements 46/48
93.1% Branches 27/29
100% Functions 11/11
95.83% Lines 46/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                      1516x                                                   236x 236x                   808x 808x 808x 808x 808x               58x 58x 58x             58x 58x 58x       204x 204x 204x             204x 204x 204x       204x 194x   10x     10x 204x       388x 388x     388x     388x 388x 388x 388x       388x 366x   22x 22x     22x                 11x         234x 234x 234x 234x 204x 1x 1x 1x              
import type { EncodedSourceMap } from "@jridgewell/trace-mapping";
import { classes } from "@vitest/istanbul-lib-coverage";
import type { FileCoverageData, FunctionMapping, Range } from "@vitest/istanbul-lib-coverage";
 
/**
 * an input source map, mapping the code being instrumented back to its
 * original source.
 */
export type InputSourceMap = EncodedSourceMap;
 
/**
 * coverage data produced by the instrumenter for a single file: raw
 * file coverage data plus the input source map, when one was provided.
 */
export interface SourceCoverageData extends FileCoverageData {
  inputSourceMap?: InputSourceMap;
}
 
function cloneLocation(loc?: Range | null): Range {
  return {
    start: {
      line: loc && loc.start.line,
      column: loc && loc.start.column,
    },
    end: {
      line: loc && loc.end.line,
      column: loc && loc.end.column,
    },
  } as Range;
}
/**
 * SourceCoverage provides mutation methods to manipulate the structure of
 * a file coverage object. Used by the instrumenter to create a full coverage
 * object for a file incrementally.
 *
 * @private
 * @param pathOrObj {String|Object} - see the argument for {@link FileCoverage}
 * @extends FileCoverage
 * @constructor
 */
class SourceCoverage extends classes.FileCoverage {
  declare data: SourceCoverageData;
  meta: { last: { s: number; f: number; b: number } };
 
  constructor(pathOrObj: string | FileCoverageData) {
    super(pathOrObj);
    this.meta = {
      last: {
        s: 0,
        f: 0,
        b: 0,
      },
    };
  }
 
  newStatement(loc: Range): number {
    const s = this.meta.last.s;
    this.data.statementMap[s] = cloneLocation(loc);
    this.data.s[s] = 0;
    this.meta.last.s += 1;
    return s;
  }
 
  newFunction(
    name: string | null | undefined,
    decl: Range | null | undefined,
    loc: Range | null | undefined,
  ): number {
    const f = this.meta.last.f;
    name = name || "(anonymous_" + f + ")";
    this.data.fnMap[f] = {
      name,
      decl: cloneLocation(decl),
      loc: cloneLocation(loc),
      // DEPRECATED: some legacy reports require this info.
      line: loc && loc.start.line,
    } as FunctionMapping;
    this.data.f[f] = 0;
    this.meta.last.f += 1;
    return f;
  }
 
  newBranch(type: string, loc: Range | null | undefined, isReportLogic = false): number {
    const b = this.meta.last.b;
    this.data.b[b] = [];
    this.data.branchMap[b] = {
      loc: cloneLocation(loc),
      type,
      locations: [],
      // DEPRECATED: some legacy reports require this info.
      line: (loc && loc.start.line) as number,
    };
    this.meta.last.b += 1;
    this.maybeNewBranchTrue(type, b, isReportLogic);
    return b;
  }
 
  maybeNewBranchTrue(type: string, name: number, isReportLogic: boolean): void {
    if (!isReportLogic) {
      return;
    }
    Iif (type !== "binary-expr") {
      return;
    }
    this.data.bT = this.data.bT || {};
    this.data.bT[name] = [];
  }
 
  addBranchPath(name: number, location: Range | null | undefined): number {
    const bMeta = this.data.branchMap[name];
    const counts = this.data.b[name];
 
    /* istanbul ignore if: paranoid check */
    if (!bMeta) {
      throw new Error("Invalid branch " + name);
    }
    bMeta.locations.push(cloneLocation(location));
    counts.push(0);
    this.maybeAddBranchTrue(name);
    return counts.length - 1;
  }
 
  maybeAddBranchTrue(name: number): void {
    if (!this.data.bT) {
      return;
    }
    const countsTrue = this.data.bT[name];
    Iif (!countsTrue) {
      return;
    }
    countsTrue.push(0);
  }
 
  /**
   * Assigns an input source map to the coverage that can be used
   * to remap the coverage output to the original source
   * @param sourceMap {object} the source map
   */
  inputSourceMap(sourceMap: InputSourceMap): void {
    this.data.inputSourceMap = sourceMap;
  }
 
  freeze(): void {
    // prune empty branches
    const map = this.data.branchMap;
    const branches = this.data.b;
    const branchesT = this.data.bT || {};
    Object.keys(map).forEach((b) => {
      if (map[b].locations.length === 0) {
        delete map[b];
        delete branches[b];
        delete branchesT[b];
      }
    });
  }
}
 
export { SourceCoverage };