embark-area-51/lib/modules/coverage/source_map.js

64 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-08-30 17:50:36 +00:00
const EmptySourceMap = {
createRelativeTo: function(sourceMapString) {
2018-08-30 17:01:13 +00:00
if(sourceMapString === '') return EmptySourceMap;
return new SourceMap(sourceMapString);
2018-08-30 17:50:36 +00:00
},
toString: function() {
return '';
}
2018-08-30 17:50:36 +00:00
};
2018-08-07 19:26:39 +00:00
class SourceMap {
constructor(sourceMapStringOrOffset, length, id, jump) {
if(typeof sourceMapStringOrOffset === 'string') {
let [offset, length, id, jump] = sourceMapStringOrOffset.split(":");
2018-08-07 19:26:39 +00:00
this.offset = parseInt(offset, 10);
this.length = parseInt(length, 10);
if(id) this.id = parseInt(id, 10);
this.jump = jump;
2018-08-07 19:26:39 +00:00
} else {
this.offset = sourceMapStringOrOffset;
this.length = length;
this.id = id;
this.jump = jump;
2018-08-07 19:26:39 +00:00
}
}
createRelativeTo(sourceMapString) {
2018-08-30 17:01:13 +00:00
if(!sourceMapString) return EmptySourceMap;
let [offset, length, id, jump] = sourceMapString.split(":");
2018-08-30 17:01:13 +00:00
offset = (offset) ? parseInt(offset, 10) : this.offset;
id = (id) ? parseInt(id, 10) : this.id;
length = parseInt(length, 10);
return new SourceMap(offset, length, id, jump);
}
2018-08-07 19:26:39 +00:00
subtract(sourceMap) {
return new SourceMap(this.offset, sourceMap.offset - this.offset, this.id, this.jump);
2018-08-07 19:26:39 +00:00
}
toString(defaultId) {
let parts = [this.offset, this.length];
if(this.id !== undefined && this.id !== '') {
2018-08-28 17:51:55 +00:00
parts.push(this.id);
} else if(defaultId !== undefined) {
parts.push(defaultId);
}
2018-08-07 19:26:39 +00:00
return parts.join(':');
}
static empty() {
return EmptySourceMap;
}
2018-08-07 19:26:39 +00:00
}
module.exports = SourceMap;