/ lib / modules / coverage / source_map.js
source_map.js
 1  const EmptySourceMap = {
 2    createRelativeTo: function(sourceMapString) {
 3      if(sourceMapString === '') return EmptySourceMap;
 4  
 5      return new SourceMap(sourceMapString);
 6    },
 7    toString: function() {
 8      return '';
 9    }
10  };
11  
12  class SourceMap {
13    constructor(sourceMapStringOrOffset, length, id, jump) {
14      if(typeof sourceMapStringOrOffset === 'string') {
15        let [offset, length, id, jump] = sourceMapStringOrOffset.split(":");
16  
17        this.offset = parseInt(offset, 10);
18        this.length = parseInt(length, 10);
19  
20        if(id) this.id = parseInt(id, 10);
21        this.jump = jump;
22      } else {
23        this.offset = sourceMapStringOrOffset;
24        this.length = length;
25        this.id = id;
26        this.jump = jump;
27      }
28    }
29  
30    createRelativeTo(sourceMapString) {
31      if(!sourceMapString) return EmptySourceMap;
32  
33      let [offset, length, id, jump] = sourceMapString.split(":");
34  
35      offset = (offset) ? parseInt(offset, 10) : this.offset;
36      id = (id) ? parseInt(id, 10) : this.id;
37      length = parseInt(length, 10);
38  
39      return new SourceMap(offset, length, id, jump);
40    }
41  
42    subtract(sourceMap) {
43      return new SourceMap(this.offset, sourceMap.offset - this.offset, this.id, this.jump);
44    }
45  
46    toString(defaultId) {
47      let parts = [this.offset, this.length];
48  
49      if(this.id !== undefined && this.id !== '') {
50        parts.push(this.id);
51      } else if(defaultId !== undefined) {
52        parts.push(defaultId);
53      }
54  
55      return parts.join(':');
56    }
57  
58    static empty() {
59      return EmptySourceMap;
60    }
61  }
62  
63  module.exports = SourceMap;