mapped.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. var FileCoverage = require('istanbul-lib-coverage').classes.FileCoverage,
  7. util = require('util');
  8. function MappedCoverage(pathOrObj) {
  9. FileCoverage.call(this, pathOrObj);
  10. this.meta = {
  11. last: {
  12. s: 0,
  13. f: 0,
  14. b: 0
  15. },
  16. seen: {}
  17. };
  18. }
  19. util.inherits(MappedCoverage, FileCoverage);
  20. function locString(loc) {
  21. return [loc.start.line, loc.start.column, loc.end.line, loc.end.column].join(':');
  22. }
  23. MappedCoverage.prototype.addStatement = function (loc, hits) {
  24. var key = 's:' + locString(loc),
  25. meta = this.meta,
  26. index = meta.seen[key];
  27. if (index === undefined) {
  28. index = meta.last.s;
  29. meta.last.s += 1;
  30. meta.seen[key] = index;
  31. this.statementMap[index] = this.cloneLocation(loc);
  32. }
  33. this.s[index] = this.s[index] || 0;
  34. this.s[index] += hits;
  35. return index;
  36. };
  37. MappedCoverage.prototype.addFunction = function (name, decl, loc, hits) {
  38. var key = 'f:' + locString(decl),
  39. meta = this.meta,
  40. index = meta.seen[key];
  41. if (index === undefined) {
  42. index = meta.last.f;
  43. meta.last.f += 1;
  44. meta.seen[key] = index;
  45. name = name || '(unknown_' + index + ')';
  46. this.fnMap[index] = {
  47. name: name,
  48. decl: this.cloneLocation(decl),
  49. loc: this.cloneLocation(loc)
  50. };
  51. }
  52. this.f[index] = this.f[index] || 0;
  53. this.f[index] += hits;
  54. return index;
  55. };
  56. MappedCoverage.prototype.addBranch = function (type, loc, branchLocations, hits) {
  57. var key = ['b'],
  58. meta = this.meta,
  59. that = this,
  60. index,
  61. i;
  62. branchLocations.forEach(function (l) {
  63. key.push(locString(l));
  64. });
  65. key = key.join(':');
  66. index = meta.seen[key];
  67. if (index === undefined) {
  68. index = meta.last.b;
  69. meta.last.b += 1;
  70. meta.seen[key] = index;
  71. this.branchMap[index] = {
  72. loc: loc,
  73. type: type,
  74. locations: branchLocations.map(function (l) {
  75. return that.cloneLocation(l);
  76. })
  77. };
  78. }
  79. if (!this.b[index]) {
  80. this.b[index] = [];
  81. branchLocations.forEach(function () {
  82. that.b[index].push(0);
  83. });
  84. }
  85. for (i = 0; i < hits.length; i += 1) {
  86. that.b[index][i] += hits[i];
  87. }
  88. return index;
  89. };
  90. // returns a clone of the location object with only
  91. // the attributes of interest
  92. MappedCoverage.prototype.cloneLocation = function (loc) {
  93. return {
  94. start: {
  95. line: loc.start.line,
  96. column: loc.start.column
  97. },
  98. end: {
  99. line: loc.end.line,
  100. column: loc.end.column
  101. }
  102. };
  103. };
  104. module.exports = {
  105. MappedCoverage: MappedCoverage
  106. };