diff --git a/lib/source-map-consumer.js b/lib/source-map-consumer.js index 17ef92b1..8fc66f11 100644 --- a/lib/source-map-consumer.js +++ b/lib/source-map-consumer.js @@ -939,14 +939,26 @@ class IndexedSourceMapConsumer extends SourceMapConsumer { } allGeneratedPositionsFor(aArgs) { - const index = this._findSectionIndex(util.getArg(aArgs, "source")); - const section = index >= 0 ? this._sections[index] : null; - const nextSection = - index >= 0 && index + 1 < this._sections.length - ? this._sections[index + 1] - : null; + const source = util.getArg(aArgs, "source"); + let positions = []; + + // The same original source can appear in more than one section, so + // collect the positions from every section that contains it. + for (let i = 0; i < this._sections.length; i++) { + if (this._sections[i].consumer._findSourceIndex(source) !== -1) { + positions = positions.concat( + this._sectionGeneratedPositionsFor(i, aArgs) + ); + } + } - if (!section) return []; + return positions; + } + + _sectionGeneratedPositionsFor(index, aArgs) { + const section = this._sections[index]; + const nextSection = + index + 1 < this._sections.length ? this._sections[index + 1] : null; return section.consumer .allGeneratedPositionsFor(aArgs) diff --git a/test/test-source-map-consumer.js b/test/test-source-map-consumer.js index 8cf50988..a1e2cdff 100644 --- a/test/test-source-map-consumer.js +++ b/test/test-source-map-consumer.js @@ -1490,6 +1490,56 @@ exports["test allGeneratedPositionsFor for index map"] = async function ( map.destroy(); }; +exports[ + "test allGeneratedPositionsFor for index map with a source in several sections" +] = async function (assert) { + const map = await new SourceMapConsumer({ + version: 3, + file: "min.js", + sections: [ + { + offset: { line: 0, column: 0 }, + map: { + version: 3, + sources: ["shared.js"], + names: [], + mappings: "AAAA", + }, + }, + { + offset: { line: 2, column: 4 }, + map: { + version: 3, + sources: ["shared.js"], + names: [], + mappings: "AAAA", + }, + }, + ], + }); + + const mappings = map.allGeneratedPositionsFor({ + line: 1, + column: 0, + source: "shared.js", + }); + + assert.deepEqual(mappings, [ + { + line: 1, + column: 0, + lastColumn: null, + }, + { + line: 3, + column: 4, + lastColumn: null, + }, + ]); + + map.destroy(); +}; + exports["test computeColumnSpans"] = async function (assert) { let map = new SourceMapGenerator({ file: "generated.js",