Skip to content

Commit b19b923

Browse files
authored
fix: generate last column mapping even when source is missing for ConcatSource
1 parent b2db929 commit b19b923

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

lib/ConcatSource.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,18 @@ class ConcatSource extends Source {
212212
? -1
213213
: nameIndexMapping[nameIndex];
214214
lastMappingLine = resultSourceIndex < 0 ? 0 : generatedLine;
215+
let _chunk;
216+
// When using finalSource, we process the entire source code at once at the end, rather than chunk by chunk
215217
if (finalSource) {
216218
if (chunk !== undefined) code += chunk;
217-
if (resultSourceIndex >= 0) {
218-
onChunk(
219-
undefined,
220-
line,
221-
column,
222-
resultSourceIndex,
223-
originalLine,
224-
originalColumn,
225-
resultNameIndex,
226-
);
227-
}
228-
} else if (resultSourceIndex < 0) {
229-
onChunk(chunk, line, column, -1, -1, -1, -1);
219+
} else {
220+
_chunk = chunk;
221+
}
222+
if (resultSourceIndex < 0) {
223+
onChunk(_chunk, line, column, -1, -1, -1, -1);
230224
} else {
231225
onChunk(
232-
chunk,
226+
_chunk,
233227
line,
234228
column,
235229
resultSourceIndex,

lib/helpers/streamChunksOfCombinedSourceMap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const streamChunksOfCombinedSourceMap = (
5151
const nameIndexMapping = [];
5252
/** @type {string[]} */
5353
const nameIndexValueMapping = [];
54-
let innerSourceIndex = -2;
54+
let outerSourceIndex = -2;
5555
/** @type {number[]} */
5656
const innerSourceIndexMapping = [];
5757
/** @type {[string | null, string | undefined][]} */
@@ -101,7 +101,7 @@ const streamChunksOfCombinedSourceMap = (
101101
nameIndex,
102102
) => {
103103
// Check if this is a mapping to the inner source
104-
if (sourceIndex === innerSourceIndex) {
104+
if (sourceIndex === outerSourceIndex) {
105105
// Check if there is a mapping in the inner source
106106
const idx = findInnerMapping(originalLine, originalColumn);
107107
if (idx !== -1) {
@@ -299,7 +299,7 @@ const streamChunksOfCombinedSourceMap = (
299299
},
300300
(i, source, sourceContent) => {
301301
if (source === innerSourceName) {
302-
innerSourceIndex = i;
302+
outerSourceIndex = i;
303303
if (innerSource !== undefined) sourceContent = innerSource;
304304
else innerSource = /** @type {string} */ (sourceContent);
305305
sourceIndexMapping[i] = -2;

test/ConcatSource.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ jest.mock("./__mocks__/createMappingsSerializer");
55
const { ConcatSource } = require("../");
66
const { RawSource } = require("../");
77
const { OriginalSource } = require("../");
8+
const { SourceMapSource } = require("../");
89
const { withReadableMappings } = require("./helpers");
910

1011
describe("concatSource", () => {
@@ -210,4 +211,41 @@ describe("concatSource", () => {
210211
}
211212
`);
212213
});
214+
215+
it("should handle column mapping correctly with missing sources", () => {
216+
const source = new ConcatSource(
217+
"/*! For license information please see main.js.LICENSE.txt */",
218+
);
219+
const innerSource = "ab\nc";
220+
const innerMap = {
221+
names: [],
222+
file: "x",
223+
version: 3,
224+
sources: ["main.js"],
225+
sourcesContent: ["a\nc"],
226+
mappings: "AAAA,CCAA;ADCA",
227+
// ______________↑ The column mapping (CCAA) references one missing source
228+
};
229+
source.add(new SourceMapSource(innerSource, "main.js", innerMap));
230+
const expected = {
231+
source:
232+
"/*! For license information please see main.js.LICENSE.txt */ab\nc",
233+
map: {
234+
version: 3,
235+
file: "x",
236+
mappings: "6DAAA,C;AACA",
237+
sources: ["main.js"],
238+
sourcesContent: ["a\nc"],
239+
names: [],
240+
},
241+
};
242+
expect(
243+
source.sourceAndMap({
244+
columns: true,
245+
}),
246+
).toEqual({
247+
source: expected.source,
248+
map: expected.map,
249+
});
250+
});
213251
});

0 commit comments

Comments
 (0)