Skip to content

fix: should generate last column mapping even when source is missing #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions lib/ConcatSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions lib/helpers/streamChunksOfCombinedSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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][]} */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions test/ConcatSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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,
});
});
});