Skip to content

Commit 9e3e676

Browse files
bcoecodebytere
authored andcommittedFeb 29, 2020
module: port source map sort logic from chromium
Digging in to the delta between V8's source map library, and chromium's the most significant difference that jumped out at me was that we were failing to sort generated columns. Since negative offsets are not restricted in the spec, this can lead to bugs. fixes: #31286 PR-URL: #31927 Fixes: #31286 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 6a35b0d commit 9e3e676

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed
 

‎lib/internal/source_map/source_map.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ class SourceMap {
152152
* @param {SourceMapV3} mappingPayload
153153
*/
154154
#parseMappingPayload = () => {
155-
if (this.#payload.sections)
155+
if (this.#payload.sections) {
156156
this.#parseSections(this.#payload.sections);
157-
else
157+
} else {
158158
this.#parseMap(this.#payload, 0, 0);
159+
}
160+
this.#mappings.sort(compareSourceMapEntry);
159161
}
160162

161163
/**
@@ -321,6 +323,21 @@ function cloneSourceMapV3(payload) {
321323
return payload;
322324
}
323325

326+
/**
327+
* @param {Array} entry1 source map entry [lineNumber, columnNumber, sourceURL,
328+
* sourceLineNumber, sourceColumnNumber]
329+
* @param {Array} entry2 source map entry.
330+
* @return {number}
331+
*/
332+
function compareSourceMapEntry(entry1, entry2) {
333+
const [lineNumber1, columnNumber1] = entry1;
334+
const [lineNumber2, columnNumber2] = entry2;
335+
if (lineNumber1 !== lineNumber2) {
336+
return lineNumber1 - lineNumber2;
337+
}
338+
return columnNumber1 - columnNumber2;
339+
}
340+
324341
module.exports = {
325342
SourceMap
326343
};

‎test/parallel/test-source-map-api.js

+25
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,28 @@ const { readFileSync } = require('fs');
124124
assert.strictEqual(originalColumn, knownDecodings[column]);
125125
}
126126
}
127+
128+
// Test that generated columns are sorted when a negative offset is
129+
// observed, see: https://github.com/mozilla/source-map/pull/92
130+
{
131+
function makeMinimalMap(generatedColumns, originalColumns) {
132+
return {
133+
sources: ['test.js'],
134+
// Mapping from the 0th line, ${g}th column of the output file to the 0th
135+
// source file, 0th line, ${column}th column.
136+
mappings: generatedColumns.map((g, i) => `${g}AA${originalColumns[i]}`)
137+
.join(',')
138+
};
139+
}
140+
// U = 10
141+
// F = -2
142+
// A = 0
143+
// E = 2
144+
const sourceMap = new SourceMap(makeMinimalMap(
145+
['U', 'F', 'F'],
146+
['A', 'E', 'E']
147+
));
148+
assert.strictEqual(sourceMap.findEntry(0, 6).originalColumn, 4);
149+
assert.strictEqual(sourceMap.findEntry(0, 8).originalColumn, 2);
150+
assert.strictEqual(sourceMap.findEntry(0, 10).originalColumn, 0);
151+
}

0 commit comments

Comments
 (0)
Please sign in to comment.