forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource-map.js
More file actions
322 lines (275 loc) · 7.87 KB
/
Copy pathsource-map.js
File metadata and controls
322 lines (275 loc) · 7.87 KB
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
/* 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 assert = require("./utils/assert");
const { fetchSourceMap } = require("./utils/fetchSourceMap");
const {
getSourceMap,
setSourceMap,
clearSourceMaps
} = require("./utils/sourceMapRequests");
const {
originalToGeneratedId,
generatedToOriginalId,
isGeneratedId,
isOriginalId,
getContentType
} = require("./utils");
import type { Location, Source, SourceId } from "debugger-html";
async function getOriginalURLs(generatedSource: Source) {
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) {
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: Location,
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: Location,
originalSource: Source
): Promise<Location> {
if (!isOriginalId(location.sourceId)) {
return location;
}
const generatedSourceId = originalToGeneratedId(location.sourceId);
const map = await getSourceMap(generatedSourceId);
if (!map) {
return location;
}
const { line, column } = map.generatedPositionFor({
source: originalSource.url,
line: location.line,
column: location.column == null ? 0 : location.column,
bias: SourceMapConsumer.LEAST_UPPER_BOUND
});
return {
sourceId: generatedSourceId,
line,
column
};
}
async function getAllGeneratedLocations(
location: Location,
originalSource: Source
): Promise<Array<Location>> {
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
}));
}
type locationOptions = {
search?: "LEAST_UPPER_BOUND" | "GREATEST_LOWER_BOUND"
};
async function getOriginalLocation(
location: Location,
{ search }: locationOptions = {}
): Promise<Location> {
if (!isGeneratedId(location.sourceId)) {
return location;
}
const map = await getSourceMap(location.sourceId);
if (!map) {
return location;
}
// 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 getOriginalSourceText(originalSource: Source) {
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 || "")
};
}
async function hasMappedSource(location: Location): 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 = SourceMapConsumer(generator.toJSON());
setSourceMap(generatedId, Promise.resolve(map));
}
module.exports = {
getOriginalURLs,
getOriginalRanges,
getGeneratedRanges,
getGeneratedLocation,
getAllGeneratedLocations,
getOriginalLocation,
getOriginalSourceText,
applySourceMap,
clearSourceMaps,
hasMappedSource
};