source-coverage.js 5.4 KB

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