diff --git a/lib/ConcatSource.js b/lib/ConcatSource.js index c393fa1..f10463c 100644 --- a/lib/ConcatSource.js +++ b/lib/ConcatSource.js @@ -212,24 +212,18 @@ class ConcatSource extends Source { ? -1 : nameIndexMapping[nameIndex]; lastMappingLine = resultSourceIndex < 0 ? 0 : generatedLine; + let _chunk; + // When using finalSource, we process the entire source code at once at the end, rather than chunk by chunk if (finalSource) { if (chunk !== undefined) code += chunk; - if (resultSourceIndex >= 0) { - onChunk( - undefined, - line, - column, - resultSourceIndex, - originalLine, - originalColumn, - resultNameIndex, - ); - } - } else if (resultSourceIndex < 0) { - onChunk(chunk, line, column, -1, -1, -1, -1); + } else { + _chunk = chunk; + } + if (resultSourceIndex < 0) { + onChunk(_chunk, line, column, -1, -1, -1, -1); } else { onChunk( - chunk, + _chunk, line, column, resultSourceIndex, diff --git a/lib/helpers/streamChunksOfCombinedSourceMap.js b/lib/helpers/streamChunksOfCombinedSourceMap.js index 8a2fd2b..d1355f1 100644 --- a/lib/helpers/streamChunksOfCombinedSourceMap.js +++ b/lib/helpers/streamChunksOfCombinedSourceMap.js @@ -51,7 +51,7 @@ const streamChunksOfCombinedSourceMap = ( const nameIndexMapping = []; /** @type {string[]} */ const nameIndexValueMapping = []; - let innerSourceIndex = -2; + let outerSourceIndex = -2; /** @type {number[]} */ const innerSourceIndexMapping = []; /** @type {[string | null, string | undefined][]} */ @@ -101,7 +101,7 @@ const streamChunksOfCombinedSourceMap = ( nameIndex, ) => { // Check if this is a mapping to the inner source - if (sourceIndex === innerSourceIndex) { + if (sourceIndex === outerSourceIndex) { // Check if there is a mapping in the inner source const idx = findInnerMapping(originalLine, originalColumn); if (idx !== -1) { @@ -299,7 +299,7 @@ const streamChunksOfCombinedSourceMap = ( }, (i, source, sourceContent) => { if (source === innerSourceName) { - innerSourceIndex = i; + outerSourceIndex = i; if (innerSource !== undefined) sourceContent = innerSource; else innerSource = /** @type {string} */ (sourceContent); sourceIndexMapping[i] = -2; diff --git a/test/ConcatSource.js b/test/ConcatSource.js index dcf8233..f3a4df1 100644 --- a/test/ConcatSource.js +++ b/test/ConcatSource.js @@ -5,6 +5,7 @@ jest.mock("./__mocks__/createMappingsSerializer"); const { ConcatSource } = require("../"); const { RawSource } = require("../"); const { OriginalSource } = require("../"); +const { SourceMapSource } = require("../"); const { withReadableMappings } = require("./helpers"); describe("concatSource", () => { @@ -210,4 +211,41 @@ describe("concatSource", () => { } `); }); + + it("should handle column mapping correctly with missing sources", () => { + const source = new ConcatSource( + "/*! For license information please see main.js.LICENSE.txt */", + ); + const innerSource = "ab\nc"; + const innerMap = { + names: [], + file: "x", + version: 3, + sources: ["main.js"], + sourcesContent: ["a\nc"], + mappings: "AAAA,CCAA;ADCA", + // ______________↑ The column mapping (CCAA) references one missing source + }; + source.add(new SourceMapSource(innerSource, "main.js", innerMap)); + const expected = { + source: + "/*! For license information please see main.js.LICENSE.txt */ab\nc", + map: { + version: 3, + file: "x", + mappings: "6DAAA,C;AACA", + sources: ["main.js"], + sourcesContent: ["a\nc"], + names: [], + }, + }; + expect( + source.sourceAndMap({ + columns: true, + }), + ).toEqual({ + source: expected.source, + map: expected.map, + }); + }); });