| 1 | "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _genmapping = require('@jridgewell/gen-mapping');
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | var _charcodes = require('./parser/util/charcodes');
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Generate a source map indicating that each line maps directly to the original line,
|
|---|
| 20 | * with the tokens in their new positions.
|
|---|
| 21 | */
|
|---|
| 22 | function computeSourceMap(
|
|---|
| 23 | {code: generatedCode, mappings: rawMappings},
|
|---|
| 24 | filePath,
|
|---|
| 25 | options,
|
|---|
| 26 | source,
|
|---|
| 27 | tokens,
|
|---|
| 28 | ) {
|
|---|
| 29 | const sourceColumns = computeSourceColumns(source, tokens);
|
|---|
| 30 | const map = new (0, _genmapping.GenMapping)({file: options.compiledFilename});
|
|---|
| 31 | let tokenIndex = 0;
|
|---|
| 32 | // currentMapping is the output source index for the current input token being
|
|---|
| 33 | // considered.
|
|---|
| 34 | let currentMapping = rawMappings[0];
|
|---|
| 35 | while (currentMapping === undefined && tokenIndex < rawMappings.length - 1) {
|
|---|
| 36 | tokenIndex++;
|
|---|
| 37 | currentMapping = rawMappings[tokenIndex];
|
|---|
| 38 | }
|
|---|
| 39 | let line = 0;
|
|---|
| 40 | let lineStart = 0;
|
|---|
| 41 | if (currentMapping !== lineStart) {
|
|---|
| 42 | _genmapping.maybeAddSegment.call(void 0, map, line, 0, filePath, line, 0);
|
|---|
| 43 | }
|
|---|
| 44 | for (let i = 0; i < generatedCode.length; i++) {
|
|---|
| 45 | if (i === currentMapping) {
|
|---|
| 46 | const genColumn = currentMapping - lineStart;
|
|---|
| 47 | const sourceColumn = sourceColumns[tokenIndex];
|
|---|
| 48 | _genmapping.maybeAddSegment.call(void 0, map, line, genColumn, filePath, line, sourceColumn);
|
|---|
| 49 | while (
|
|---|
| 50 | (currentMapping === i || currentMapping === undefined) &&
|
|---|
| 51 | tokenIndex < rawMappings.length - 1
|
|---|
| 52 | ) {
|
|---|
| 53 | tokenIndex++;
|
|---|
| 54 | currentMapping = rawMappings[tokenIndex];
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | if (generatedCode.charCodeAt(i) === _charcodes.charCodes.lineFeed) {
|
|---|
| 58 | line++;
|
|---|
| 59 | lineStart = i + 1;
|
|---|
| 60 | if (currentMapping !== lineStart) {
|
|---|
| 61 | _genmapping.maybeAddSegment.call(void 0, map, line, 0, filePath, line, 0);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | const {sourceRoot, sourcesContent, ...sourceMap} = _genmapping.toEncodedMap.call(void 0, map);
|
|---|
| 66 | return sourceMap ;
|
|---|
| 67 | } exports.default = computeSourceMap;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Create an array mapping each token index to the 0-based column of the start
|
|---|
| 71 | * position of the token.
|
|---|
| 72 | */
|
|---|
| 73 | function computeSourceColumns(code, tokens) {
|
|---|
| 74 | const sourceColumns = new Array(tokens.length);
|
|---|
| 75 | let tokenIndex = 0;
|
|---|
| 76 | let currentMapping = tokens[tokenIndex].start;
|
|---|
| 77 | let lineStart = 0;
|
|---|
| 78 | for (let i = 0; i < code.length; i++) {
|
|---|
| 79 | if (i === currentMapping) {
|
|---|
| 80 | sourceColumns[tokenIndex] = currentMapping - lineStart;
|
|---|
| 81 | tokenIndex++;
|
|---|
| 82 | currentMapping = tokens[tokenIndex].start;
|
|---|
| 83 | }
|
|---|
| 84 | if (code.charCodeAt(i) === _charcodes.charCodes.lineFeed) {
|
|---|
| 85 | lineStart = i + 1;
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | return sourceColumns;
|
|---|
| 89 | }
|
|---|