All files coverage-map.ts

100% Statements 48/48
83.33% Branches 15/18
100% Functions 8/8
100% Lines 48/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 160 161 162 163 164                                                  908x 908x   908x 908x   908x 908x 4x   904x       908x             908x                           908x                     5750x 5750x   5750x 5750x   5750x 5742x 5742x 5742x   5742x               5750x 5750x                 41061x 41061x   41061x 41061x   41061x 41045x 41045x 41045x 41045x     41061x                     20209x 20209x   20209x 20209x   20209x 20209x 20209x 20209x   20209x       43769x         20209x 20209x     20209x 43769x         116507x             90580x    
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { type Needle, type TraceMap } from "@jridgewell/trace-mapping";
import {
  type CoverageMapData as IstanbulCoverageMapData,
  type FileCoverageData,
} from "istanbul-lib-coverage";
 
// https://github.com/istanbuljs/istanbuljs/blob/main/docs/raw-output.md#branch-types
export type Branch = "if" | "binary-expr" | "cond-expr" | "switch" | "default-arg";
 
export type CoverageMapData = IstanbulCoverageMapData & {
  [key: string]: FileCoverageData;
};
 
type FileCoverageDataWithMeta = FileCoverageData & { meta: Meta };
 
type Meta = {
  lastFunction: number;
  lastBranch: number;
  lastStatement: number;
  seen: Record<string, number>;
};
 
export function createCoverageMapData(filename: string, sourceMap: TraceMap) {
  const data: CoverageMapData = {};
  const directory = dirname(filename);
 
  for (const source of sourceMap.sources) {
    let path = filename;
 
    Eif (source) {
      if (source.startsWith("file://")) {
        path = fileURLToPath(source);
      } else {
        path = resolve(directory, source);
      }
    }
 
    const meta: Meta = {
      lastBranch: 0,
      lastFunction: 0,
      lastStatement: 0,
      seen: {},
    };
 
    data[path] = {
      path,
      statementMap: {},
      fnMap: {},
      branchMap: {},
      s: {},
      f: {},
      b: {},
 
      // @ts-expect-error -- internal
      meta,
    };
  }
 
  return data;
}
 
export function addFunction(options: {
  coverageMapData: CoverageMapData;
  filename: string;
  name?: string;
  covered?: number;
  decl: { start: Needle; end: Needle };
  loc: { start: Needle; end: Needle };
}) {
  const fileCoverage = options.coverageMapData[options.filename];
  const meta = (fileCoverage as FileCoverageDataWithMeta).meta;
 
  const key = `f:${cacheKey(options.decl)}`;
  let index = meta.seen[key];
 
  if (index == null) {
    index = meta.lastFunction;
    meta.lastFunction++;
    meta.seen[key] = index;
 
    fileCoverage.fnMap[index] = {
      name: options.name || `(anonymous_${index})`,
      decl: pickLocation(options.decl),
      loc: pickLocation(options.loc),
      line: options.loc.start.line,
    };
  }
 
  fileCoverage.f[index] ||= 0;
  fileCoverage.f[index] += options.covered || 0;
}
 
export function addStatement(options: {
  coverageMapData: CoverageMapData;
  filename: string;
  covered?: number;
  loc: { start: Needle; end: Needle };
}) {
  const fileCoverage = options.coverageMapData[options.filename];
  const meta = (fileCoverage as FileCoverageDataWithMeta).meta;
 
  const key = `s:${cacheKey(options.loc)}`;
  let index = meta.seen[key];
 
  if (index == null) {
    index = meta.lastStatement;
    meta.lastStatement++;
    meta.seen[key] = index;
    fileCoverage.statementMap[index] = pickLocation(options.loc);
  }
 
  fileCoverage.s[index] = options.covered || 0;
}
 
export function addBranch(options: {
  coverageMapData: CoverageMapData;
  filename: string;
  type: Branch;
  loc: { start: Needle; end: Needle };
  locations: { start: Partial<Needle>; end: Partial<Needle> }[];
  covered?: number[];
}) {
  const fileCoverage = options.coverageMapData[options.filename];
  const meta = (fileCoverage as FileCoverageDataWithMeta).meta;
 
  const key = ["b", ...options.locations.map(cacheKey)].join(":");
  let index = meta.seen[key];
 
  Eif (index == null) {
    index = meta.lastBranch;
    meta.lastBranch++;
    meta.seen[key] = index;
 
    fileCoverage.branchMap[index] = {
      loc: pickLocation(options.loc),
      type: options.type,
      // @ts-expect-error -- Istanbul cheats types for implicit else
      locations: options.locations.map((loc) => pickLocation(loc)),
      line: options.loc.start.line,
    };
  }
 
  Eif (!fileCoverage.b[index]) {
    fileCoverage.b[index] = Array(options.locations.length).fill(0);
  }
 
  options.covered?.forEach((hit, i) => {
    fileCoverage.b[index][i] += hit;
  });
}
 
function pickLocation(loc: { start: Needle; end: Needle }) {
  return {
    start: { line: loc.start.line, column: loc.start.column },
    end: { line: loc.end.line, column: loc.end.column },
  };
}
 
function cacheKey(loc: { start: Partial<Needle>; end: Partial<Needle> }) {
  return `${loc.start.line}:${loc.start.column}:${loc.end.line}:${loc.end.column}`;
}