Skip to content

Commit 2621df2

Browse files
authored
Merge pull request #127 from webpack/bugfix/invalid-mappings
2 parents d103b21 + dfff046 commit 2621df2

File tree

10 files changed

+1502
-184
lines changed

10 files changed

+1502
-184
lines changed

lib/ConcatSource.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,6 @@ class ConcatSource extends Source {
221221
(finalSource && lastMappingLine === generatedLine);
222222
currentLineOffset += generatedLine - 1;
223223
}
224-
if (needToCloseMapping) {
225-
onChunk(
226-
undefined,
227-
currentLineOffset + 1,
228-
currentColumnOffset,
229-
-1,
230-
-1,
231-
-1,
232-
-1
233-
);
234-
}
235224
return {
236225
generatedLine: currentLineOffset + 1,
237226
generatedColumn: currentColumnOffset,

lib/PrefixSource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class PrefixSource extends Source {
7171
// for performance reasons)
7272
if (linesOnly || sourceIndex < 0) {
7373
chunk = prefix + chunk;
74-
} else {
74+
} else if (prefixOffset > 0) {
7575
onChunk(prefix, generatedLine, generatedColumn, -1, -1, -1, -1);
7676
generatedColumn += prefixOffset;
7777
}

lib/helpers/__mocks__/createMappingsSerializer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const createMappingsSerializer = jest.requireActual(
77
module.exports = options => {
88
const fn = createMappingsSerializer(options);
99
let lastLine = 1;
10-
let lastColumn = 0;
10+
let lastColumn = -1;
1111
return (
1212
generatedLine,
1313
generatedColumn,
@@ -18,7 +18,7 @@ module.exports = options => {
1818
) => {
1919
if (
2020
generatedLine >= lastLine &&
21-
generatedColumn >= (generatedLine === lastLine ? lastColumn : 0) &&
21+
generatedColumn > (generatedLine === lastLine ? lastColumn : -1) &&
2222
(sourceIndex === -1
2323
? originalLine === -1 && originalColumn === -1 && nameIndex === -1
2424
: sourceIndex >= 0 &&
@@ -38,8 +38,8 @@ module.exports = options => {
3838
);
3939
}
4040
throw new Error(`Invalid mapping passed to mapping serializer:
41-
generatedLine = ${generatedLine},
42-
generatedColumn = ${generatedColumn},
41+
generatedLine = ${generatedLine} (lastLine = ${lastLine}),
42+
generatedColumn = ${generatedColumn} (lastColumn = ${lastColumn}),
4343
sourceIndex = ${sourceIndex},
4444
originalLine = ${originalLine},
4545
originalColumn = ${originalColumn},

lib/helpers/readMappings.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,43 @@ const readMappings = (mappings, onMapping) => {
3838
let currentValue = 0;
3939
let currentValuePos = 0;
4040
let generatedLine = 1;
41+
let generatedColumn = -1;
4142
for (let i = 0; i < mappings.length; i++) {
4243
const cc = mappings.charCodeAt(i);
4344
if (cc > ccMax) continue;
4445
const value = ccToValue[cc];
4546
if ((value & END_SEGMENT_BIT) !== 0) {
4647
// End current segment
47-
if (currentDataPos === 1) {
48-
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
49-
} else if (currentDataPos === 4) {
50-
onMapping(
51-
generatedLine,
52-
currentData[0],
53-
currentData[1],
54-
currentData[2],
55-
currentData[3],
56-
-1
57-
);
58-
} else if (currentDataPos === 5) {
59-
onMapping(
60-
generatedLine,
61-
currentData[0],
62-
currentData[1],
63-
currentData[2],
64-
currentData[3],
65-
currentData[4]
66-
);
48+
if (currentData[0] > generatedColumn) {
49+
if (currentDataPos === 1) {
50+
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
51+
} else if (currentDataPos === 4) {
52+
onMapping(
53+
generatedLine,
54+
currentData[0],
55+
currentData[1],
56+
currentData[2],
57+
currentData[3],
58+
-1
59+
);
60+
} else if (currentDataPos === 5) {
61+
onMapping(
62+
generatedLine,
63+
currentData[0],
64+
currentData[1],
65+
currentData[2],
66+
currentData[3],
67+
currentData[4]
68+
);
69+
}
70+
generatedColumn = currentData[0];
6771
}
6872
currentDataPos = 0;
6973
if (value === NEXT_LINE) {
7074
// Start new line
7175
generatedLine++;
7276
currentData[0] = 0;
77+
generatedColumn = -1;
7378
}
7479
} else if ((value & CONTINUATION_BIT) === 0) {
7580
// last sextet

lib/helpers/streamChunksOfSourceMap.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ const streamChunksOfSourceMapFull = (
3939
}
4040
}
4141

42+
const lastLine = lines[lines.length - 1];
43+
const lastNewLine = lastLine.endsWith("\n");
44+
const finalLine = lastNewLine ? lines.length + 1 : lines.length;
45+
const finalColumn = lastNewLine ? 0 : lastLine.length;
46+
4247
let currentGeneratedLine = 1;
4348
let currentGeneratedColumn = 0;
4449

@@ -132,7 +137,11 @@ const streamChunksOfSourceMapFull = (
132137
}
133138
currentGeneratedColumn = generatedColumn;
134139
}
135-
if (sourceIndex >= 0) {
140+
if (
141+
sourceIndex >= 0 &&
142+
(generatedLine < finalLine ||
143+
(generatedLine === finalLine && generatedColumn < finalColumn))
144+
) {
136145
mappingActive = true;
137146
activeMappingSourceIndex = sourceIndex;
138147
activeMappingOriginalLine = originalLine;
@@ -141,18 +150,11 @@ const streamChunksOfSourceMapFull = (
141150
}
142151
};
143152
readMappings(mappings, onMapping);
144-
onMapping(lines.length + 1, 0, -1, -1, -1, -1);
145-
const lastLine = lines[lines.length - 1];
146-
const lastNewLine = lastLine.endsWith("\n");
147-
return lastNewLine
148-
? {
149-
generatedLine: lines.length + 1,
150-
generatedColumn: 0
151-
}
152-
: {
153-
generatedLine: lines.length,
154-
generatedColumn: lastLine.length
155-
};
153+
onMapping(finalLine, finalColumn, -1, -1, -1, -1);
154+
return {
155+
generatedLine: finalLine,
156+
generatedColumn: finalColumn
157+
};
156158
};
157159

158160
const streamChunksOfSourceMapLinesFull = (
@@ -188,7 +190,13 @@ const streamChunksOfSourceMapLinesFull = (
188190
originalColumn,
189191
_nameIndex
190192
) => {
191-
if (sourceIndex < 0 || generatedLine < currentGeneratedLine) return;
193+
if (
194+
sourceIndex < 0 ||
195+
generatedLine < currentGeneratedLine ||
196+
generatedLine > lines.length
197+
) {
198+
return;
199+
}
192200
while (generatedLine > currentGeneratedLine) {
193201
if (currentGeneratedLine <= lines.length) {
194202
onChunk(
@@ -228,17 +236,17 @@ const streamChunksOfSourceMapLinesFull = (
228236
-1
229237
);
230238
}
239+
231240
const lastLine = lines[lines.length - 1];
232241
const lastNewLine = lastLine.endsWith("\n");
233-
return lastNewLine
234-
? {
235-
generatedLine: lines.length + 1,
236-
generatedColumn: 0
237-
}
238-
: {
239-
generatedLine: lines.length,
240-
generatedColumn: lastLine.length
241-
};
242+
243+
const finalLine = lastNewLine ? lines.length + 1 : lines.length;
244+
const finalColumn = lastNewLine ? 0 : lastLine.length;
245+
246+
return {
247+
generatedLine: finalLine,
248+
generatedColumn: finalColumn
249+
};
242250
};
243251

244252
const streamChunksOfSourceMapFinal = (
@@ -248,6 +256,10 @@ const streamChunksOfSourceMapFinal = (
248256
onSource,
249257
onName
250258
) => {
259+
const result = getGeneratedSourceInfo(source);
260+
const { generatedLine: finalLine, generatedColumn: finalColumn } = result;
261+
262+
if (finalLine === 1 && finalColumn === 0) return result;
251263
const { sources, sourcesContent, names, mappings } = sourceMap;
252264
for (let i = 0; i < sources.length; i++) {
253265
onSource(
@@ -272,6 +284,12 @@ const streamChunksOfSourceMapFinal = (
272284
originalColumn,
273285
nameIndex
274286
) => {
287+
if (
288+
generatedLine >= finalLine &&
289+
(generatedColumn >= finalColumn || generatedLine > finalLine)
290+
) {
291+
return;
292+
}
275293
if (sourceIndex >= 0) {
276294
onChunk(
277295
undefined,
@@ -289,7 +307,7 @@ const streamChunksOfSourceMapFinal = (
289307
}
290308
};
291309
readMappings(mappings, onMapping);
292-
return getGeneratedSourceInfo(source);
310+
return result;
293311
};
294312

295313
const streamChunksOfSourceMapLinesFinal = (
@@ -300,8 +318,8 @@ const streamChunksOfSourceMapLinesFinal = (
300318
_onName
301319
) => {
302320
const result = getGeneratedSourceInfo(source);
303-
const { generatedLine: numberOfLines, generatedColumn } = result;
304-
if (numberOfLines === 1 && generatedColumn === 0) {
321+
const { generatedLine, generatedColumn } = result;
322+
if (generatedLine === 1 && generatedColumn === 0) {
305323
return {
306324
generatedLine: 1,
307325
generatedColumn: 0
@@ -317,6 +335,8 @@ const streamChunksOfSourceMapLinesFinal = (
317335
);
318336
}
319337

338+
const finalLine = generatedColumn === 0 ? generatedLine - 1 : generatedLine;
339+
320340
let currentGeneratedLine = 1;
321341

322342
const onMapping = (
@@ -330,7 +350,7 @@ const streamChunksOfSourceMapLinesFinal = (
330350
if (
331351
sourceIndex >= 0 &&
332352
currentGeneratedLine <= generatedLine &&
333-
generatedLine <= numberOfLines
353+
generatedLine <= finalLine
334354
) {
335355
onChunk(
336356
undefined,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"pretest": "yarn lint",
88
"test": "jest",
9-
"lint": "eslint --cache lib test",
9+
"lint": "eslint --cache lib test/*.js",
1010
"cover": "jest --coverage"
1111
},
1212
"devDependencies": {

0 commit comments

Comments
 (0)