UNPKG

4.86 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.SourceCoverage = undefined;
7
8var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
10var _istanbulLibCoverage = require('istanbul-lib-coverage');
11
12function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
14function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
15
16function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
17
18function cloneLocation(loc) {
19 return {
20 start: {
21 line: loc && loc.start.line,
22 column: loc && loc.start.column
23 },
24 end: {
25 line: loc && loc.end.line,
26 column: loc && loc.end.column
27 }
28 };
29}
30/**
31 * SourceCoverage provides mutation methods to manipulate the structure of
32 * a file coverage object. Used by the instrumenter to create a full coverage
33 * object for a file incrementally.
34 *
35 * @private
36 * @param pathOrObj {String|Object} - see the argument for {@link FileCoverage}
37 * @extends FileCoverage
38 * @constructor
39 */
40
41var SourceCoverage = function (_classes$FileCoverage) {
42 _inherits(SourceCoverage, _classes$FileCoverage);
43
44 function SourceCoverage(pathOrObj) {
45 _classCallCheck(this, SourceCoverage);
46
47 var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(SourceCoverage).call(this, pathOrObj));
48
49 _this.meta = {
50 last: {
51 s: 0,
52 f: 0,
53 b: 0
54 }
55 };
56 return _this;
57 }
58
59 _createClass(SourceCoverage, [{
60 key: 'newStatement',
61 value: function newStatement(loc) {
62 this.meta.last.s += 1;
63 var s = this.meta.last.s;
64 this.data.statementMap[s] = cloneLocation(loc);
65 this.data.s[s] = 0;
66 return s;
67 }
68 }, {
69 key: 'newFunction',
70 value: function newFunction(name, decl, loc) {
71 this.meta.last.f += 1;
72 var f = this.meta.last.f;
73 name = name || '(anonymous_' + f + ')';
74 this.data.fnMap[f] = {
75 name: name,
76 decl: cloneLocation(decl),
77 loc: cloneLocation(loc)
78 };
79 this.data.f[f] = 0;
80 return f;
81 }
82 }, {
83 key: 'newBranch',
84 value: function newBranch(type, loc) {
85 this.meta.last.b += 1;
86 var b = this.meta.last.b;
87 this.data.b[b] = [];
88 this.data.branchMap[b] = {
89 loc: cloneLocation(loc),
90 type: type,
91 locations: []
92 };
93 return b;
94 }
95 }, {
96 key: 'addBranchPath',
97 value: function addBranchPath(name, location) {
98 var bMeta = this.data.branchMap[name],
99 counts = this.data.b[name];
100
101 /* istanbul ignore if: paranoid check */
102 if (!bMeta) {
103 throw new Error("Invalid branch " + name);
104 }
105 bMeta.locations.push(cloneLocation(location));
106 counts.push(0);
107 return counts.length - 1;
108 }
109 }, {
110 key: 'freeze',
111 value: function freeze() {
112 // prune empty branches
113 var map = this.data.branchMap,
114 branches = this.data.b;
115 Object.keys(map).forEach(function (b) {
116 if (map[b].locations.length === 0) {
117 delete map[b];
118 delete branches[b];
119 }
120 });
121 }
122 }]);
123
124 return SourceCoverage;
125}(_istanbulLibCoverage.classes.FileCoverage);
126
127exports.SourceCoverage = SourceCoverage;
\No newline at end of file