summarizer-factory.js
7.18 KB
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
/*
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;