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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | 10x 10x 10x 10x 10x 10x 10x 16x 14x 14x 6x 6x 6x 7x 7x 6x 6x 6x 6x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 6x 6x 6x 6x 4x 3x 3x 3x 1x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 4x 2x 1x 2x 2x 1x 1x 19x 19x 19x 19x 19x 14x 14x 19x 19x 19x 19x 19x 19x 19x 19x 19x 13x 13x 13x 143x 143x 13x | /*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import type { FileCoverage, FunctionMapping, Range } from "@vitest/istanbul-lib-coverage";
import InsertionText from "./insertion-text";
const lt = "\u0001";
const gt = "\u0002";
const RE_LT = /</g;
const RE_GT = />/g;
const RE_AMP = /&/g;
// eslint-disable-next-line
var RE_lt = /\u0001/g;
// eslint-disable-next-line
var RE_gt = /\u0002/g;
/** allows the optional `skip` marker some instrumenters place on metadata */
type MaybeSkipped<T> = T & { skip?: boolean };
/** a single line of source text being annotated */
interface StructuredTextItem {
line: number;
covered: string | null;
hits: number;
text: InsertionText;
}
/** an object that can look up source code for a file path */
export interface SourceStore {
getSource(filePath: string): string;
}
/** per-line coverage info as rendered in the detail view */
export interface LineCoverageInfo {
covered: string | null;
hits: number | string;
}
/** the annotated source data consumed by the html report's detail template */
export interface AnnotatedData {
annotatedCode: string[];
lineCoverage: LineCoverageInfo[];
maxLines: number;
}
function title(str: string): string {
return ' title="' + str + '" ';
}
function customEscape(text: string): string {
text = String(text);
return text
.replace(RE_AMP, "&")
.replace(RE_LT, "<")
.replace(RE_GT, ">")
.replace(RE_lt, "<")
.replace(RE_gt, ">");
}
function annotateLines(fileCoverage: FileCoverage, structuredText: StructuredTextItem[]): void {
const lineStats = fileCoverage.getLineCoverage();
Iif (!lineStats) {
return;
}
Object.entries(lineStats).forEach(([lineNumber, count]) => {
const item = structuredText[lineNumber as unknown as number];
if (item) {
item.covered = count > 0 ? "yes" : "no";
item.hits = count;
}
});
}
function annotateStatements(
fileCoverage: FileCoverage,
structuredText: StructuredTextItem[],
): void {
const statementStats = fileCoverage.s;
const statementMeta = fileCoverage.statementMap;
Object.entries(statementStats).forEach(([stName, count]) => {
const meta = statementMeta[stName] as MaybeSkipped<Range>;
const type = count > 0 ? "yes" : "no";
const startCol = meta.start.column;
let endCol = meta.end.column + 1;
const startLine = meta.start.line;
const endLine = meta.end.line;
const openSpan =
lt +
'span class="' +
(meta.skip ? "cstat-skip" : "cstat-no") +
'"' +
title("statement not covered") +
gt;
const closeSpan = lt + "/span" + gt;
let text;
if (type === "no" && structuredText[startLine]) {
Iif (endLine !== startLine) {
endCol = structuredText[startLine].text.originalLength();
}
text = structuredText[startLine].text;
text.wrap(startCol, openSpan, startCol < endCol ? endCol : text.originalLength(), closeSpan);
}
});
}
function annotateFunctions(fileCoverage: FileCoverage, structuredText: StructuredTextItem[]): void {
const fnStats = fileCoverage.f;
const fnMeta = fileCoverage.fnMap;
Iif (!fnStats) {
return;
}
Object.entries(fnStats).forEach(([fName, count]) => {
const meta = fnMeta[fName] as MaybeSkipped<FunctionMapping>;
const type = count > 0 ? "yes" : "no";
// Some versions of the instrumenter in the wild populate 'func'
// but not 'decl':
const decl = meta.decl || meta.loc;
const startCol = decl.start.column;
let endCol = decl.end.column + 1;
const startLine = decl.start.line;
const endLine = decl.end.line;
const openSpan =
lt +
'span class="' +
(meta.skip ? "fstat-skip" : "fstat-no") +
'"' +
title("function not covered") +
gt;
const closeSpan = lt + "/span" + gt;
let text;
if (type === "no" && structuredText[startLine]) {
Iif (endLine !== startLine) {
endCol = structuredText[startLine].text.originalLength();
}
text = structuredText[startLine].text;
text.wrap(startCol, openSpan, startCol < endCol ? endCol : text.originalLength(), closeSpan);
}
});
}
function annotateBranches(fileCoverage: FileCoverage, structuredText: StructuredTextItem[]): void {
const branchStats = fileCoverage.b;
const branchMeta = fileCoverage.branchMap;
Iif (!branchStats) {
return;
}
Object.entries(branchStats).forEach(([branchName, branchArray]) => {
const sumCount = branchArray.reduce((p, n) => p + n, 0);
const metaArray = branchMeta[branchName].locations;
let i: number;
let count: number;
let meta: MaybeSkipped<Range>;
let startCol: number;
let endCol: number;
let startLine: number | undefined;
let endLine: number | undefined;
let openSpan: string;
let closeSpan: string;
let text: InsertionText;
// only highlight if partial branches are missing or if there is a
// single uncovered branch.
Eif (sumCount > 0 || (sumCount === 0 && branchArray.length === 1)) {
// Need to recover the metaArray placeholder item to count an implicit else
if (
// Check if the branch is a conditional if branch.
branchMeta[branchName].type === "if" &&
// Check if the branch has an implicit else.
branchArray.length === 2 &&
// Check if the implicit else branch is unaccounted for.
metaArray.length === 1 &&
// Check if the implicit else branch is uncovered.
branchArray[1] === 0
) {
metaArray[1] = {
start: {},
end: {},
} as Range;
}
for (i = 0; i < branchArray.length && i < metaArray.length; i += 1) {
count = branchArray[i];
meta = metaArray[i];
startCol = meta.start.column;
endCol = meta.end.column + 1;
startLine = meta.start.line;
endLine = meta.end.line;
openSpan =
lt +
'span class="branch-' +
i +
" " +
(meta.skip ? "cbranch-skip" : "cbranch-no") +
'"' +
title("branch not covered") +
gt;
closeSpan = lt + "/span" + gt;
// If the branch is an implicit else from an if statement,
// then the coverage report won't show a statistic.
// Therefore, the previous branch will be used to report that
// there is no coverage on that implicit branch.
if (count === 0 && startLine === undefined && branchMeta[branchName].type === "if") {
const prevMeta = metaArray[i - 1];
startCol = prevMeta.start.column;
endCol = prevMeta.end.column + 1;
startLine = prevMeta.start.line;
endLine = prevMeta.end.line;
}
if (count === 0 && structuredText[startLine as number]) {
//skip branches taken
if (endLine !== startLine) {
endCol = structuredText[startLine as number].text.originalLength();
}
text = structuredText[startLine as number].text;
if (branchMeta[branchName].type === "if") {
// 'if' is a special case
// since the else branch might not be visible, being nonexistent
text.insertAt(
startCol,
lt +
'span class="' +
(meta.skip ? "skip-if-branch" : "missing-if-branch") +
'"' +
title((i === 0 ? "if" : "else") + " path not taken") +
gt +
(i === 0 ? "I" : "E") +
lt +
"/span" +
gt,
true,
false,
);
} else {
text.wrap(
startCol,
openSpan,
startCol < endCol ? endCol : text.originalLength(),
closeSpan,
);
}
}
}
}
});
}
function annotateSourceCode(fileCoverage: FileCoverage, sourceStore: SourceStore): AnnotatedData {
let codeArray: string[];
let lineCoverageArray: LineCoverageInfo[];
try {
const sourceText = sourceStore.getSource(fileCoverage.path);
const code = sourceText.split(/(?:\r?\n)|\r/);
let count = 0;
const structured: StructuredTextItem[] = code.map((str) => {
count += 1;
return {
line: count,
covered: "neutral",
hits: 0,
text: new InsertionText(str, true),
};
});
structured.unshift({
line: 0,
covered: null,
text: new InsertionText(""),
} as StructuredTextItem);
annotateLines(fileCoverage, structured);
//note: order is important, since statements typically result in spanning the whole line and doing branches late
//causes mismatched tags
annotateBranches(fileCoverage, structured);
annotateFunctions(fileCoverage, structured);
annotateStatements(fileCoverage, structured);
structured.shift();
codeArray = structured.map((item) => customEscape(item.text.toString()) || " ");
lineCoverageArray = structured.map((item) => ({
covered: item.covered,
hits: item.hits > 0 ? item.hits + "x" : " ",
}));
return {
annotatedCode: codeArray,
lineCoverage: lineCoverageArray,
maxLines: structured.length,
};
} catch (ex) {
codeArray = [(ex as Error).message];
lineCoverageArray = [{ covered: "no", hits: 0 }];
String((ex as Error).stack || "")
.split(/\r?\n/)
.forEach((line) => {
codeArray.push(line);
lineCoverageArray.push({ covered: "no", hits: 0 });
});
return {
annotatedCode: codeArray,
lineCoverage: lineCoverageArray,
maxLines: codeArray.length,
};
}
}
export default annotateSourceCode;
|