Skip to content

Commit

Permalink
Merge pull request #507 from takikawa/fix-section-lookup-off-by-one
Browse files Browse the repository at this point in the history
Fix an off-by-1 error in section lookup
  • Loading branch information
bomsy committed May 30, 2024
2 parents 60adcb0 + 79aa958 commit 43819cc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,11 @@ 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 43819cc

Please sign in to comment.