Skip to content

Commit 37233e3

Browse files
committed
ignore invalid mappings when reading a source map
1 parent d103b21 commit 37233e3

File tree

6 files changed

+1224
-25
lines changed

6 files changed

+1224
-25
lines changed

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

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": {

test/SourceMapSource.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const OriginalSource = require("../").OriginalSource;
44
const ConcatSource = require("../").ConcatSource;
55
const ReplaceSource = require("../").ReplaceSource;
66
const SourceNode = require("source-map").SourceNode;
7+
const fs = require("fs");
8+
const path = require("path");
79
const { withReadableMappings } = require("./helpers");
810

911
describe("SourceMapSource", () => {
@@ -207,4 +209,21 @@ describe("SourceMapSource", () => {
207209
}
208210
`);
209211
});
212+
213+
it("should handle es6-promise correctly", () => {
214+
const code = fs.readFileSync(
215+
path.resolve(__dirname, "fixtures", "es6-promise.js"),
216+
"utf-8"
217+
);
218+
const map = JSON.parse(
219+
fs.readFileSync(
220+
path.resolve(__dirname, "fixtures", "es6-promise.map"),
221+
"utf-8"
222+
)
223+
);
224+
const inner = new SourceMapSource(code, "es6-promise.js", map);
225+
const source = new ConcatSource(inner, inner);
226+
expect(source.source()).toBe(code + code);
227+
expect(source.sourceAndMap().source).toBe(code + code);
228+
});
210229
});

0 commit comments

Comments
 (0)