source: frontend/node_modules/sucrase/dist/esm/computeSourceMap.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import {GenMapping, maybeAddSegment, toEncodedMap} from "@jridgewell/gen-mapping";
2
3
4
5import {charCodes} from "./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 */
22export default 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 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 maybeAddSegment(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 maybeAddSegment(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.lineFeed) {
58 line++;
59 lineStart = i + 1;
60 if (currentMapping !== lineStart) {
61 maybeAddSegment(map, line, 0, filePath, line, 0);
62 }
63 }
64 }
65 const {sourceRoot, sourcesContent, ...sourceMap} = toEncodedMap(map);
66 return sourceMap ;
67}
68
69/**
70 * Create an array mapping each token index to the 0-based column of the start
71 * position of the token.
72 */
73function 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.lineFeed) {
85 lineStart = i + 1;
86 }
87 }
88 return sourceColumns;
89}
Note: See TracBrowser for help on using the repository browser.