1 | /*
|
---|
2 | Copyright 2015, Yahoo Inc.
|
---|
3 | Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
---|
4 | */
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 | const { FileCoverage } = require('istanbul-lib-coverage').classes;
|
---|
8 |
|
---|
9 | function locString(loc) {
|
---|
10 | return [
|
---|
11 | loc.start.line,
|
---|
12 | loc.start.column,
|
---|
13 | loc.end.line,
|
---|
14 | loc.end.column
|
---|
15 | ].join(':');
|
---|
16 | }
|
---|
17 |
|
---|
18 | class MappedCoverage extends FileCoverage {
|
---|
19 | constructor(pathOrObj) {
|
---|
20 | super(pathOrObj);
|
---|
21 |
|
---|
22 | this.meta = {
|
---|
23 | last: {
|
---|
24 | s: 0,
|
---|
25 | f: 0,
|
---|
26 | b: 0
|
---|
27 | },
|
---|
28 | seen: {}
|
---|
29 | };
|
---|
30 | }
|
---|
31 |
|
---|
32 | addStatement(loc, hits) {
|
---|
33 | const key = 's:' + locString(loc);
|
---|
34 | const { meta } = this;
|
---|
35 | let index = meta.seen[key];
|
---|
36 |
|
---|
37 | if (index === undefined) {
|
---|
38 | index = meta.last.s;
|
---|
39 | meta.last.s += 1;
|
---|
40 | meta.seen[key] = index;
|
---|
41 | this.statementMap[index] = this.cloneLocation(loc);
|
---|
42 | }
|
---|
43 |
|
---|
44 | this.s[index] = this.s[index] || 0;
|
---|
45 | this.s[index] += hits;
|
---|
46 | return index;
|
---|
47 | }
|
---|
48 |
|
---|
49 | addFunction(name, decl, loc, hits) {
|
---|
50 | const key = 'f:' + locString(decl);
|
---|
51 | const { meta } = this;
|
---|
52 | let index = meta.seen[key];
|
---|
53 |
|
---|
54 | if (index === undefined) {
|
---|
55 | index = meta.last.f;
|
---|
56 | meta.last.f += 1;
|
---|
57 | meta.seen[key] = index;
|
---|
58 | name = name || `(unknown_${index})`;
|
---|
59 | this.fnMap[index] = {
|
---|
60 | name,
|
---|
61 | decl: this.cloneLocation(decl),
|
---|
62 | loc: this.cloneLocation(loc)
|
---|
63 | };
|
---|
64 | }
|
---|
65 |
|
---|
66 | this.f[index] = this.f[index] || 0;
|
---|
67 | this.f[index] += hits;
|
---|
68 | return index;
|
---|
69 | }
|
---|
70 |
|
---|
71 | addBranch(type, loc, branchLocations, hits) {
|
---|
72 | const key = ['b', ...branchLocations.map(l => locString(l))].join(':');
|
---|
73 | const { meta } = this;
|
---|
74 | let index = meta.seen[key];
|
---|
75 | if (index === undefined) {
|
---|
76 | index = meta.last.b;
|
---|
77 | meta.last.b += 1;
|
---|
78 | meta.seen[key] = index;
|
---|
79 | this.branchMap[index] = {
|
---|
80 | loc,
|
---|
81 | type,
|
---|
82 | locations: branchLocations.map(l => this.cloneLocation(l))
|
---|
83 | };
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (!this.b[index]) {
|
---|
87 | this.b[index] = branchLocations.map(() => 0);
|
---|
88 | }
|
---|
89 |
|
---|
90 | hits.forEach((hit, i) => {
|
---|
91 | this.b[index][i] += hit;
|
---|
92 | });
|
---|
93 | return index;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Returns a clone of the location object with only the attributes of interest */
|
---|
97 | cloneLocation(loc) {
|
---|
98 | return {
|
---|
99 | start: {
|
---|
100 | line: loc.start.line,
|
---|
101 | column: loc.start.column
|
---|
102 | },
|
---|
103 | end: {
|
---|
104 | line: loc.end.line,
|
---|
105 | column: loc.end.column
|
---|
106 | }
|
---|
107 | };
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | module.exports = {
|
---|
112 | MappedCoverage
|
---|
113 | };
|
---|