all files / src/ SparceCoverageCollector.js

95.65% Statements 44/46
85.71% Branches 12/14
100% Functions 3/3
95.35% Lines 41/43
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  15× 15× 15×       387× 387× 12×                 12×                   387×                           13×     150×     50× 100×         50×   50× 50× 50× 50× 50×     50× 100×               168×     56×           56× 56× 55× 55× 55×     56× 56×     828×     276×           276× 276× 251× 251× 251×     276× 276×        
class SparceCoverageCollector {
  constructor() {
    this.srcCoverage = {};
    this.metaInfo = {};
  }
 
  getSourceCoverage(filename) {
    let data = this.srcCoverage[filename];
    if (!data) {
      data = this.srcCoverage[filename] = {
        path: filename,
        statementMap: {},
        fnMap: {},
        branchMap: {},
        s: {},
        b: {},
        f: {},
      };
      this.metaInfo[filename] = {
        indexes: {},
        lastIndex: {
          s: 0,
          b: 0,
          f: 0,
        },
      };
    }
 
    return {
      data,
      meta: this.metaInfo[filename],
    };
  }
 
  setCoverage(filePath, fileCoverage) {
    this.srcCoverage[filePath] = fileCoverage;
  }
 
  setSourceCode(filePath, source) {
    this.getSourceCoverage(filePath).data.code = source;
  }
 
 
  getFinalCoverage() {
    return this.srcCoverage;
  }
 
  updateBranch(source, srcItem, hits) {
    const { data, meta } = this.getSourceCoverage(source);
 
    let key = ['b'];
    srcItem.locations.map(loc => key.push(
      loc.start.line, loc.start.column,
      loc.end.line, loc.end.line
    ));
 
    key = key.join(':');
 
    let index = meta.indexes[key];
    Eif (!index) {
      index = ++meta.lastIndex.b;
      meta.indexes[key] = index;
      data.branchMap[index] = srcItem;
    }
 
    Eif (!data.b[index]) {
      data.b[index] = hits.map(v => v);
    } else {
      for (let i = 0; i < hits.length; ++i) {
        data.b[index][i] += hits[i];
      }
    }
  }
 
  updateFunction(source, srcItem, hits) {
    const { data, meta } = this.getSourceCoverage(source);
 
    const key = [
      'f',
      srcItem.loc.start.line, srcItem.loc.start.column,
      srcItem.loc.end.line, srcItem.loc.end.column,
    ].join(':');
 
    let index = meta.indexes[key];
    if (!index) {
      index = ++meta.lastIndex.f;
      meta.indexes[key] = index;
      data.fnMap[index] = srcItem;
    }
 
    data.f[index] = data.f[index] || 0;
    data.f[index] += hits;
  }
 
  updateStatement(source, srcItem, hits) {
    const { data, meta } = this.getSourceCoverage(source);
 
    const key = [
      's',
      srcItem.start.line, srcItem.start.column,
      srcItem.end.line, srcItem.end.column,
    ].join(':');
 
    let index = meta.indexes[key];
    if (!index) {
      index = ++meta.lastIndex.s;
      meta.indexes[key] = index;
      data.statementMap[index] = srcItem;
    }
 
    data.s[index] = data.s[index] || 0;
    data.s[index] += hits;
  }
}
 
module.exports.SparceCoverageCollector = SparceCoverageCollector;