Skip to content

Commit 43819cc

Browse files
authored
Merge pull request #507 from takikawa/fix-section-lookup-off-by-one
Fix an off-by-1 error in section lookup
2 parents 60adcb0 + 79aa958 commit 43819cc

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

lib/source-map-consumer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,11 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
842842
return cmp;
843843
}
844844

845+
// The generated column is 0-based, but the section offset column is
846+
// stored 1-based.
845847
return (
846-
aNeedle.generatedColumn - section.generatedOffset.generatedColumn
848+
aNeedle.generatedColumn -
849+
(section.generatedOffset.generatedColumn - 1)
847850
);
848851
}
849852
);

test/test-source-map-consumer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,3 +2183,12 @@ exports["test SourceMapConsumer.with and exceptions"] = async function (
21832183
assert.equal(error, 6);
21842184
assert.equal(consumer._mappingsPtr, 0);
21852185
};
2186+
2187+
exports["test a mapping at the boundary of indexed source map offset"] =
2188+
async function (assert) {
2189+
const map = await new SourceMapConsumer(
2190+
util.indexedTestMapAtOffsetBoundary
2191+
);
2192+
util.assertMapping(1, 0, "/the/root/one.js", 1, 0, null, null, map, assert);
2193+
map.destroy();
2194+
};

test/util.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,31 @@ exports.indexedTestMapColumnOffset = {
226226
},
227227
],
228228
};
229+
// This mapping is for testing a case where the mapped position is at the
230+
// section offset.
231+
exports.indexedTestMapAtOffsetBoundary = {
232+
version: 3,
233+
file: "min.js",
234+
sections: [
235+
{
236+
offset: {
237+
line: 0,
238+
column: 0,
239+
},
240+
map: {
241+
version: 3,
242+
sources: ["one.js"],
243+
sourcesContent: [
244+
"ONE.foo = function (bar) {\n return baz(bar);\n };",
245+
],
246+
names: ["bar", "baz"],
247+
mappings: "AAAA",
248+
file: "min.js",
249+
sourceRoot: "/the/root",
250+
},
251+
},
252+
],
253+
};
229254
exports.testMapWithSourcesContent = {
230255
version: 3,
231256
file: "min.js",

0 commit comments

Comments
 (0)