Skip to content

Commit

Permalink
Merge pull request #127 from webpack/bugfix/invalid-mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra authored Jul 30, 2021
2 parents d103b21 + dfff046 commit 2621df2
Show file tree
Hide file tree
Showing 10 changed files with 1,502 additions and 184 deletions.
11 changes: 0 additions & 11 deletions lib/ConcatSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,6 @@ class ConcatSource extends Source {
(finalSource && lastMappingLine === generatedLine);
currentLineOffset += generatedLine - 1;
}
if (needToCloseMapping) {
onChunk(
undefined,
currentLineOffset + 1,
currentColumnOffset,
-1,
-1,
-1,
-1
);
}
return {
generatedLine: currentLineOffset + 1,
generatedColumn: currentColumnOffset,
Expand Down
2 changes: 1 addition & 1 deletion lib/PrefixSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PrefixSource extends Source {
// for performance reasons)
if (linesOnly || sourceIndex < 0) {
chunk = prefix + chunk;
} else {
} else if (prefixOffset > 0) {
onChunk(prefix, generatedLine, generatedColumn, -1, -1, -1, -1);
generatedColumn += prefixOffset;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/helpers/__mocks__/createMappingsSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const createMappingsSerializer = jest.requireActual(
module.exports = options => {
const fn = createMappingsSerializer(options);
let lastLine = 1;
let lastColumn = 0;
let lastColumn = -1;
return (
generatedLine,
generatedColumn,
Expand All @@ -18,7 +18,7 @@ module.exports = options => {
) => {
if (
generatedLine >= lastLine &&
generatedColumn >= (generatedLine === lastLine ? lastColumn : 0) &&
generatedColumn > (generatedLine === lastLine ? lastColumn : -1) &&
(sourceIndex === -1
? originalLine === -1 && originalColumn === -1 && nameIndex === -1
: sourceIndex >= 0 &&
Expand All @@ -38,8 +38,8 @@ module.exports = options => {
);
}
throw new Error(`Invalid mapping passed to mapping serializer:
generatedLine = ${generatedLine},
generatedColumn = ${generatedColumn},
generatedLine = ${generatedLine} (lastLine = ${lastLine}),
generatedColumn = ${generatedColumn} (lastColumn = ${lastColumn}),
sourceIndex = ${sourceIndex},
originalLine = ${originalLine},
originalColumn = ${originalColumn},
Expand Down
45 changes: 25 additions & 20 deletions lib/helpers/readMappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,43 @@ const readMappings = (mappings, onMapping) => {
let currentValue = 0;
let currentValuePos = 0;
let generatedLine = 1;
let generatedColumn = -1;
for (let i = 0; i < mappings.length; i++) {
const cc = mappings.charCodeAt(i);
if (cc > ccMax) continue;
const value = ccToValue[cc];
if ((value & END_SEGMENT_BIT) !== 0) {
// End current segment
if (currentDataPos === 1) {
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
} else if (currentDataPos === 4) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
-1
);
} else if (currentDataPos === 5) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
currentData[4]
);
if (currentData[0] > generatedColumn) {
if (currentDataPos === 1) {
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
} else if (currentDataPos === 4) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
-1
);
} else if (currentDataPos === 5) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
currentData[4]
);
}
generatedColumn = currentData[0];
}
currentDataPos = 0;
if (value === NEXT_LINE) {
// Start new line
generatedLine++;
currentData[0] = 0;
generatedColumn = -1;
}
} else if ((value & CONTINUATION_BIT) === 0) {
// last sextet
Expand Down
74 changes: 47 additions & 27 deletions lib/helpers/streamChunksOfSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const streamChunksOfSourceMapFull = (
}
}

const lastLine = lines[lines.length - 1];
const lastNewLine = lastLine.endsWith("\n");
const finalLine = lastNewLine ? lines.length + 1 : lines.length;
const finalColumn = lastNewLine ? 0 : lastLine.length;

let currentGeneratedLine = 1;
let currentGeneratedColumn = 0;

Expand Down Expand Up @@ -132,7 +137,11 @@ const streamChunksOfSourceMapFull = (
}
currentGeneratedColumn = generatedColumn;
}
if (sourceIndex >= 0) {
if (
sourceIndex >= 0 &&
(generatedLine < finalLine ||
(generatedLine === finalLine && generatedColumn < finalColumn))
) {
mappingActive = true;
activeMappingSourceIndex = sourceIndex;
activeMappingOriginalLine = originalLine;
Expand All @@ -141,18 +150,11 @@ const streamChunksOfSourceMapFull = (
}
};
readMappings(mappings, onMapping);
onMapping(lines.length + 1, 0, -1, -1, -1, -1);
const lastLine = lines[lines.length - 1];
const lastNewLine = lastLine.endsWith("\n");
return lastNewLine
? {
generatedLine: lines.length + 1,
generatedColumn: 0
}
: {
generatedLine: lines.length,
generatedColumn: lastLine.length
};
onMapping(finalLine, finalColumn, -1, -1, -1, -1);
return {
generatedLine: finalLine,
generatedColumn: finalColumn
};
};

const streamChunksOfSourceMapLinesFull = (
Expand Down Expand Up @@ -188,7 +190,13 @@ const streamChunksOfSourceMapLinesFull = (
originalColumn,
_nameIndex
) => {
if (sourceIndex < 0 || generatedLine < currentGeneratedLine) return;
if (
sourceIndex < 0 ||
generatedLine < currentGeneratedLine ||
generatedLine > lines.length
) {
return;
}
while (generatedLine > currentGeneratedLine) {
if (currentGeneratedLine <= lines.length) {
onChunk(
Expand Down Expand Up @@ -228,17 +236,17 @@ const streamChunksOfSourceMapLinesFull = (
-1
);
}

const lastLine = lines[lines.length - 1];
const lastNewLine = lastLine.endsWith("\n");
return lastNewLine
? {
generatedLine: lines.length + 1,
generatedColumn: 0
}
: {
generatedLine: lines.length,
generatedColumn: lastLine.length
};

const finalLine = lastNewLine ? lines.length + 1 : lines.length;
const finalColumn = lastNewLine ? 0 : lastLine.length;

return {
generatedLine: finalLine,
generatedColumn: finalColumn
};
};

const streamChunksOfSourceMapFinal = (
Expand All @@ -248,6 +256,10 @@ const streamChunksOfSourceMapFinal = (
onSource,
onName
) => {
const result = getGeneratedSourceInfo(source);
const { generatedLine: finalLine, generatedColumn: finalColumn } = result;

if (finalLine === 1 && finalColumn === 0) return result;
const { sources, sourcesContent, names, mappings } = sourceMap;
for (let i = 0; i < sources.length; i++) {
onSource(
Expand All @@ -272,6 +284,12 @@ const streamChunksOfSourceMapFinal = (
originalColumn,
nameIndex
) => {
if (
generatedLine >= finalLine &&
(generatedColumn >= finalColumn || generatedLine > finalLine)
) {
return;
}
if (sourceIndex >= 0) {
onChunk(
undefined,
Expand All @@ -289,7 +307,7 @@ const streamChunksOfSourceMapFinal = (
}
};
readMappings(mappings, onMapping);
return getGeneratedSourceInfo(source);
return result;
};

const streamChunksOfSourceMapLinesFinal = (
Expand All @@ -300,8 +318,8 @@ const streamChunksOfSourceMapLinesFinal = (
_onName
) => {
const result = getGeneratedSourceInfo(source);
const { generatedLine: numberOfLines, generatedColumn } = result;
if (numberOfLines === 1 && generatedColumn === 0) {
const { generatedLine, generatedColumn } = result;
if (generatedLine === 1 && generatedColumn === 0) {
return {
generatedLine: 1,
generatedColumn: 0
Expand All @@ -317,6 +335,8 @@ const streamChunksOfSourceMapLinesFinal = (
);
}

const finalLine = generatedColumn === 0 ? generatedLine - 1 : generatedLine;

let currentGeneratedLine = 1;

const onMapping = (
Expand All @@ -330,7 +350,7 @@ const streamChunksOfSourceMapLinesFinal = (
if (
sourceIndex >= 0 &&
currentGeneratedLine <= generatedLine &&
generatedLine <= numberOfLines
generatedLine <= finalLine
) {
onChunk(
undefined,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"pretest": "yarn lint",
"test": "jest",
"lint": "eslint --cache lib test",
"lint": "eslint --cache lib test/*.js",
"cover": "jest --coverage"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 2621df2

Please # to comment.