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 | 270x 270x 270x 270x 270x 32x 32x 60x 32x 193x 193x 405x 1x 404x 284x 119x 119x 119x 119x 86x 86x 37x 86x 33x 316x 701x 1640x 141x 248x 248x 105x 143x 76x 67x 67x 67x 80x 8x 72x 72x 67x 3x 143x 143x 77x 77x 269x 67x 77x 129x 129x 132x 132x 132x 140x 133x 133x 98x 98x 98x 133x 51x 51x 82x 82x 51x 51x 30x 15x 15x 51x 66x 66x 1x 1x 65x 65x 27x 12x 9x 4x 5x 82x 82x 140x 140x 82x 39x 74x 9x 9x 8x 22x 9x 51x 51x 30x 21x 53x 51x 53x 18x 18x 18x 36x 36x 18x 18x 15x 3x 19x 18x 19x | /*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import type { CoverageMap, CoverageSummary, FileCoverage } from "@vitest/istanbul-lib-coverage";
import { createCoverageSummary } from "@vitest/istanbul-lib-coverage";
import Path from "./path";
import { BaseNode, BaseTree } from "./tree";
/** names of the summarizer strategies supported by {@link SummarizerFactory} */
export type Summarizers = "flat" | "nested" | "pkg" | "defaultSummarizer";
class ReportNode extends BaseNode {
declare path: Path;
declare parent: ReportNode | null;
declare fileCoverage: FileCoverage | undefined;
declare children: ReportNode[];
declare c_files?: CoverageSummary | null;
declare c_full?: CoverageSummary | null;
constructor(path: Path, fileCoverage?: FileCoverage) {
super();
this.path = path;
this.parent = null;
this.fileCoverage = fileCoverage;
this.children = [];
}
static createRoot(children: ReportNode[]): ReportNode {
const root = new ReportNode(new Path([]));
children.forEach((child) => {
root.addChild(child);
});
return root;
}
addChild(child: ReportNode): void {
child.parent = this;
this.children.push(child);
}
asRelative(p: string): string {
if (p.substring(0, 1) === "/") {
return p.substring(1);
}
return p;
}
getQualifiedName(): string {
return this.asRelative(this.path.toString());
}
getRelativeName(): string {
const parent = this.getParent();
const myPath = this.path;
let relPath;
let i;
const parentPath = parent ? parent.path : new Path([]);
if (parentPath.ancestorOf(myPath)) {
relPath = new Path(myPath.elements());
for (i = 0; i < parentPath.length; i += 1) {
relPath.shift();
}
return this.asRelative(relPath.toString());
}
return this.asRelative(this.path.toString());
}
getParent(): ReportNode | null {
return this.parent;
}
getChildren(): ReportNode[] {
return this.children;
}
isSummary(): boolean {
return !this.fileCoverage;
}
getFileCoverage(): FileCoverage {
return this.fileCoverage as FileCoverage;
}
getCoverageSummary(filesOnly?: boolean): CoverageSummary | null {
const cacheProp = `c_${filesOnly ? "files" : "full"}` as "c_files" | "c_full";
let summary: CoverageSummary | null;
if (Object.prototype.hasOwnProperty.call(this, cacheProp)) {
return this[cacheProp] as CoverageSummary | null;
}
if (!this.isSummary()) {
summary = this.getFileCoverage().toSummary();
} else {
let count = 0;
summary = createCoverageSummary();
this.getChildren().forEach((child) => {
if (filesOnly && child.isSummary()) {
return;
}
count += 1;
summary!.merge(child.getCoverageSummary(filesOnly)!);
});
if (count === 0 && filesOnly) {
summary = null;
}
}
this[cacheProp] = summary;
return summary;
}
}
class ReportTree extends BaseTree<ReportNode> {
constructor(root: ReportNode, childPrefix?: string) {
super(root);
const maybePrefix = (node: ReportNode) => {
if (childPrefix && !node.isRoot()) {
node.path.unshift(childPrefix);
}
};
this.visit({
onDetail: maybePrefix,
onSummary(node) {
maybePrefix(node);
node.children.sort((a, b) => {
const astr = a.path.toString();
const bstr = b.path.toString();
return astr < bstr ? -1 : astr > bstr ? 1 : /* istanbul ignore next */ 0;
});
},
});
}
}
interface InitialListNode {
filePath: string;
path: Path;
fileCoverage: FileCoverage;
}
function findCommonParent(paths: Path[]): Path {
return paths.reduce((common, path) => common.commonPrefixPath(path), paths[0] || new Path([]));
}
function findOrCreateParent(
parentPath: Path,
nodeMap: Record<string, ReportNode>,
created: (parentPath: Path, parent: ReportNode) => void = () => {},
): ReportNode {
let parent = nodeMap[parentPath.toString()];
if (!parent) {
parent = new ReportNode(parentPath);
nodeMap[parentPath.toString()] = parent;
created(parentPath, parent);
}
return parent;
}
function toDirParents(list: InitialListNode[]): ReportNode[] {
const nodeMap: Record<string, ReportNode> = Object.create(null);
list.forEach((o) => {
const parent = findOrCreateParent(o.path.parent(), nodeMap);
parent.addChild(new ReportNode(o.path, o.fileCoverage));
});
return Object.values(nodeMap);
}
function addAllPaths(
topPaths: ReportNode[],
nodeMap: Record<string, ReportNode>,
path: Path,
node: ReportNode,
): void {
const parent = findOrCreateParent(path.parent(), nodeMap, (parentPath, parent) => {
if (parentPath.hasParent()) {
addAllPaths(topPaths, nodeMap, parentPath, parent);
} else {
topPaths.push(parent);
}
});
parent.addChild(node);
}
function foldIntoOneDir(node: ReportNode, parent?: ReportNode): ReportNode {
const { children } = node;
if (children.length === 1 && !children[0].fileCoverage) {
children[0].parent = parent as ReportNode;
return foldIntoOneDir(children[0], parent);
}
node.children = children.map((child) => foldIntoOneDir(child, node));
return node;
}
function pkgSummaryPrefix(dirParents: ReportNode[], commonParent: Path): string | undefined {
if (!dirParents.some((dp) => dp.path.length === 0)) {
return;
}
if (commonParent.length === 0) {
return "root";
}
return commonParent.name();
}
class SummarizerFactory {
declare private _coverageMap: CoverageMap;
declare private _defaultSummarizer: Summarizers;
declare private _initialList: InitialListNode[];
declare private _commonParent: Path;
declare private _flat: ReportTree | undefined;
declare private _pkg: ReportTree | undefined;
declare private _nested: ReportTree | undefined;
constructor(coverageMap: CoverageMap, defaultSummarizer: Summarizers = "pkg") {
this._coverageMap = coverageMap;
this._defaultSummarizer = defaultSummarizer;
this._initialList = coverageMap.files().map((filePath) => ({
filePath,
path: new Path(filePath),
fileCoverage: coverageMap.fileCoverageFor(filePath),
}));
this._commonParent = findCommonParent(this._initialList.map((o) => o.path.parent()));
if (this._commonParent.length > 0) {
this._initialList.forEach((o) => {
o.path.splice(0, this._commonParent.length);
});
}
}
get defaultSummarizer(): ReportTree {
return this[this._defaultSummarizer];
}
get flat(): ReportTree {
if (!this._flat) {
this._flat = new ReportTree(
ReportNode.createRoot(
this._initialList.map((node) => new ReportNode(node.path, node.fileCoverage)),
),
);
}
return this._flat;
}
private _createPkg(): ReportTree {
const dirParents = toDirParents(this._initialList);
if (dirParents.length === 1) {
return new ReportTree(dirParents[0]);
}
return new ReportTree(
ReportNode.createRoot(dirParents),
pkgSummaryPrefix(dirParents, this._commonParent),
);
}
get pkg(): ReportTree {
if (!this._pkg) {
this._pkg = this._createPkg();
}
return this._pkg;
}
private _createNested(): ReportTree {
const nodeMap: Record<string, ReportNode> = Object.create(null);
const topPaths: ReportNode[] = [];
this._initialList.forEach((o) => {
const node = new ReportNode(o.path, o.fileCoverage);
addAllPaths(topPaths, nodeMap, o.path, node);
});
const topNodes = topPaths.map((node) => foldIntoOneDir(node));
if (topNodes.length === 1) {
return new ReportTree(topNodes[0]);
}
return new ReportTree(ReportNode.createRoot(topNodes));
}
get nested(): ReportTree {
if (!this._nested) {
this._nested = this._createNested();
}
return this._nested;
}
}
export default SummarizerFactory;
export { ReportNode, ReportTree };
|