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: https://github.com/nodejs/node/pull/31927
Fixes: https://github.com/nodejs/node/issues/31286
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
bcoe
2020-02-23 19:19:15 -08:00
committed by Benjamin Coe
parent a777cfa843
commit fb26b13623
2 changed files with 44 additions and 2 deletions

View File

@@ -152,10 +152,12 @@ class SourceMap {
* @param {SourceMapV3} mappingPayload
*/
#parseMappingPayload = () => {
if (this.#payload.sections)
if (this.#payload.sections) {
this.#parseSections(this.#payload.sections);
else
} else {
this.#parseMap(this.#payload, 0, 0);
}
this.#mappings.sort(compareSourceMapEntry);
}
/**
@@ -321,6 +323,21 @@ function cloneSourceMapV3(payload) {
return payload;
}
/**
* @param {Array} entry1 source map entry [lineNumber, columnNumber, sourceURL,
* sourceLineNumber, sourceColumnNumber]
* @param {Array} entry2 source map entry.
* @return {number}
*/
function compareSourceMapEntry(entry1, entry2) {
const [lineNumber1, columnNumber1] = entry1;
const [lineNumber2, columnNumber2] = entry2;
if (lineNumber1 !== lineNumber2) {
return lineNumber1 - lineNumber2;
}
return columnNumber1 - columnNumber2;
}
module.exports = {
SourceMap
};

View File

@@ -124,3 +124,28 @@ const { readFileSync } = require('fs');
assert.strictEqual(originalColumn, knownDecodings[column]);
}
}
// Test that generated columns are sorted when a negative offset is
// observed, see: https://github.com/mozilla/source-map/pull/92
{
function makeMinimalMap(generatedColumns, originalColumns) {
return {
sources: ['test.js'],
// Mapping from the 0th line, ${g}th column of the output file to the 0th
// source file, 0th line, ${column}th column.
mappings: generatedColumns.map((g, i) => `${g}AA${originalColumns[i]}`)
.join(',')
};
}
// U = 10
// F = -2
// A = 0
// E = 2
const sourceMap = new SourceMap(makeMinimalMap(
['U', 'F', 'F'],
['A', 'E', 'E']
));
assert.strictEqual(sourceMap.findEntry(0, 6).originalColumn, 4);
assert.strictEqual(sourceMap.findEntry(0, 8).originalColumn, 2);
assert.strictEqual(sourceMap.findEntry(0, 10).originalColumn, 0);
}