fix(ngcc): handle missing sources when flattening source-maps (#35718)

If a package has a source-map but it does not provide
the actual content of the sources, then the source-map
flattening was crashing.

Now we ignore such mappings that have no source
since we are not able to compute the merged
mapping if there is no source file.

Fixes #35709

PR Close #35718
This commit is contained in:
Pete Bacon Darwin 2020-02-27 19:50:14 +00:00 committed by Matias Niemelä
parent 01ab168774
commit 72c4fda613
2 changed files with 28 additions and 1 deletions

View File

@ -290,11 +290,16 @@ export function parseMappings(
const generatedLineMappings = rawMappings[generatedLine]; const generatedLineMappings = rawMappings[generatedLine];
for (const rawMapping of generatedLineMappings) { for (const rawMapping of generatedLineMappings) {
if (rawMapping.length >= 4) { if (rawMapping.length >= 4) {
const originalSource = sources[rawMapping[1] !];
if (originalSource === null || originalSource === undefined) {
// the original source is missing so ignore this mapping
continue;
}
const generatedColumn = rawMapping[0]; const generatedColumn = rawMapping[0];
const name = rawMapping.length === 5 ? rawMap.names[rawMapping[4]] : undefined; const name = rawMapping.length === 5 ? rawMap.names[rawMapping[4]] : undefined;
const mapping: Mapping = { const mapping: Mapping = {
generatedSegment: {line: generatedLine, column: generatedColumn}, generatedSegment: {line: generatedLine, column: generatedColumn},
originalSource: sources[rawMapping[1] !] !, originalSource,
originalSegment: {line: rawMapping[2] !, column: rawMapping[3] !}, name originalSegment: {line: rawMapping[2] !, column: rawMapping[3] !}, name
}; };
mappings.push(mapping); mappings.push(mapping);

View File

@ -174,6 +174,28 @@ runInEachFileSystem(() => {
}, },
]); ]);
}); });
it('should ignore mappings to missing source files', () => {
const bSourceMap: RawSourceMap = {
mappings: encode([[[1, 0, 0, 0], [4, 0, 0, 3], [4, 0, 0, 6], [5, 0, 0, 7]]]),
names: [],
sources: ['c.js'],
version: 3
};
const bSource = new SourceFile(_('/foo/src/b.js'), 'abcdef', bSourceMap, false, [null]);
const aSourceMap: RawSourceMap = {
mappings: encode([[[0, 0, 0, 0], [2, 0, 0, 3], [4, 0, 0, 2], [5, 0, 0, 5]]]),
names: [],
sources: ['b.js'],
version: 3
};
const aSource =
new SourceFile(_('/foo/src/a.js'), 'abdecf', aSourceMap, false, [bSource]);
// These flattened mappings are just the mappings from a to b.
// (The mappings to c are dropped since there is no source file to map to.)
expect(aSource.flattenedMappings).toEqual(parseMappings(aSourceMap, [bSource]));
});
}); });
describe('renderFlattenedSourceMap()', () => { describe('renderFlattenedSourceMap()', () => {