Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9e3e676

Browse filesBrowse files
bcoecodebytere
authored andcommitted
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
Copy full SHA for 9e3e676

File tree

Expand file treeCollapse file tree

2 files changed

+44
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+44
-2
lines changed
Open diff view settings
Collapse file

‎lib/internal/source_map/source_map.js‎

Copy file name to clipboardExpand all lines: lib/internal/source_map/source_map.js
+19-2Lines changed: 19 additions & 2 deletions
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
};
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-source-map-api.js
+25Lines changed: 25 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.