slimming SourceMap slightly further

This commit is contained in:
Jeremy Ashkenas 2013-03-18 19:36:34 +08:00
parent 6786bab2ba
commit c8b8beb500
3 changed files with 131 additions and 176 deletions

View File

@ -247,33 +247,10 @@ code.
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">&#182;</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) -&gt;
<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">&#182;</a>
<a class="pilcrow" href="#section-8">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-9">&#182;</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,12 +282,32 @@ set &quot;sources&quot; and &quot;file&quot;, respectively.
needComma = <span class="literal">no</span>
buffer = <span class="string">""</span>
<span class="property">@each</span> (mapping) =&gt;
<span class="keyword">while</span> writingline &lt; mapping.line
lastColumn = <span class="number">0</span>
needComma = <span class="literal">no</span>
buffer += <span class="string">";"</span>
writingline++</pre></div></div>
<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 &lt; mapping.line
lastColumn = <span class="number">0</span>
needComma = <span class="literal">no</span>
buffer += <span class="string">";"</span>
writingline++</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">&#182;</a>
</div>
<p>Write a comma if we&#39;ve already written a segment on this line.
</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> needComma
buffer += <span class="string">","</span>
needComma = <span class="literal">no</span></pre></div></div>
</li>
@ -321,25 +318,6 @@ set &quot;sources&quot; and &quot;file&quot;, respectively.
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">&#182;</a>
</div>
<p>Write a comma if we&#39;ve already written a segment on this line.
</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span> needComma
buffer += <span class="string">","</span>
needComma = <span class="literal">no</span></pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">&#182;</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&#39;t match anything in the source code.
@ -351,8 +329,25 @@ column for the current line:
</div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> mapping.column - lastColumn
lastColumn = mapping.column</pre></div></div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> mapping.column - lastColumn
lastColumn = mapping.column</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">&#182;</a>
</div>
<p>The index into the list of sources:
</p>
</div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> <span class="number">0</span></pre></div></div>
</li>
@ -363,13 +358,16 @@ column for the current line:
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">&#182;</a>
</div>
<p>The index into the list of sources:
<p>The starting line in the original source, relative to the previous source line.
</p>
</div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> <span class="number">0</span></pre></div></div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> mapping.sourceLine - lastSourceLine
<span class="keyword">if</span> lastSourceLine <span class="keyword">isnt</span> mapping.sourceLine
lastSourceLine = mapping.sourceLine
lastSourceColumn = <span class="number">0</span></pre></div></div>
</li>
@ -380,16 +378,15 @@ column for the current line:
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">&#182;</a>
</div>
<p>The starting line in the original source, relative to the previous source line.
<p>The starting column in the original source, relative to the previous column.
</p>
</div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> mapping.sourceLine - lastSourceLine
<span class="keyword">if</span> lastSourceLine <span class="keyword">isnt</span> mapping.sourceLine
lastSourceLine = mapping.sourceLine
lastSourceColumn = <span class="number">0</span></pre></div></div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> mapping.sourceColumn - lastSourceColumn
lastSourceColumn = mapping.sourceColumn
needComma = <span class="literal">yes</span></pre></div></div>
</li>
@ -400,25 +397,6 @@ column for the current line:
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">&#182;</a>
</div>
<p>The starting column in the original source, relative to the previous column.
</p>
</div>
<div class="content"><div class='highlight'><pre> buffer += <span class="property">@encodeVlq</span> mapping.sourceColumn - lastSourceColumn
lastSourceColumn = mapping.sourceColumn
needComma = <span class="literal">yes</span></pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">&#182;</a>
</div>
<p>Produce the canonical JSON object format for a &quot;v3&quot; source map.
</p>
@ -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">&#182;</a>
<a class="pilcrow" href="#section-16">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-17">&#182;</a>
</div>
<p>Note that SourceMap VLQ encoding is &quot;backwards&quot;. 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">&#182;</a>
<a class="pilcrow" href="#section-18">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-19">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-20">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-21">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-22">&#182;</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">&#182;</a>
<a class="pilcrow" href="#section-23">&#182;</a>
</div>
<p>Our API for source maps is just the <code>SourceMap</code> class.

View File

@ -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,29 +79,40 @@
lastSourceColumn = 0;
needComma = false;
buffer = "";
this.each(function(mapping) {
while (writingline < mapping.line) {
lastColumn = 0;
needComma = false;
buffer += ";";
writingline++;
_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);
if (lastSourceLine !== mapping.sourceLine) {
lastSourceLine = mapping.sourceLine;
lastSourceColumn = 0;
}
buffer += this.encodeVlq(mapping.sourceColumn - lastSourceColumn);
lastSourceColumn = mapping.sourceColumn;
needComma = true;
}
}
if (needComma) {
buffer += ",";
needComma = false;
}
buffer += _this.encodeVlq(mapping.column - lastColumn);
lastColumn = mapping.column;
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);
lastSourceColumn = mapping.sourceColumn;
return needComma = true;
});
}
v3 = {
version: 3,
file: options.generatedFile || '',

View File

@ -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,18 +75,19 @@ set "sources" and "file", respectively.
needComma = no
buffer = ""
@each (mapping) =>
while writingline < mapping.line
lastColumn = 0
needComma = no
buffer += ";"
writingline++
for lineMap, lineNumber in @lines when lineMap
for mapping in lineMap.columns when mapping
while writingline < mapping.line
lastColumn = 0
needComma = no
buffer += ";"
writingline++
Write a comma if we've already written a segment on this line.
if needComma
buffer += ","
needComma = no
if needComma
buffer += ","
needComma = no
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.
@ -104,25 +95,25 @@ is a generated column which doesn't match anything in the source code.
The starting column in the generated source, relative to any previous recorded
column for the current line:
buffer += @encodeVlq mapping.column - lastColumn
lastColumn = mapping.column
buffer += @encodeVlq mapping.column - lastColumn
lastColumn = mapping.column
The index into the list of sources:
buffer += @encodeVlq 0
buffer += @encodeVlq 0
The starting line in the original source, relative to the previous source line.
buffer += @encodeVlq mapping.sourceLine - lastSourceLine
if lastSourceLine isnt mapping.sourceLine
lastSourceLine = mapping.sourceLine
lastSourceColumn = 0
buffer += @encodeVlq mapping.sourceLine - lastSourceLine
if lastSourceLine isnt mapping.sourceLine
lastSourceLine = mapping.sourceLine
lastSourceColumn = 0
The starting column in the original source, relative to the previous column.
buffer += @encodeVlq mapping.sourceColumn - lastSourceColumn
lastSourceColumn = mapping.sourceColumn
needComma = yes
buffer += @encodeVlq mapping.sourceColumn - lastSourceColumn
lastSourceColumn = mapping.sourceColumn
needComma = yes
Produce the canonical JSON object format for a "v3" source map.
@ -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