This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 762
/
source-map.js
584 lines (510 loc) 路 14.6 KB
/
source-map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
/**
* Source Map Worker
* @module utils/source-map-worker
*/
const { networkRequest } = require("devtools-utils");
const { SourceMapConsumer, SourceMapGenerator } = require("source-map");
const { createConsumer } = require("./utils/createConsumer");
const assert = require("./utils/assert");
const {
fetchSourceMap,
hasOriginalURL,
clearOriginalURLs
} = require("./utils/fetchSourceMap");
const {
getSourceMap,
setSourceMap,
clearSourceMaps: clearSourceMapsRequests
} = require("./utils/sourceMapRequests");
const {
originalToGeneratedId,
generatedToOriginalId,
isGeneratedId,
isOriginalId,
getContentType
} = require("./utils");
const { clearWasmXScopes } = require("./utils/wasmXScopes");
import type { SourceLocation, Source, SourceId } from "debugger-html";
type Range = {
start: {
line: number,
column: number
},
end: {
line: number,
column: number
}
};
export type LocationOptions = {
search?: "LEAST_UPPER_BOUND" | "GREATEST_LOWER_BOUND"
};
async function getOriginalURLs(
generatedSource: Source
): Promise<SourceMapConsumer> {
const map = await fetchSourceMap(generatedSource);
return map && map.sources;
}
const COMPUTED_SPANS = new WeakSet();
const SOURCE_MAPPINGS = new WeakMap();
async function getOriginalRanges(
sourceId: SourceId,
url: string
): Promise<
Array<{
line: number,
columnStart: number,
columnEnd: number
}>
> {
if (!isOriginalId(sourceId)) {
return [];
}
const generatedSourceId = originalToGeneratedId(sourceId);
const map = await getSourceMap(generatedSourceId);
if (!map) {
return [];
}
let mappings = SOURCE_MAPPINGS.get(map);
if (!mappings) {
mappings = new Map();
SOURCE_MAPPINGS.set(map, mappings);
}
let fileMappings = mappings.get(url);
if (!fileMappings) {
fileMappings = [];
mappings.set(url, fileMappings);
const originalMappings = fileMappings;
map.eachMapping(
mapping => {
if (mapping.source !== url) {
return;
}
const last = originalMappings[originalMappings.length - 1];
if (last && last.line === mapping.originalLine) {
if (last.columnStart < mapping.originalColumn) {
last.columnEnd = mapping.originalColumn;
} else {
// Skip this duplicate original location,
return;
}
}
originalMappings.push({
line: mapping.originalLine,
columnStart: mapping.originalColumn,
columnEnd: Infinity
});
},
null,
SourceMapConsumer.ORIGINAL_ORDER
);
}
return fileMappings;
}
/**
* Given an original location, find the ranges on the generated file that
* are mapped from the original range containing the location.
*/
async function getGeneratedRanges(
location: SourceLocation,
originalSource: Source
): Promise<
Array<{
line: number,
columnStart: number,
columnEnd: number
}>
> {
if (!isOriginalId(location.sourceId)) {
return [];
}
const generatedSourceId = originalToGeneratedId(location.sourceId);
const map = await getSourceMap(generatedSourceId);
if (!map) {
return [];
}
if (!COMPUTED_SPANS.has(map)) {
COMPUTED_SPANS.add(map);
map.computeColumnSpans();
}
// We want to use 'allGeneratedPositionsFor' to get the _first_ generated
// location, but it hard-codes SourceMapConsumer.LEAST_UPPER_BOUND as the
// bias, making it search in the wrong direction for this usecase.
// To work around this, we use 'generatedPositionFor' and then look up the
// exact original location, making any bias value unnecessary, and then
// use that location for the call to 'allGeneratedPositionsFor'.
const genPos = map.generatedPositionFor({
source: originalSource.url,
line: location.line,
column: location.column == null ? 0 : location.column,
bias: SourceMapConsumer.GREATEST_LOWER_BOUND
});
if (genPos.line === null) {
return [];
}
const positions = map.allGeneratedPositionsFor(
map.originalPositionFor({
line: genPos.line,
column: genPos.column
})
);
return positions
.map(mapping => ({
line: mapping.line,
columnStart: mapping.column,
columnEnd: mapping.lastColumn
}))
.sort((a, b) => {
const line = a.line - b.line;
return line === 0 ? a.column - b.column : line;
});
}
async function getGeneratedLocation(
location: SourceLocation,
originalSource: Source
): Promise<SourceLocation> {
if (!isOriginalId(location.sourceId)) {
return location;
}
const generatedSourceId = originalToGeneratedId(location.sourceId);
const map = await getSourceMap(generatedSourceId);
if (!map) {
return location;
}
const positions = map.allGeneratedPositionsFor({
source: originalSource.url,
line: location.line,
column: location.column == null ? 0 : location.column
});
// Prior to source-map 0.7, the source-map module returned the earliest
// generated location in the file when there were multiple generated
// locations. The current comparison fn in 0.7 does not appear to take
// generated location into account properly.
let match;
for (const pos of positions) {
if (!match || pos.line < match.line || pos.column < match.column) {
match = pos;
}
}
if (!match) {
match = map.generatedPositionFor({
source: originalSource.url,
line: location.line,
column: location.column == null ? 0 : location.column,
bias: SourceMapConsumer.LEAST_UPPER_BOUND
});
}
return {
sourceId: generatedSourceId,
line: match.line,
column: match.column
};
}
async function getAllGeneratedLocations(
location: SourceLocation,
originalSource: Source
): Promise<Array<SourceLocation>> {
if (!isOriginalId(location.sourceId)) {
return [];
}
const generatedSourceId = originalToGeneratedId(location.sourceId);
const map = await getSourceMap(generatedSourceId);
if (!map) {
return [];
}
const positions = map.allGeneratedPositionsFor({
source: originalSource.url,
line: location.line,
column: location.column == null ? 0 : location.column
});
return positions.map(({ line, column }) => ({
sourceId: generatedSourceId,
line,
column
}));
}
async function getOriginalLocations(
sourceId: string,
locations: SourceLocation[],
options: LocationOptions = {}
): Promise<SourceLocation[]> {
if (locations.some(location => location.sourceId != sourceId)) {
throw new Error("Generated locations must belong to the same source");
}
const map = await getSourceMap(sourceId);
if (!map) {
return locations;
}
return locations.map(location =>
getOriginalLocationSync(map, location, options)
);
}
function getOriginalLocationSync(
map,
location: SourceLocation,
{ search }: LocationOptions = {}
): SourceLocation {
// First check for an exact match
let match = map.originalPositionFor({
line: location.line,
column: location.column == null ? 0 : location.column
});
// If there is not an exact match, look for a match with a bias at the
// current location and then on subsequent lines
if (search) {
let line = location.line;
let column = location.column == null ? 0 : location.column;
while (match.source === null) {
match = map.originalPositionFor({
line,
column,
bias: SourceMapConsumer[search]
});
line += search == "LEAST_UPPER_BOUND" ? 1 : -1;
column = search == "LEAST_UPPER_BOUND" ? 0 : Infinity;
}
}
const { source: sourceUrl, line, column } = match;
if (sourceUrl == null) {
// No url means the location didn't map.
return location;
}
return {
sourceId: generatedToOriginalId(location.sourceId, sourceUrl),
sourceUrl,
line,
column
};
}
async function getOriginalLocation(
location: SourceLocation,
options: LocationOptions = {}
): Promise<SourceLocation> {
if (!isGeneratedId(location.sourceId)) {
return location;
}
const map = await getSourceMap(location.sourceId);
if (!map) {
return location;
}
return getOriginalLocationSync(map, location, options);
}
async function getOriginalSourceText(
originalSource: Source
): Promise<?{
text: string,
contentType: string
}> {
assert(isOriginalId(originalSource.id), "Source is not an original source");
const generatedSourceId = originalToGeneratedId(originalSource.id);
const map = await getSourceMap(generatedSourceId);
if (!map) {
return null;
}
let text = map.sourceContentFor(originalSource.url);
if (!text) {
text = (await networkRequest(originalSource.url, { loadFromCache: false }))
.content;
}
return {
text,
contentType: getContentType(originalSource.url || "")
};
}
/**
* Find the set of ranges on the generated file that map from the original
* file's locations.
*
* @param sourceId - The original ID of the file we are processing.
* @param url - The original URL of the file we are processing.
* @param mergeUnmappedRegions - If unmapped regions are encountered between
* two mappings for the given original file, allow the two mappings to be
* merged anyway. This is useful if you are more interested in the general
* contiguous ranges associated with a file, rather than the specifics of
* the ranges provided by the sourcemap.
*/
const GENERATED_MAPPINGS = new WeakMap();
async function getGeneratedRangesForOriginal(
sourceId: SourceId,
url: string,
mergeUnmappedRegions: boolean = false
): Promise<Range[]> {
assert(isOriginalId(sourceId), "Source is not an original source");
const map = await getSourceMap(originalToGeneratedId(sourceId));
// NOTE: this is only needed for Flow
if (!map) {
return [];
}
if (!COMPUTED_SPANS.has(map)) {
COMPUTED_SPANS.add(map);
map.computeColumnSpans();
}
if (!GENERATED_MAPPINGS.has(map)) {
GENERATED_MAPPINGS.set(map, new Map());
}
const generatedRangesMap = GENERATED_MAPPINGS.get(map);
if (!generatedRangesMap) {
return [];
}
if (generatedRangesMap.has(sourceId)) {
// NOTE we need to coerce the result to an array for Flow
return generatedRangesMap.get(sourceId) || [];
}
// Gather groups of mappings on the generated file, with new groups created
// if we cross a mapping for a different file.
let currentGroup = [];
const originalGroups = [currentGroup];
map.eachMapping(
mapping => {
if (mapping.source === url) {
currentGroup.push({
start: {
line: mapping.generatedLine,
column: mapping.generatedColumn
},
end: {
line: mapping.generatedLine,
// The lastGeneratedColumn value is an inclusive value so we add
// one to it to get the exclusive end position.
column: mapping.lastGeneratedColumn + 1
}
});
} else if (
typeof mapping.source === "string" &&
currentGroup.length > 0
) {
// If there is a URL, but it is for a _different_ file, we create a
// new group of mappings so that we can tell
currentGroup = [];
originalGroups.push(currentGroup);
}
},
null,
SourceMapConsumer.GENERATED_ORDER
);
const generatedMappingsForOriginal = [];
if (mergeUnmappedRegions) {
// If we don't care about excluding unmapped regions, then we just need to
// create a range that is the fully encompasses each group, ignoring the
// empty space between each individual range.
for (const group of originalGroups) {
if (group.length > 0) {
generatedMappingsForOriginal.push({
start: group[0].start,
end: group[group.length - 1].end
});
}
}
} else {
let lastEntry;
for (const group of originalGroups) {
lastEntry = null;
for (const { start, end } of group) {
const lastEnd = lastEntry
? wrappedMappingPosition(lastEntry.end)
: null;
// If this entry comes immediately after the previous one, extend the
// range of the previous entry instead of adding a new one.
if (
lastEntry &&
lastEnd &&
lastEnd.line === start.line &&
lastEnd.column === start.column
) {
lastEntry.end = end;
} else {
const newEntry = { start, end };
generatedMappingsForOriginal.push(newEntry);
lastEntry = newEntry;
}
}
}
}
generatedRangesMap.set(sourceId, generatedMappingsForOriginal);
return generatedMappingsForOriginal;
}
function wrappedMappingPosition(pos: {
line: number,
column: number
}): {
line: number,
column: number
} {
if (pos.column !== Infinity) {
return pos;
}
// If the end of the entry consumes the whole line, treat it as wrapping to
// the next line.
return {
line: pos.line + 1,
column: 0
};
}
async function getFileGeneratedRange(
originalSource: Source
): Promise<?{ start: any, end: any }> {
assert(isOriginalId(originalSource.id), "Source is not an original source");
const map = await getSourceMap(originalToGeneratedId(originalSource.id));
if (!map) {
return;
}
const start = map.generatedPositionFor({
source: originalSource.url,
line: 1,
column: 0,
bias: SourceMapConsumer.LEAST_UPPER_BOUND
});
const end = map.generatedPositionFor({
source: originalSource.url,
line: Number.MAX_SAFE_INTEGER,
column: Number.MAX_SAFE_INTEGER,
bias: SourceMapConsumer.GREATEST_LOWER_BOUND
});
return {
start,
end
};
}
async function hasMappedSource(location: SourceLocation): Promise<boolean> {
if (isOriginalId(location.sourceId)) {
return true;
}
const loc = await getOriginalLocation(location);
return loc.sourceId !== location.sourceId;
}
function applySourceMap(
generatedId: string,
url: string,
code: string,
mappings: Object
) {
const generator = new SourceMapGenerator({ file: url });
mappings.forEach(mapping => generator.addMapping(mapping));
generator.setSourceContent(url, code);
const map = createConsumer(generator.toJSON());
setSourceMap(generatedId, Promise.resolve(map));
}
function clearSourceMaps() {
clearSourceMapsRequests();
clearWasmXScopes();
clearOriginalURLs();
}
module.exports = {
getOriginalURLs,
hasOriginalURL,
getOriginalRanges,
getGeneratedRanges,
getGeneratedLocation,
getAllGeneratedLocations,
getOriginalLocation,
getOriginalLocations,
getOriginalSourceText,
getGeneratedRangesForOriginal,
getFileGeneratedRange,
applySourceMap,
clearSourceMaps,
hasMappedSource
};