Skip to content

Commit 830cffb

Browse files
committed
add basic support for maps with sections
1 parent 7faaca2 commit 830cffb

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

code.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,92 @@
451451
throw new Error('Invalid source map');
452452
}
453453

454+
if (json.sections instanceof Array) {
455+
const sections = json.sections;
456+
const decodedSections = [];
457+
let totalDataLength = 0;
458+
459+
for (let i = 0; i < sections.length; i++) {
460+
const { offset: { line, column }, map } = sections[i];
461+
if (typeof line !== 'number' || typeof column !== 'number') {
462+
showLoadingError(`The imported source map is invalid. Expected the "offset" field for section ${i} to have a line and column.`);
463+
throw new Error('Invalid source map');
464+
}
465+
466+
if (!map) {
467+
showLoadingError(`The imported source map is unsupported. Section ${i} does not contain a "map" field.`);
468+
throw new Error('Invalid source map');
469+
}
470+
471+
if (map.version !== 3) {
472+
showLoadingError(`The imported source map is invalid. Expected the "version" field for section ${i} to contain the number 3.`);
473+
throw new Error('Invalid source map');
474+
}
475+
476+
if (!(map.sources instanceof Array) || map.sources.some(x => typeof x !== 'string')) {
477+
showLoadingError(`The imported source map is invalid. Expected the "sources" field for section ${i} to be an array of strings.`);
478+
throw new Error('Invalid source map');
479+
}
480+
481+
if (typeof map.mappings !== 'string') {
482+
showLoadingError(`The imported source map is invalid. Expected the "mappings" field for section ${i} to be a string.`);
483+
throw new Error('Invalid source map');
484+
}
485+
486+
const { sources, sourcesContent, names, mappings } = map;
487+
const emptyData = new Int32Array(0);
488+
for (let i = 0; i < sources.length; i++) {
489+
sources[i] = {
490+
name: sources[i],
491+
content: sourcesContent && sourcesContent[i] || '',
492+
data: emptyData,
493+
dataLength: 0,
494+
};
495+
}
496+
497+
const data = decodeMappings(mappings, sources.length, names.length);
498+
decodedSections.push({ offset: { line, column }, sources, names, data });
499+
totalDataLength += data.length;
500+
}
501+
502+
decodedSections.sort((a, b) => {
503+
if (a.offset.line < b.offset.line) return -1;
504+
if (a.offset.line > b.offset.line) return 1;
505+
if (a.offset.column < b.offset.column) return -1;
506+
if (a.offset.column > b.offset.column) return 1;
507+
return 0;
508+
});
509+
510+
const mergedData = new Int32Array(totalDataLength);
511+
const mergedSources = [];
512+
const mergedNames = [];
513+
let dataOffset = 0;
514+
515+
for (const { offset: { line, column }, sources, names, data } of decodedSections) {
516+
const sourcesOffset = mergedSources.length;
517+
const nameOffset = mergedNames.length;
518+
519+
for (let i = 0, n = data.length; i < n; i += 6) {
520+
if (data[i] === 0) data[i + 1] += column;
521+
data[i] += line;
522+
if (data[i + 2] !== -1) data[i + 2] += sourcesOffset;
523+
if (data[i + 5] !== -1) data[i + 5] += nameOffset;
524+
}
525+
526+
mergedData.set(data, dataOffset);
527+
for (const source of sources) mergedSources.push(source);
528+
for (const name of names) mergedNames.push(name);
529+
dataOffset += data.length;
530+
}
531+
532+
generateInverseMappings(mergedSources, mergedData);
533+
return {
534+
sources: mergedSources,
535+
names: mergedNames,
536+
data: mergedData,
537+
};
538+
}
539+
454540
if (!(json.sources instanceof Array) || json.sources.some(x => typeof x !== 'string')) {
455541
showLoadingError(`The imported source map is invalid. Expected the "sources" field to be an array of strings.`);
456542
throw new Error('Invalid source map');

0 commit comments

Comments
 (0)