From 31218750a854ccee8908dce75a9913b1b30bfda6 Mon Sep 17 00:00:00 2001 From: Hai Date: Wed, 11 Jun 2025 00:33:59 +0800 Subject: [PATCH 1/3] fix: should generate last column mapping even when source is missing --- lib/ConcatSource.js | 22 +++++------- .../streamChunksOfCombinedSourceMap.js | 6 ++-- test/ConcatSource.js | 36 +++++++++++++++++++ 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/lib/ConcatSource.js b/lib/ConcatSource.js index c393fa1..f6fa4e0 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 = undefined; + // 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..c9bffed 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,39 @@ 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 = { + 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, + }); + }); }); From bac72d1d75a01f8baab794cdc895d86ed2dc2a71 Mon Sep 17 00:00:00 2001 From: Hai Date: Wed, 11 Jun 2025 01:29:54 +0800 Subject: [PATCH 2/3] chore: fix lint --- lib/ConcatSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ConcatSource.js b/lib/ConcatSource.js index f6fa4e0..f10463c 100644 --- a/lib/ConcatSource.js +++ b/lib/ConcatSource.js @@ -212,7 +212,7 @@ class ConcatSource extends Source { ? -1 : nameIndexMapping[nameIndex]; lastMappingLine = resultSourceIndex < 0 ? 0 : generatedLine; - let _chunk = undefined; + 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; From 0b913d790869752bccdd3115cf1d0fe587414a16 Mon Sep 17 00:00:00 2001 From: Hai Date: Wed, 11 Jun 2025 01:36:38 +0800 Subject: [PATCH 3/3] chore: fix lint --- test/ConcatSource.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/ConcatSource.js b/test/ConcatSource.js index c9bffed..f3a4df1 100644 --- a/test/ConcatSource.js +++ b/test/ConcatSource.js @@ -218,6 +218,8 @@ describe("concatSource", () => { ); const innerSource = "ab\nc"; const innerMap = { + names: [], + file: "x", version: 3, sources: ["main.js"], sourcesContent: ["a\nc"],