forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.js
More file actions
359 lines (306 loc) · 7.96 KB
/
source.js
File metadata and controls
359 lines (306 loc) · 7.96 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
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
/* 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
/**
* Utils for working with Source URLs
* @module utils/source
*/
import { isOriginalId } from "devtools-source-map";
import { endTruncateStr } from "./utils";
import { basename } from "../utils/path";
import { parse as parseURL } from "url";
import type { Source } from "../types";
import type { SourceMetaDataType } from "../reducers/ast";
type transformUrlCallback = string => string;
/**
* Trims the query part or reference identifier of a url string, if necessary.
*
* @memberof utils/source
* @static
*/
function trimUrlQuery(url: string): string {
const length = url.length;
const q1 = url.indexOf("?");
const q2 = url.indexOf("&");
const q3 = url.indexOf("#");
const q = Math.min(
q1 != -1 ? q1 : length,
q2 != -1 ? q2 : length,
q3 != -1 ? q3 : length
);
return url.slice(0, q);
}
function shouldPrettyPrint(source: any) {
if (!source) {
return false;
}
const _isPretty = isPretty(source);
const _isJavaScript = isJavaScript(source);
const isOriginal = isOriginalId(source.id);
const hasSourceMap = source.sourceMapURL;
if (_isPretty || isOriginal || hasSourceMap || !_isJavaScript) {
return false;
}
return true;
}
/**
* Returns true if the specified url and/or content type are specific to
* javascript files.
*
* @return boolean
* True if the source is likely javascript.
*
* @memberof utils/source
* @static
*/
function isJavaScript(source: Source): boolean {
return (
(source.url && /\.(jsm|js)?$/.test(trimUrlQuery(source.url))) ||
!!(source.contentType && source.contentType.includes("javascript"))
);
}
/**
* @memberof utils/source
* @static
*/
function isPretty(source: Source): boolean {
return source.url ? /formatted$/.test(source.url) : false;
}
function isThirdParty(source: Source) {
if (!source || !source.url) {
return false;
}
return !!source.url.match(/(node_modules|bower_components)/);
}
/**
* @memberof utils/source
* @static
*/
function getPrettySourceURL(url: ?string): string {
if (!url) {
url = "";
}
return `${url}:formatted`;
}
/**
* @memberof utils/source
* @static
*/
function getRawSourceURL(url: string): string {
return url.replace(/:formatted$/, "");
}
function resolveFileURL(
url: string,
transformUrl: transformUrlCallback = initialUrl => initialUrl
) {
url = getRawSourceURL(url || "");
const name = transformUrl(url);
return endTruncateStr(name, 50);
}
function getFilenameFromURL(url: string) {
return resolveFileURL(url, initialUrl => basename(initialUrl) || "(index)");
}
function getFormattedSourceId(id: string) {
const sourceId = id.split("/")[1];
return `SOURCE${sourceId}`;
}
/**
* Show a source url's filename.
* If the source does not have a url, use the source id.
*
* @memberof utils/source
* @static
*/
function getFilename(source: Source) {
const { url, id } = source;
if (!url) {
return getFormattedSourceId(id);
}
let filename = getFilenameFromURL(url);
const qMarkIdx = filename.indexOf("?");
if (qMarkIdx > 0) {
filename = filename.slice(0, qMarkIdx);
}
return filename;
}
/**
* Show a source url.
* If the source does not have a url, use the source id.
*
* @memberof utils/source
* @static
*/
function getFileURL(source: Source) {
const { url, id } = source;
if (!url) {
return getFormattedSourceId(id);
}
return resolveFileURL(url);
}
const contentTypeModeMap = {
"text/javascript": { name: "javascript" },
"text/typescript": { name: "javascript", typescript: true },
"text/coffeescript": "coffeescript",
"text/typescript-jsx": {
name: "jsx",
base: { name: "javascript", typescript: true }
},
"text/jsx": "jsx",
"text/x-elm": "elm",
"text/x-clojure": "clojure",
"text/wasm": { name: "text" },
"text/html": { name: "htmlmixed" }
};
function getSourcePath(source: Source) {
if (!source.url) {
return "";
}
const { path, href } = parseURL(source.url);
// for URLs like "about:home" the path is null so we pass the full href
return path || href;
}
/**
* Returns amount of lines in the source. If source is a WebAssembly binary,
* the function returns amount of bytes.
*/
function getSourceLineCount(source: Source) {
if (source.isWasm) {
const { binary } = (source.text: any);
return binary.length;
}
return source.text != undefined ? source.text.split("\n").length : 0;
}
// Used to detect minification for automatic pretty printing
const SAMPLE_SIZE = 50;
const INDENT_COUNT_THRESHOLD = 5;
const CHARACTER_LIMIT = 250;
const _minifiedCache = new Map();
/**
*
* Checks if a source is minified based on some heuristics
* @param key
* @param text
* @return boolean
* @memberof utils/source
* @static
*/
function isMinified(key: string, text: string) {
if (!key || !text) {
return false;
}
if (_minifiedCache.has(key)) {
return _minifiedCache.get(key);
}
let lineEndIndex = 0;
let lineStartIndex = 0;
let lines = 0;
let indentCount = 0;
let overCharLimit = false;
// Strip comments.
text = text.replace(/\/\*[\S\s]*?\*\/|\/\/(.+|\n)/g, "");
while (lines++ < SAMPLE_SIZE) {
lineEndIndex = text.indexOf("\n", lineStartIndex);
if (lineEndIndex == -1) {
break;
}
if (/^\s+/.test(text.slice(lineStartIndex, lineEndIndex))) {
indentCount++;
}
// For files with no indents but are not minified.
if (lineEndIndex - lineStartIndex > CHARACTER_LIMIT) {
overCharLimit = true;
break;
}
lineStartIndex = lineEndIndex + 1;
}
const minified =
indentCount / lines * 100 < INDENT_COUNT_THRESHOLD || overCharLimit;
_minifiedCache.set(key, minified);
return minified;
}
/**
*
* Returns Code Mirror mode for source content type
* @param contentType
* @return String
* @memberof utils/source
* @static
*/
function getMode(source: Source, sourceMetaData: SourceMetaDataType) {
const { contentType, text, isWasm, url } = source;
if (!text || isWasm) {
return { name: "text" };
}
if (
(url && url.match(/\.jsx$/i)) ||
(sourceMetaData && sourceMetaData.isReactComponent)
) {
return "jsx";
}
const languageMimeMap = [
{ ext: ".c", mode: "text/x-csrc" },
{ ext: ".kt", mode: "text/x-kotlin" },
{ ext: ".cpp", mode: "text/x-c++src" },
{ ext: ".m", mode: "text/x-objectivec" },
{ ext: ".rs", mode: "text/x-rustsrc" }
];
// check for C and other non JS languages
if (url) {
const result = languageMimeMap.find(({ ext }) => url.endsWith(ext));
if (result !== undefined) {
return result.mode;
}
}
// if the url ends with .marko we set the name to Javascript so
// syntax highlighting works for marko too
if (url && url.match(/\.marko$/i)) {
return { name: "javascript" };
}
// Use HTML mode for files in which the first non whitespace
// character is `<` regardless of extension.
const isHTMLLike = text.match(/^\s*</);
if (!contentType) {
if (isHTMLLike) {
return { name: "htmlmixed" };
}
return { name: "text" };
}
// // @flow or /* @flow */
if (text.match(/^\s*(\/\/ @flow|\/\* @flow \*\/)/)) {
return contentTypeModeMap["text/typescript"];
}
if (/script|elm|jsx|clojure|wasm|html/.test(contentType)) {
if (contentType in contentTypeModeMap) {
return contentTypeModeMap[contentType];
}
return contentTypeModeMap["text/javascript"];
}
if (isHTMLLike) {
return { name: "htmlmixed" };
}
return { name: "text" };
}
function isLoaded(source: Source) {
return source.loadedState === "loaded";
}
function isLoading(source: Source) {
return source.loadedState === "loading";
}
export {
isMinified,
isJavaScript,
isPretty,
isThirdParty,
shouldPrettyPrint,
getPrettySourceURL,
getRawSourceURL,
getFilename,
getFilenameFromURL,
getFileURL,
getSourcePath,
getSourceLineCount,
getMode,
isLoaded,
isLoading
};