source: frontend/node_modules/@jridgewell/remapping/src/source-map-tree.ts

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: 4.7 KB
Line 
1import { GenMapping, maybeAddSegment, setIgnore, setSourceContent } from '@jridgewell/gen-mapping';
2import { traceSegment, decodedMappings } from '@jridgewell/trace-mapping';
3
4import type { TraceMap } from '@jridgewell/trace-mapping';
5
6export type SourceMapSegmentObject = {
7 column: number;
8 line: number;
9 name: string;
10 source: string;
11 content: string | null;
12 ignore: boolean;
13};
14
15export type OriginalSource = {
16 map: null;
17 sources: Sources[];
18 source: string;
19 content: string | null;
20 ignore: boolean;
21};
22
23export type MapSource = {
24 map: TraceMap;
25 sources: Sources[];
26 source: string;
27 content: null;
28 ignore: false;
29};
30
31export type Sources = OriginalSource | MapSource;
32
33const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null, false);
34const EMPTY_SOURCES: Sources[] = [];
35
36function SegmentObject(
37 source: string,
38 line: number,
39 column: number,
40 name: string,
41 content: string | null,
42 ignore: boolean,
43): SourceMapSegmentObject {
44 return { source, line, column, name, content, ignore };
45}
46
47function Source(
48 map: TraceMap,
49 sources: Sources[],
50 source: '',
51 content: null,
52 ignore: false,
53): MapSource;
54function Source(
55 map: null,
56 sources: Sources[],
57 source: string,
58 content: string | null,
59 ignore: boolean,
60): OriginalSource;
61function Source(
62 map: TraceMap | null,
63 sources: Sources[],
64 source: string | '',
65 content: string | null,
66 ignore: boolean,
67): Sources {
68 return {
69 map,
70 sources,
71 source,
72 content,
73 ignore,
74 } as any;
75}
76
77/**
78 * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
79 * (which may themselves be SourceMapTrees).
80 */
81export function MapSource(map: TraceMap, sources: Sources[]): MapSource {
82 return Source(map, sources, '', null, false);
83}
84
85/**
86 * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
87 * segment tracing ends at the `OriginalSource`.
88 */
89export function OriginalSource(
90 source: string,
91 content: string | null,
92 ignore: boolean,
93): OriginalSource {
94 return Source(null, EMPTY_SOURCES, source, content, ignore);
95}
96
97/**
98 * traceMappings is only called on the root level SourceMapTree, and begins the process of
99 * resolving each mapping in terms of the original source files.
100 */
101export function traceMappings(tree: MapSource): GenMapping {
102 // TODO: Eventually support sourceRoot, which has to be removed because the sources are already
103 // fully resolved. We'll need to make sources relative to the sourceRoot before adding them.
104 const gen = new GenMapping({ file: tree.map.file });
105 const { sources: rootSources, map } = tree;
106 const rootNames = map.names;
107 const rootMappings = decodedMappings(map);
108
109 for (let i = 0; i < rootMappings.length; i++) {
110 const segments = rootMappings[i];
111
112 for (let j = 0; j < segments.length; j++) {
113 const segment = segments[j];
114 const genCol = segment[0];
115 let traced: SourceMapSegmentObject | null = SOURCELESS_MAPPING;
116
117 // 1-length segments only move the current generated column, there's no source information
118 // to gather from it.
119 if (segment.length !== 1) {
120 const source = rootSources[segment[1]];
121 traced = originalPositionFor(
122 source,
123 segment[2],
124 segment[3],
125 segment.length === 5 ? rootNames[segment[4]] : '',
126 );
127
128 // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
129 // respective segment into an original source.
130 if (traced == null) continue;
131 }
132
133 const { column, line, name, content, source, ignore } = traced;
134
135 maybeAddSegment(gen, i, genCol, source, line, column, name);
136 if (source && content != null) setSourceContent(gen, source, content);
137 if (ignore) setIgnore(gen, source, true);
138 }
139 }
140
141 return gen;
142}
143
144/**
145 * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
146 * child SourceMapTrees, until we find the original source map.
147 */
148export function originalPositionFor(
149 source: Sources,
150 line: number,
151 column: number,
152 name: string,
153): SourceMapSegmentObject | null {
154 if (!source.map) {
155 return SegmentObject(source.source, line, column, name, source.content, source.ignore);
156 }
157
158 const segment = traceSegment(source.map, line, column);
159
160 // If we couldn't find a segment, then this doesn't exist in the sourcemap.
161 if (segment == null) return null;
162 // 1-length segments only move the current generated column, there's no source information
163 // to gather from it.
164 if (segment.length === 1) return SOURCELESS_MAPPING;
165
166 return originalPositionFor(
167 source.sources[segment[1]],
168 segment[2],
169 segment[3],
170 segment.length === 5 ? source.map.names[segment[4]] : name,
171 );
172}
Note: See TracBrowser for help on using the repository browser.