1 | import { SetArray, put, remove } from '@jridgewell/set-array';
|
---|
2 | import { encode } from '@jridgewell/sourcemap-codec';
|
---|
3 | import { TraceMap, decodedMappings } from '@jridgewell/trace-mapping';
|
---|
4 |
|
---|
5 | const COLUMN = 0;
|
---|
6 | const SOURCES_INDEX = 1;
|
---|
7 | const SOURCE_LINE = 2;
|
---|
8 | const SOURCE_COLUMN = 3;
|
---|
9 | const NAMES_INDEX = 4;
|
---|
10 |
|
---|
11 | const NO_NAME = -1;
|
---|
12 | /**
|
---|
13 | * Provides the state to generate a sourcemap.
|
---|
14 | */
|
---|
15 | class GenMapping {
|
---|
16 | constructor({ file, sourceRoot } = {}) {
|
---|
17 | this._names = new SetArray();
|
---|
18 | this._sources = new SetArray();
|
---|
19 | this._sourcesContent = [];
|
---|
20 | this._mappings = [];
|
---|
21 | this.file = file;
|
---|
22 | this.sourceRoot = sourceRoot;
|
---|
23 | this._ignoreList = new SetArray();
|
---|
24 | }
|
---|
25 | }
|
---|
26 | /**
|
---|
27 | * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
|
---|
28 | * with public access modifiers.
|
---|
29 | */
|
---|
30 | function cast(map) {
|
---|
31 | return map;
|
---|
32 | }
|
---|
33 | function addSegment(map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
|
---|
34 | return addSegmentInternal(false, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
|
---|
35 | }
|
---|
36 | function addMapping(map, mapping) {
|
---|
37 | return addMappingInternal(false, map, mapping);
|
---|
38 | }
|
---|
39 | /**
|
---|
40 | * Same as `addSegment`, but will only add the segment if it generates useful information in the
|
---|
41 | * resulting map. This only works correctly if segments are added **in order**, meaning you should
|
---|
42 | * not add a segment with a lower generated line/column than one that came before.
|
---|
43 | */
|
---|
44 | const maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
|
---|
45 | return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
|
---|
46 | };
|
---|
47 | /**
|
---|
48 | * Same as `addMapping`, but will only add the mapping if it generates useful information in the
|
---|
49 | * resulting map. This only works correctly if mappings are added **in order**, meaning you should
|
---|
50 | * not add a mapping with a lower generated line/column than one that came before.
|
---|
51 | */
|
---|
52 | const maybeAddMapping = (map, mapping) => {
|
---|
53 | return addMappingInternal(true, map, mapping);
|
---|
54 | };
|
---|
55 | /**
|
---|
56 | * Adds/removes the content of the source file to the source map.
|
---|
57 | */
|
---|
58 | function setSourceContent(map, source, content) {
|
---|
59 | const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
|
---|
60 | const index = put(sources, source);
|
---|
61 | sourcesContent[index] = content;
|
---|
62 | }
|
---|
63 | function setIgnore(map, source, ignore = true) {
|
---|
64 | const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
|
---|
65 | const index = put(sources, source);
|
---|
66 | if (index === sourcesContent.length)
|
---|
67 | sourcesContent[index] = null;
|
---|
68 | if (ignore)
|
---|
69 | put(ignoreList, index);
|
---|
70 | else
|
---|
71 | remove(ignoreList, index);
|
---|
72 | }
|
---|
73 | /**
|
---|
74 | * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
|
---|
75 | * a sourcemap, or to JSON.stringify.
|
---|
76 | */
|
---|
77 | function toDecodedMap(map) {
|
---|
78 | const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList, } = cast(map);
|
---|
79 | removeEmptyFinalLines(mappings);
|
---|
80 | return {
|
---|
81 | version: 3,
|
---|
82 | file: map.file || undefined,
|
---|
83 | names: names.array,
|
---|
84 | sourceRoot: map.sourceRoot || undefined,
|
---|
85 | sources: sources.array,
|
---|
86 | sourcesContent,
|
---|
87 | mappings,
|
---|
88 | ignoreList: ignoreList.array,
|
---|
89 | };
|
---|
90 | }
|
---|
91 | /**
|
---|
92 | * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
|
---|
93 | * a sourcemap, or to JSON.stringify.
|
---|
94 | */
|
---|
95 | function toEncodedMap(map) {
|
---|
96 | const decoded = toDecodedMap(map);
|
---|
97 | return Object.assign(Object.assign({}, decoded), { mappings: encode(decoded.mappings) });
|
---|
98 | }
|
---|
99 | /**
|
---|
100 | * Constructs a new GenMapping, using the already present mappings of the input.
|
---|
101 | */
|
---|
102 | function fromMap(input) {
|
---|
103 | const map = new TraceMap(input);
|
---|
104 | const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });
|
---|
105 | putAll(cast(gen)._names, map.names);
|
---|
106 | putAll(cast(gen)._sources, map.sources);
|
---|
107 | cast(gen)._sourcesContent = map.sourcesContent || map.sources.map(() => null);
|
---|
108 | cast(gen)._mappings = decodedMappings(map);
|
---|
109 | if (map.ignoreList)
|
---|
110 | putAll(cast(gen)._ignoreList, map.ignoreList);
|
---|
111 | return gen;
|
---|
112 | }
|
---|
113 | /**
|
---|
114 | * Returns an array of high-level mapping objects for every recorded segment, which could then be
|
---|
115 | * passed to the `source-map` library.
|
---|
116 | */
|
---|
117 | function allMappings(map) {
|
---|
118 | const out = [];
|
---|
119 | const { _mappings: mappings, _sources: sources, _names: names } = cast(map);
|
---|
120 | for (let i = 0; i < mappings.length; i++) {
|
---|
121 | const line = mappings[i];
|
---|
122 | for (let j = 0; j < line.length; j++) {
|
---|
123 | const seg = line[j];
|
---|
124 | const generated = { line: i + 1, column: seg[COLUMN] };
|
---|
125 | let source = undefined;
|
---|
126 | let original = undefined;
|
---|
127 | let name = undefined;
|
---|
128 | if (seg.length !== 1) {
|
---|
129 | source = sources.array[seg[SOURCES_INDEX]];
|
---|
130 | original = { line: seg[SOURCE_LINE] + 1, column: seg[SOURCE_COLUMN] };
|
---|
131 | if (seg.length === 5)
|
---|
132 | name = names.array[seg[NAMES_INDEX]];
|
---|
133 | }
|
---|
134 | out.push({ generated, source, original, name });
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return out;
|
---|
138 | }
|
---|
139 | // This split declaration is only so that terser can elminiate the static initialization block.
|
---|
140 | function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
|
---|
141 | const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = cast(map);
|
---|
142 | const line = getLine(mappings, genLine);
|
---|
143 | const index = getColumnIndex(line, genColumn);
|
---|
144 | if (!source) {
|
---|
145 | if (skipable && skipSourceless(line, index))
|
---|
146 | return;
|
---|
147 | return insert(line, index, [genColumn]);
|
---|
148 | }
|
---|
149 | const sourcesIndex = put(sources, source);
|
---|
150 | const namesIndex = name ? put(names, name) : NO_NAME;
|
---|
151 | if (sourcesIndex === sourcesContent.length)
|
---|
152 | sourcesContent[sourcesIndex] = content !== null && content !== void 0 ? content : null;
|
---|
153 | if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
|
---|
154 | return;
|
---|
155 | }
|
---|
156 | return insert(line, index, name
|
---|
157 | ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
|
---|
158 | : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
|
---|
159 | }
|
---|
160 | function getLine(mappings, index) {
|
---|
161 | for (let i = mappings.length; i <= index; i++) {
|
---|
162 | mappings[i] = [];
|
---|
163 | }
|
---|
164 | return mappings[index];
|
---|
165 | }
|
---|
166 | function getColumnIndex(line, genColumn) {
|
---|
167 | let index = line.length;
|
---|
168 | for (let i = index - 1; i >= 0; index = i--) {
|
---|
169 | const current = line[i];
|
---|
170 | if (genColumn >= current[COLUMN])
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | return index;
|
---|
174 | }
|
---|
175 | function insert(array, index, value) {
|
---|
176 | for (let i = array.length; i > index; i--) {
|
---|
177 | array[i] = array[i - 1];
|
---|
178 | }
|
---|
179 | array[index] = value;
|
---|
180 | }
|
---|
181 | function removeEmptyFinalLines(mappings) {
|
---|
182 | const { length } = mappings;
|
---|
183 | let len = length;
|
---|
184 | for (let i = len - 1; i >= 0; len = i, i--) {
|
---|
185 | if (mappings[i].length > 0)
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | if (len < length)
|
---|
189 | mappings.length = len;
|
---|
190 | }
|
---|
191 | function putAll(setarr, array) {
|
---|
192 | for (let i = 0; i < array.length; i++)
|
---|
193 | put(setarr, array[i]);
|
---|
194 | }
|
---|
195 | function skipSourceless(line, index) {
|
---|
196 | // The start of a line is already sourceless, so adding a sourceless segment to the beginning
|
---|
197 | // doesn't generate any useful information.
|
---|
198 | if (index === 0)
|
---|
199 | return true;
|
---|
200 | const prev = line[index - 1];
|
---|
201 | // If the previous segment is also sourceless, then adding another sourceless segment doesn't
|
---|
202 | // genrate any new information. Else, this segment will end the source/named segment and point to
|
---|
203 | // a sourceless position, which is useful.
|
---|
204 | return prev.length === 1;
|
---|
205 | }
|
---|
206 | function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
|
---|
207 | // A source/named segment at the start of a line gives position at that genColumn
|
---|
208 | if (index === 0)
|
---|
209 | return false;
|
---|
210 | const prev = line[index - 1];
|
---|
211 | // If the previous segment is sourceless, then we're transitioning to a source.
|
---|
212 | if (prev.length === 1)
|
---|
213 | return false;
|
---|
214 | // If the previous segment maps to the exact same source position, then this segment doesn't
|
---|
215 | // provide any new position information.
|
---|
216 | return (sourcesIndex === prev[SOURCES_INDEX] &&
|
---|
217 | sourceLine === prev[SOURCE_LINE] &&
|
---|
218 | sourceColumn === prev[SOURCE_COLUMN] &&
|
---|
219 | namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
|
---|
220 | }
|
---|
221 | function addMappingInternal(skipable, map, mapping) {
|
---|
222 | const { generated, source, original, name, content } = mapping;
|
---|
223 | if (!source) {
|
---|
224 | return addSegmentInternal(skipable, map, generated.line - 1, generated.column, null, null, null, null, null);
|
---|
225 | }
|
---|
226 | return addSegmentInternal(skipable, map, generated.line - 1, generated.column, source, original.line - 1, original.column, name, content);
|
---|
227 | }
|
---|
228 |
|
---|
229 | export { GenMapping, addMapping, addSegment, allMappings, fromMap, maybeAddMapping, maybeAddSegment, setIgnore, setSourceContent, toDecodedMap, toEncodedMap };
|
---|
230 | //# sourceMappingURL=gen-mapping.mjs.map
|
---|