1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffee-script/sourcemap.js
2017-02-09 13:28:22 -08:00

153 lines
4.4 KiB
JavaScript

// Generated by CoffeeScript 2.0.0-alpha1
(function() {
var LineMap, SourceMap;
LineMap = class LineMap {
constructor(line1) {
this.line = line1;
this.columns = [];
}
add(column, arg, options) {
var sourceColumn, sourceLine;
sourceLine = arg[0], sourceColumn = arg[1];
if (options === void 0) {
options = {};
}
if (this.columns[column] && options.noReplace) {
return;
}
return this.columns[column] = {
line: this.line,
column: column,
sourceLine: sourceLine,
sourceColumn: sourceColumn
};
}
sourceLocation(column) {
var mapping;
while (!((mapping = this.columns[column]) || (column <= 0))) {
column--;
}
return mapping && [mapping.sourceLine, mapping.sourceColumn];
}
};
SourceMap = (function() {
var BASE64_CHARS, VLQ_CONTINUATION_BIT, VLQ_SHIFT, VLQ_VALUE_MASK;
class SourceMap {
constructor() {
this.lines = [];
}
add(sourceLocation, generatedLocation, options = {}) {
var base, column, line, lineMap;
line = generatedLocation[0], column = generatedLocation[1];
lineMap = ((base = this.lines)[line] || (base[line] = new LineMap(line)));
return lineMap.add(column, sourceLocation, options);
}
sourceLocation(arg) {
var column, line, lineMap;
line = arg[0], column = arg[1];
while (!((lineMap = this.lines[line]) || (line <= 0))) {
line--;
}
return lineMap && lineMap.sourceLocation(column);
}
generate(options = {}, code = null) {
var buffer, i, j, lastColumn, lastSourceColumn, lastSourceLine, len, len1, lineMap, lineNumber, mapping, needComma, ref, ref1, v3, writingline;
writingline = 0;
lastColumn = 0;
lastSourceLine = 0;
lastSourceColumn = 0;
needComma = false;
buffer = "";
ref = this.lines;
for (lineNumber = i = 0, len = ref.length; i < len; lineNumber = ++i) {
lineMap = ref[lineNumber];
if (lineMap) {
ref1 = lineMap.columns;
for (j = 0, len1 = ref1.length; j < len1; j++) {
mapping = ref1[j];
if (!(mapping)) {
continue;
}
while (writingline < mapping.line) {
lastColumn = 0;
needComma = false;
buffer += ";";
writingline++;
}
if (needComma) {
buffer += ",";
needComma = false;
}
buffer += this.encodeVlq(mapping.column - lastColumn);
lastColumn = mapping.column;
buffer += this.encodeVlq(0);
buffer += this.encodeVlq(mapping.sourceLine - lastSourceLine);
lastSourceLine = mapping.sourceLine;
buffer += this.encodeVlq(mapping.sourceColumn - lastSourceColumn);
lastSourceColumn = mapping.sourceColumn;
needComma = true;
}
}
}
v3 = {
version: 3,
file: options.generatedFile || '',
sourceRoot: options.sourceRoot || '',
sources: options.sourceFiles || [''],
names: [],
mappings: buffer
};
if (options.inlineMap) {
v3.sourcesContent = [code];
}
return v3;
}
encodeVlq(value) {
var answer, nextChunk, signBit, valueToEncode;
answer = '';
signBit = value < 0 ? 1 : 0;
valueToEncode = (Math.abs(value) << 1) + signBit;
while (valueToEncode || !answer) {
nextChunk = valueToEncode & VLQ_VALUE_MASK;
valueToEncode = valueToEncode >> VLQ_SHIFT;
if (valueToEncode) {
nextChunk |= VLQ_CONTINUATION_BIT;
}
answer += this.encodeBase64(nextChunk);
}
return answer;
}
encodeBase64(value) {
return BASE64_CHARS[value] || (function() {
throw new Error(`Cannot Base64 encode value: ${value}`);
})();
}
};
VLQ_SHIFT = 5;
VLQ_CONTINUATION_BIT = 1 << VLQ_SHIFT;
VLQ_VALUE_MASK = VLQ_CONTINUATION_BIT - 1;
BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
return SourceMap;
})();
module.exports = SourceMap;
}).call(this);