Skip to content

Commit

Permalink
Fix an off-by-1 error in section lookup
Browse files Browse the repository at this point in the history
In an indexed map, the offset line & column are stored 1-based. However, the
lookup for originalPositionFor was not incrementing the 0-based column from the
API argument.
  • Loading branch information
takikawa committed May 28, 2024
1 parent 60adcb0 commit 3789341
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,10 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
return cmp;
}

// The generated column is 0-based, but the section offset column is
// stored 1-based.
return (
aNeedle.generatedColumn - section.generatedOffset.generatedColumn
aNeedle.generatedColumn - (section.generatedOffset.generatedColumn - 1)
);
}
);
Expand Down
9 changes: 9 additions & 0 deletions test/test-source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2183,3 +2183,12 @@ exports["test SourceMapConsumer.with and exceptions"] = async function (
assert.equal(error, 6);
assert.equal(consumer._mappingsPtr, 0);
};

exports["test a mapping at the boundary of indexed source map offset"] =
async function (assert) {
const map = await new SourceMapConsumer(
util.indexedTestMapAtOffsetBoundary
);
util.assertMapping(1, 0, "/the/root/one.js", 1, 0, null, null, map, assert);
map.destroy();
};
25 changes: 25 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ exports.indexedTestMapColumnOffset = {
},
],
};
// This mapping is for testing a case where the mapped position is at the
// section offset.
exports.indexedTestMapAtOffsetBoundary = {
version: 3,
file: "min.js",
sections: [
{
offset: {
line: 0,
column: 0,
},
map: {
version: 3,
sources: ["one.js"],
sourcesContent: [
"ONE.foo = function (bar) {\n return baz(bar);\n };",
],
names: ["bar", "baz"],
mappings: "AAAA",
file: "min.js",
sourceRoot: "/the/root",
},
},
],
};
exports.testMapWithSourcesContent = {
version: 3,
file: "min.js",
Expand Down

0 comments on commit 3789341

Please sign in to comment.