mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
slimming SourceMap slightly further
This commit is contained in:
parent
6786bab2ba
commit
c8b8beb500
3 changed files with 131 additions and 176 deletions
|
@ -247,33 +247,10 @@ code.
|
|||
|
||||
|
||||
<li id="section-8">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-8">¶</a>
|
||||
</div>
|
||||
<p><code>func</code> will be called once for every recorded mapping, in the order in
|
||||
which they occur in the generated source. <code>fn</code> will be passed an object
|
||||
with four properties: sourceLine, sourceColumn, line, and
|
||||
column.
|
||||
|
||||
</p>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="content"><div class='highlight'><pre> each: (iterator) ->
|
||||
<span class="keyword">for</span> lineMap, lineNumber <span class="keyword">in</span> <span class="property">@lines</span> <span class="keyword">when</span> lineMap
|
||||
<span class="keyword">for</span> mapping <span class="keyword">in</span> lineMap.columns <span class="keyword">when</span> mapping
|
||||
iterator mapping</pre></div></div>
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
<li id="section-9">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap for-h2">
|
||||
<a class="pilcrow" href="#section-9">¶</a>
|
||||
<a class="pilcrow" href="#section-8">¶</a>
|
||||
</div>
|
||||
<h2>V3 SourceMap Generation</h2>
|
||||
|
||||
|
@ -282,11 +259,11 @@ column.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-10">
|
||||
<li id="section-9">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-10">¶</a>
|
||||
<a class="pilcrow" href="#section-9">¶</a>
|
||||
</div>
|
||||
<p>Builds up a V3 source map, returning the generated JSON as a string.
|
||||
<code>options.sourceRoot</code> may be used to specify the sourceRoot written to the source
|
||||
|
@ -305,7 +282,8 @@ set "sources" and "file", respectively.
|
|||
needComma = <span class="literal">no</span>
|
||||
buffer = <span class="string">""</span>
|
||||
|
||||
<span class="property">@each</span> (mapping) =>
|
||||
<span class="keyword">for</span> lineMap, lineNumber <span class="keyword">in</span> <span class="property">@lines</span> <span class="keyword">when</span> lineMap
|
||||
<span class="keyword">for</span> mapping <span class="keyword">in</span> lineMap.columns <span class="keyword">when</span> mapping
|
||||
<span class="keyword">while</span> writingline < mapping.line
|
||||
lastColumn = <span class="number">0</span>
|
||||
needComma = <span class="literal">no</span>
|
||||
|
@ -315,11 +293,11 @@ set "sources" and "file", respectively.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-11">
|
||||
<li id="section-10">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-11">¶</a>
|
||||
<a class="pilcrow" href="#section-10">¶</a>
|
||||
</div>
|
||||
<p>Write a comma if we've already written a segment on this line.
|
||||
|
||||
|
@ -334,11 +312,11 @@ set "sources" and "file", respectively.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-12">
|
||||
<li id="section-11">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-12">¶</a>
|
||||
<a class="pilcrow" href="#section-11">¶</a>
|
||||
</div>
|
||||
<p>Write the next segment. Segments can be 1, 4, or 5 values. If just one, then it
|
||||
is a generated column which doesn't match anything in the source code.
|
||||
|
@ -357,11 +335,11 @@ column for the current line:
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-13">
|
||||
<li id="section-12">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-13">¶</a>
|
||||
<a class="pilcrow" href="#section-12">¶</a>
|
||||
</div>
|
||||
<p>The index into the list of sources:
|
||||
|
||||
|
@ -374,11 +352,11 @@ column for the current line:
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-14">
|
||||
<li id="section-13">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-14">¶</a>
|
||||
<a class="pilcrow" href="#section-13">¶</a>
|
||||
</div>
|
||||
<p>The starting line in the original source, relative to the previous source line.
|
||||
|
||||
|
@ -394,11 +372,11 @@ column for the current line:
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-15">
|
||||
<li id="section-14">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-15">¶</a>
|
||||
<a class="pilcrow" href="#section-14">¶</a>
|
||||
</div>
|
||||
<p>The starting column in the original source, relative to the previous column.
|
||||
|
||||
|
@ -413,11 +391,11 @@ column for the current line:
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-16">
|
||||
<li id="section-15">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-16">¶</a>
|
||||
<a class="pilcrow" href="#section-15">¶</a>
|
||||
</div>
|
||||
<p>Produce the canonical JSON object format for a "v3" source map.
|
||||
|
||||
|
@ -435,16 +413,16 @@ column for the current line:
|
|||
|
||||
v3.sourcesContent = [code] <span class="keyword">if</span> options.inline
|
||||
|
||||
<span class="keyword">return</span> JSON.stringify v3, <span class="literal">null</span>, <span class="number">2</span></pre></div></div>
|
||||
JSON.stringify v3, <span class="literal">null</span>, <span class="number">2</span></pre></div></div>
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
<li id="section-17">
|
||||
<li id="section-16">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap for-h2">
|
||||
<a class="pilcrow" href="#section-17">¶</a>
|
||||
<a class="pilcrow" href="#section-16">¶</a>
|
||||
</div>
|
||||
<h2>Base64 VLQ Encoding</h2>
|
||||
|
||||
|
@ -453,11 +431,11 @@ column for the current line:
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-18">
|
||||
<li id="section-17">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-18">¶</a>
|
||||
<a class="pilcrow" href="#section-17">¶</a>
|
||||
</div>
|
||||
<p>Note that SourceMap VLQ encoding is "backwards". MIDI-style VLQ encoding puts
|
||||
the most-significant-bit (MSB) from the original value into the MSB of the VLQ
|
||||
|
@ -479,11 +457,11 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-19">
|
||||
<li id="section-18">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-19">¶</a>
|
||||
<a class="pilcrow" href="#section-18">¶</a>
|
||||
</div>
|
||||
<p>Least significant bit represents the sign.
|
||||
</p>
|
||||
|
@ -495,11 +473,11 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-20">
|
||||
<li id="section-19">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-20">¶</a>
|
||||
<a class="pilcrow" href="#section-19">¶</a>
|
||||
</div>
|
||||
<p>The next bits are the actual value.
|
||||
</p>
|
||||
|
@ -511,11 +489,11 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-21">
|
||||
<li id="section-20">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-21">¶</a>
|
||||
<a class="pilcrow" href="#section-20">¶</a>
|
||||
</div>
|
||||
<p>Make sure we encode at least one character, even if valueToEncode is 0.
|
||||
</p>
|
||||
|
@ -528,16 +506,16 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
nextChunk |= VLQ_CONTINUATION_BIT <span class="keyword">if</span> valueToEncode
|
||||
answer += <span class="property">@encodeBase64</span> nextChunk
|
||||
|
||||
<span class="keyword">return</span> answer</pre></div></div>
|
||||
answer</pre></div></div>
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
<li id="section-22">
|
||||
<li id="section-21">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap for-h2">
|
||||
<a class="pilcrow" href="#section-22">¶</a>
|
||||
<a class="pilcrow" href="#section-21">¶</a>
|
||||
</div>
|
||||
<h2>Regular Base64 Encoding</h2>
|
||||
|
||||
|
@ -546,11 +524,11 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-23">
|
||||
<li id="section-22">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-23">¶</a>
|
||||
<a class="pilcrow" href="#section-22">¶</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
@ -563,11 +541,11 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
</li>
|
||||
|
||||
|
||||
<li id="section-24">
|
||||
<li id="section-23">
|
||||
<div class="annotation">
|
||||
|
||||
<div class="pilwrap ">
|
||||
<a class="pilcrow" href="#section-24">¶</a>
|
||||
<a class="pilcrow" href="#section-23">¶</a>
|
||||
</div>
|
||||
<p>Our API for source maps is just the <code>SourceMap</code> class.
|
||||
|
||||
|
|
|
@ -65,33 +65,8 @@
|
|||
return lineMap && lineMap.sourceLocation(column);
|
||||
};
|
||||
|
||||
SourceMap.prototype.each = function(iterator) {
|
||||
var lineMap, lineNumber, mapping, _i, _len, _ref, _results;
|
||||
_ref = this.lines;
|
||||
_results = [];
|
||||
for (lineNumber = _i = 0, _len = _ref.length; _i < _len; lineNumber = ++_i) {
|
||||
lineMap = _ref[lineNumber];
|
||||
if (lineMap) {
|
||||
_results.push((function() {
|
||||
var _j, _len1, _ref1, _results1;
|
||||
_ref1 = lineMap.columns;
|
||||
_results1 = [];
|
||||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
||||
mapping = _ref1[_j];
|
||||
if (mapping) {
|
||||
_results1.push(iterator(mapping));
|
||||
}
|
||||
}
|
||||
return _results1;
|
||||
})());
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
SourceMap.prototype.generate = function(options, code) {
|
||||
var buffer, lastColumn, lastSourceColumn, lastSourceLine, needComma, v3, writingline,
|
||||
_this = this;
|
||||
var buffer, lastColumn, lastSourceColumn, lastSourceLine, lineMap, lineNumber, mapping, needComma, v3, writingline, _i, _j, _len, _len1, _ref, _ref1;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
|
@ -104,7 +79,16 @@
|
|||
lastSourceColumn = 0;
|
||||
needComma = false;
|
||||
buffer = "";
|
||||
this.each(function(mapping) {
|
||||
_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;
|
||||
|
@ -115,18 +99,20 @@
|
|||
buffer += ",";
|
||||
needComma = false;
|
||||
}
|
||||
buffer += _this.encodeVlq(mapping.column - lastColumn);
|
||||
buffer += this.encodeVlq(mapping.column - lastColumn);
|
||||
lastColumn = mapping.column;
|
||||
buffer += _this.encodeVlq(0);
|
||||
buffer += _this.encodeVlq(mapping.sourceLine - lastSourceLine);
|
||||
buffer += this.encodeVlq(0);
|
||||
buffer += this.encodeVlq(mapping.sourceLine - lastSourceLine);
|
||||
if (lastSourceLine !== mapping.sourceLine) {
|
||||
lastSourceLine = mapping.sourceLine;
|
||||
lastSourceColumn = 0;
|
||||
}
|
||||
buffer += _this.encodeVlq(mapping.sourceColumn - lastSourceColumn);
|
||||
buffer += this.encodeVlq(mapping.sourceColumn - lastSourceColumn);
|
||||
lastSourceColumn = mapping.sourceColumn;
|
||||
return needComma = true;
|
||||
});
|
||||
needComma = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
v3 = {
|
||||
version: 3,
|
||||
file: options.generatedFile || '',
|
||||
|
|
|
@ -58,16 +58,6 @@ code.
|
|||
line-- until (lineMap = @lines[line]) or (line <= 0)
|
||||
lineMap and lineMap.sourceLocation column
|
||||
|
||||
`func` will be called once for every recorded mapping, in the order in
|
||||
which they occur in the generated source. `fn` will be passed an object
|
||||
with four properties: sourceLine, sourceColumn, line, and
|
||||
column.
|
||||
|
||||
each: (iterator) ->
|
||||
for lineMap, lineNumber in @lines when lineMap
|
||||
for mapping in lineMap.columns when mapping
|
||||
iterator mapping
|
||||
|
||||
|
||||
V3 SourceMap Generation
|
||||
-----------------------
|
||||
|
@ -85,7 +75,8 @@ set "sources" and "file", respectively.
|
|||
needComma = no
|
||||
buffer = ""
|
||||
|
||||
@each (mapping) =>
|
||||
for lineMap, lineNumber in @lines when lineMap
|
||||
for mapping in lineMap.columns when mapping
|
||||
while writingline < mapping.line
|
||||
lastColumn = 0
|
||||
needComma = no
|
||||
|
@ -136,7 +127,7 @@ Produce the canonical JSON object format for a "v3" source map.
|
|||
|
||||
v3.sourcesContent = [code] if options.inline
|
||||
|
||||
return JSON.stringify v3, null, 2
|
||||
JSON.stringify v3, null, 2
|
||||
|
||||
|
||||
Base64 VLQ Encoding
|
||||
|
@ -168,7 +159,7 @@ bits of the original value encoded into the first byte of the VLQ encoded value.
|
|||
nextChunk |= VLQ_CONTINUATION_BIT if valueToEncode
|
||||
answer += @encodeBase64 nextChunk
|
||||
|
||||
return answer
|
||||
answer
|
||||
|
||||
|
||||
Regular Base64 Encoding
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue