embark/lib/modules/coverage/source_map.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

class EmptySourceMap {
static createRelativeTo(sourceMapString) {
if(sourceMapString == '') return EmptySourceMap;
return new SourceMap(sourceMapString);
}
static toString() {
return '';
}
}
2018-08-07 19:26:39 +00:00
class SourceMap {
constructor(sourceMapStringOrOffset, length, id, jump) {
2018-08-07 19:26:39 +00:00
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) {
if(sourceMapString == '' || sourceMapString == undefined) return EmptySourceMap;
let [offset, length, id, jump] = sourceMapString.split(":");
(offset == '') ? offset = this.offset : offset = parseInt(offset, 10);
(id == '' || id == undefined) ? id = this.id : id = parseInt(id, 10);
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 != '') {
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;