Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: port source map sorting logic from chromium #31927

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/internal/source_map/source_map.js
Expand Up @@ -212,6 +212,7 @@ class SourceMap {
let sourceIndex = 0;
let sourceLineNumber = 0;
let sourceColumnNumber = 0;
let hadNegativeColumnOffset = false;

const sources = [];
const originalToCanonicalURLMap = {};
Expand Down Expand Up @@ -241,7 +242,13 @@ class SourceMap {
break;
}

columnNumber += decodeVLQ(stringCharIterator);
// A negative columnOffset is valid, and if one is observed sorting is
// necessary, see: https://github.com/mozilla/source-map/pull/92
const columnOffset = decodeVLQ(stringCharIterator);
if (columnOffset < 0) {
hadNegativeColumnOffset = true;
}
columnNumber += columnOffset;
if (isSeparator(stringCharIterator.peek())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much can we clean up this code? The whole iterator string iterator wrapper thing is icky to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've diverged quite a bit form the V8 implementation at this point, introducing private fields as an example. I probably wouldn't do it in this PR, but I think you could go ahead and refactor as much as you see fit.

this.#mappings.push([lineNumber, columnNumber]);
continue;
Expand All @@ -261,6 +268,9 @@ class SourceMap {
this.#mappings.push([lineNumber, columnNumber, sourceURL,
sourceLineNumber, sourceColumnNumber]);
}
if (hadNegativeColumnOffset) {
this.#mappings.sort(compareSourceMapEntry);
}
};
}

Expand Down Expand Up @@ -321,6 +331,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
};
25 changes: 25 additions & 0 deletions test/parallel/test-source-map-api.js
Expand Up @@ -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, 0th column of the output file to the 0th
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Mapping from the 0th line, 0th column of the output file to the 0th
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

assert.strictEqual(sourceMap.findEntry(0, 8).originalColumn, 2);
assert.strictEqual(sourceMap.findEntry(0, 10).originalColumn, 0);
}