All files coverage-map.ts

100% Statements 60/60
85.71% Branches 24/28
100% Functions 8/8
100% Lines 60/60

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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                    916x 916x   916x 916x   916x 916x 4x   912x       916x               916x                           916x                     5810x 5810x   5810x 5810x   5810x 5810x 5810x 5810x   5810x   5810x 4403x 4403x 4403x   4403x 16x 16x     4403x   4403x 170x       5810x               5810x 5810x                 41149x 41149x   41149x 41149x   41149x 41121x 41121x 41121x 41121x     41149x                     20217x 20217x   20217x 20217x   20217x 20217x 20217x 20217x   20217x       43781x         20217x 20217x     20217x 43785x 43781x           116739x             90740x    
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>;
  fnNames: Map<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: {},
      fnNames: new Map(),
    };
 
    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];
 
  Eif (index == null) {
    index = meta.lastFunction;
    meta.lastFunction++;
    meta.seen[key] = index;
 
    let name = options.name;
 
    if (name) {
      const base = name;
      let count = (meta.fnNames.get(base) || 0) + 1;
      name = count > 1 ? `${base}_${count}` : base;
 
      while (meta.fnNames.has(name)) {
        count++;
        name = `${base}_${count}`;
      }
 
      meta.fnNames.set(base, count);
 
      if (name !== base) {
        meta.fnNames.set(name, 0);
      }
    }
 
    fileCoverage.fnMap[index] = {
      name: 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) => {
    if (fileCoverage.b[index][i] !== undefined) {
      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}`;
}