123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /*
- Copyright 2012-2015, Yahoo Inc.
- Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
- */
- 'use strict';
- const coverage = require('istanbul-lib-coverage');
- const Path = require('./path');
- const { BaseNode, BaseTree } = require('./tree');
- class ReportNode extends BaseNode {
- constructor(path, fileCoverage) {
- super();
- this.path = path;
- this.parent = null;
- this.fileCoverage = fileCoverage;
- this.children = [];
- }
- static createRoot(children) {
- const root = new ReportNode(new Path([]));
- children.forEach(child => {
- root.addChild(child);
- });
- return root;
- }
- addChild(child) {
- child.parent = this;
- this.children.push(child);
- }
- asRelative(p) {
- if (p.substring(0, 1) === '/') {
- return p.substring(1);
- }
- return p;
- }
- getQualifiedName() {
- return this.asRelative(this.path.toString());
- }
- getRelativeName() {
- 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() {
- return this.parent;
- }
- getChildren() {
- return this.children;
- }
- isSummary() {
- return !this.fileCoverage;
- }
- getFileCoverage() {
- return this.fileCoverage;
- }
- getCoverageSummary(filesOnly) {
- const cacheProp = `c_${filesOnly ? 'files' : 'full'}`;
- let summary;
- if (Object.prototype.hasOwnProperty.call(this, cacheProp)) {
- return this[cacheProp];
- }
- if (!this.isSummary()) {
- summary = this.getFileCoverage().toSummary();
- } else {
- let count = 0;
- summary = coverage.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 {
- constructor(root, childPrefix) {
- super(root);
- const maybePrefix = node => {
- 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;
- });
- }
- });
- }
- }
- function findCommonParent(paths) {
- return paths.reduce(
- (common, path) => common.commonPrefixPath(path),
- paths[0] || new Path([])
- );
- }
- function findOrCreateParent(parentPath, nodeMap, created = () => {}) {
- let parent = nodeMap[parentPath.toString()];
- if (!parent) {
- parent = new ReportNode(parentPath);
- nodeMap[parentPath.toString()] = parent;
- created(parentPath, parent);
- }
- return parent;
- }
- function toDirParents(list) {
- const nodeMap = 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, nodeMap, path, node) {
- 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, parent) {
- const { children } = node;
- if (children.length === 1 && !children[0].fileCoverage) {
- children[0].parent = parent;
- return foldIntoOneDir(children[0], parent);
- }
- node.children = children.map(child => foldIntoOneDir(child, node));
- return node;
- }
- function pkgSummaryPrefix(dirParents, commonParent) {
- if (!dirParents.some(dp => dp.path.length === 0)) {
- return;
- }
- if (commonParent.length === 0) {
- return 'root';
- }
- return commonParent.name();
- }
- class SummarizerFactory {
- constructor(coverageMap, defaultSummarizer = '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() {
- return this[this._defaultSummarizer];
- }
- get flat() {
- if (!this._flat) {
- this._flat = new ReportTree(
- ReportNode.createRoot(
- this._initialList.map(
- node => new ReportNode(node.path, node.fileCoverage)
- )
- )
- );
- }
- return this._flat;
- }
- _createPkg() {
- 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() {
- if (!this._pkg) {
- this._pkg = this._createPkg();
- }
- return this._pkg;
- }
- _createNested() {
- const nodeMap = Object.create(null);
- const topPaths = [];
- 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() {
- if (!this._nested) {
- this._nested = this._createNested();
- }
- return this._nested;
- }
- }
- module.exports = SummarizerFactory;
|