-
Notifications
You must be signed in to change notification settings - Fork 47.5k
/
Copy pathparseHookNames.js
501 lines (433 loc) · 15 KB
/
parseHookNames.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
/* global chrome */
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import {parse} from '@babel/parser';
import {enableHookNameParsing} from 'react-devtools-feature-flags';
import LRU from 'lru-cache';
import {SourceMapConsumer} from 'source-map';
import {getHookName, isNonDeclarativePrimitiveHook} from './astUtils';
import {areSourceMapsAppliedToErrors} from './ErrorTester';
import {__DEBUG__} from 'react-devtools-shared/src/constants';
import type {
HooksNode,
HookSource,
HooksTree,
} from 'react-debug-tools/src/ReactDebugHooks';
import type {HookNames, LRUCache} from 'react-devtools-shared/src/types';
import type {Thenable} from 'shared/ReactTypes';
import type {SourceConsumer, SourceMap} from './astUtils';
const SOURCE_MAP_REGEX = / ?sourceMappingURL=([^\s'"]+)/gm;
const ABSOLUTE_URL_REGEX = /^https?:\/\//i;
const MAX_SOURCE_LENGTH = 100_000_000;
type AST = mixed;
type HookSourceData = {|
// Generated by react-debug-tools.
hookSource: HookSource,
// AST for original source code; typically comes from a consumed source map.
originalSourceAST: AST | null,
// Source code (React components or custom hooks) containing primitive hook calls.
// If no source map has been provided, this code will be the same as runtimeSourceCode.
originalSourceCode: string | null,
// Compiled code (React components or custom hooks) containing primitive hook calls.
runtimeSourceCode: string | null,
// APIs from source-map for parsing source maps (if detected).
sourceConsumer: SourceConsumer | null,
// External URL of source map.
// Sources without source maps (or with inline source maps) won't have this.
sourceMapURL: string | null,
// Parsed source map object.
sourceMapContents: SourceMap | null,
|};
type CachedMetadata = {|
originalSourceAST: AST,
originalSourceCode: string,
sourceConsumer: SourceConsumer | null,
|};
// On large trees, encoding takes significant time.
// Try to reuse the already encoded strings.
const fileNameToMetadataCache: LRUCache<string, CachedMetadata> = new LRU({
max: 50,
dispose: (fileName: string, metadata: CachedMetadata) => {
if (__DEBUG__) {
console.log(
'fileNameToHookSourceData.dispose() Evicting cached metadata for "' +
fileName +
'"',
);
}
const sourceConsumer = metadata.sourceConsumer;
if (sourceConsumer !== null) {
sourceConsumer.destroy();
}
},
});
export default async function parseHookNames(
hooksTree: HooksTree,
): Thenable<HookNames | null> {
if (!enableHookNameParsing) {
return Promise.resolve(null);
}
const hooksList: Array<HooksNode> = [];
flattenHooksList(hooksTree, hooksList);
if (__DEBUG__) {
console.log('parseHookNames() hooksList:', hooksList);
}
// Gather the unique set of source files to load for the built-in hooks.
const fileNameToHookSourceData: Map<string, HookSourceData> = new Map();
for (let i = 0; i < hooksList.length; i++) {
const hook = hooksList[i];
const hookSource = hook.hookSource;
if (hookSource == null) {
// Older versions of react-debug-tools don't include this information.
// In this case, we can't continue.
throw Error('Hook source code location not found.');
}
const fileName = hookSource.fileName;
if (fileName == null) {
throw Error('Hook source code location not found.');
} else {
if (!fileNameToHookSourceData.has(fileName)) {
const hookSourceData: HookSourceData = {
hookSource,
originalSourceAST: null,
originalSourceCode: null,
runtimeSourceCode: null,
sourceConsumer: null,
sourceMapURL: null,
sourceMapContents: null,
};
// If we've already loaded source/source map info for this file,
// we can skip reloading it (and more importantly, re-parsing it).
const metadata = fileNameToMetadataCache.get(fileName);
if (metadata != null) {
if (__DEBUG__) {
console.groupCollapsed(
'parseHookNames() Found cached metadata for file "' +
fileName +
'"',
);
console.log(metadata);
console.groupEnd();
}
hookSourceData.originalSourceAST = metadata.originalSourceAST;
hookSourceData.originalSourceCode = metadata.originalSourceCode;
hookSourceData.sourceConsumer = metadata.sourceConsumer;
}
fileNameToHookSourceData.set(fileName, hookSourceData);
}
}
}
return loadSourceFiles(fileNameToHookSourceData)
.then(() => extractAndLoadSourceMaps(fileNameToHookSourceData))
.then(() => parseSourceAST(fileNameToHookSourceData))
.then(() => updateLruCache(fileNameToHookSourceData))
.then(() => findHookNames(hooksList, fileNameToHookSourceData));
}
function decodeBase64String(encoded: string): Object {
if (typeof atob === 'function') {
return atob(encoded);
} else if (
typeof Buffer !== 'undefined' &&
Buffer !== null &&
typeof Buffer.from === 'function'
) {
return Buffer.from(encoded, 'base64');
} else {
throw Error('Cannot decode base64 string');
}
}
function extractAndLoadSourceMaps(
fileNameToHookSourceData: Map<string, HookSourceData>,
): Promise<*> {
const promises = [];
fileNameToHookSourceData.forEach(hookSourceData => {
if (hookSourceData.originalSourceAST !== null) {
// Use cached metadata.
return;
}
const runtimeSourceCode = ((hookSourceData.runtimeSourceCode: any): string);
const sourceMappingURLs = runtimeSourceCode.match(SOURCE_MAP_REGEX);
if (sourceMappingURLs == null) {
// Maybe file has not been transformed; we'll try to parse it as-is in parseSourceAST().
if (__DEBUG__) {
console.log('extractAndLoadSourceMaps() No source map found');
}
} else {
for (let i = 0; i < sourceMappingURLs.length; i++) {
const sourceMappingURL = sourceMappingURLs[i];
const index = sourceMappingURL.indexOf('base64,');
if (index >= 0) {
// Web apps like Code Sandbox embed multiple inline source maps.
// In this case, we need to loop through and find the right one.
// We may also need to trim any part of this string that isn't based64 encoded data.
const trimmed = ((sourceMappingURL.match(
/base64,([a-zA-Z0-9+\/=]+)/,
): any): Array<string>)[1];
const decoded = decodeBase64String(trimmed);
const parsed = JSON.parse(decoded);
if (__DEBUG__) {
console.groupCollapsed(
'extractAndLoadSourceMaps() Inline source map',
);
console.log(parsed);
console.groupEnd();
}
// Hook source might be a URL like "https://4syus.csb.app/src/App.js"
// Parsed source map might be a partial path like "src/App.js"
const fileName = ((hookSourceData.hookSource.fileName: any): string);
const match = parsed.sources.find(
source =>
source === 'Inline Babel script' || fileName.includes(source),
);
if (match) {
hookSourceData.sourceMapContents = parsed;
break;
}
} else {
if (sourceMappingURLs.length > 1) {
console.warn(
'More than one external source map detected in the source file',
);
}
let url = sourceMappingURLs[0].split('=')[1];
if (ABSOLUTE_URL_REGEX.test(url)) {
const baseURL = url.slice(0, url.lastIndexOf('/'));
url = `${baseURL}/${url}`;
if (!isValidUrl(url)) {
throw new Error(`Invalid source map URL "${url}"`);
}
}
hookSourceData.sourceMapURL = url;
if (__DEBUG__) {
console.log(
'extractAndLoadSourceMaps() External source map "' + url + '"',
);
}
promises.push(
fetchFile(url).then(sourceMapContents => {
hookSourceData.sourceMapContents = JSON.parse(sourceMapContents);
}),
);
break;
}
}
}
});
return Promise.all(promises);
}
function fetchFile(url: string): Promise<string> {
return new Promise((resolve, reject) => {
fetch(url).then(response => {
if (response.ok) {
response
.text()
.then(text => {
resolve(text);
})
.catch(error => {
reject(null);
});
} else {
reject(null);
}
});
});
}
function findHookNames(
hooksList: Array<HooksNode>,
fileNameToHookSourceData: Map<string, HookSourceData>,
): HookNames {
const map: HookNames = new Map();
hooksList.map(hook => {
if (isNonDeclarativePrimitiveHook(hook)) {
if (__DEBUG__) {
console.log('findHookNames() Non declarative primitive hook');
}
// Not all hooks have names (e.g. useEffect or useLayoutEffect)
return null;
}
// We already guard against a null HookSource in parseHookNames()
const hookSource = ((hook.hookSource: any): HookSource);
const fileName = hookSource.fileName;
if (!fileName) {
return null; // Should not be reachable.
}
const hookSourceData = fileNameToHookSourceData.get(fileName);
if (!hookSourceData) {
return null; // Should not be reachable.
}
const {lineNumber, columnNumber} = hookSource;
if (!lineNumber || !columnNumber) {
return null; // Should not be reachable.
}
const sourceConsumer = hookSourceData.sourceConsumer;
let originalSourceLineNumber;
if (areSourceMapsAppliedToErrors() || !sourceConsumer) {
// Either the current environment automatically applies source maps to errors,
// or the current code had no source map to begin with.
// Either way, we don't need to convert the Error stack frame locations.
originalSourceLineNumber = lineNumber;
} else {
originalSourceLineNumber = sourceConsumer.originalPositionFor({
line: lineNumber,
column: columnNumber,
}).line;
}
if (__DEBUG__) {
console.log(
'findHookNames() mapped line number',
lineNumber,
'to',
originalSourceLineNumber,
);
}
if (originalSourceLineNumber === null) {
return null;
}
const name = getHookName(
hook,
hookSourceData.originalSourceAST,
((hookSourceData.originalSourceCode: any): string),
((originalSourceLineNumber: any): number),
);
if (__DEBUG__) {
console.log('findHookNames() Found name "' + (name || '-') + '"');
}
map.set(hook, name);
});
return map;
}
function isValidUrl(possibleURL: string): boolean {
try {
// eslint-disable-next-line no-new
new URL(possibleURL);
} catch (_) {
return false;
}
return true;
}
function loadSourceFiles(
fileNameToHookSourceData: Map<string, HookSourceData>,
): Promise<*> {
const promises = [];
fileNameToHookSourceData.forEach((hookSourceData, fileName) => {
promises.push(
fetchFile(fileName).then(runtimeSourceCode => {
if (runtimeSourceCode.length > MAX_SOURCE_LENGTH) {
throw Error('Source code too large to parse');
}
if (__DEBUG__) {
console.groupCollapsed(
'loadSourceFiles() fileName "' + fileName + '"',
);
console.log(runtimeSourceCode);
console.groupEnd();
}
hookSourceData.runtimeSourceCode = runtimeSourceCode;
}),
);
});
return Promise.all(promises);
}
async function parseSourceAST(
fileNameToHookSourceData: Map<string, HookSourceData>,
): Promise<*> {
// SourceMapConsumer.initialize() does nothing when running in Node (aka Jest)
// because the wasm file is automatically read from the file system
// so we can avoid triggering a warning message about this.
if (!__TEST__) {
if (__DEBUG__) {
console.log('parseSourceAST() Initializing source-map library ...');
}
// $FlowFixMe
const wasmMappingsURL = chrome.extension.getURL('mappings.wasm');
SourceMapConsumer.initialize({'lib/mappings.wasm': wasmMappingsURL});
}
const promises = [];
fileNameToHookSourceData.forEach(hookSourceData => {
if (hookSourceData.originalSourceAST !== null) {
// Use cached metadata.
return;
}
const {runtimeSourceCode, sourceMapContents} = hookSourceData;
if (sourceMapContents !== null) {
// Parse and extract the AST from the source map.
promises.push(
SourceMapConsumer.with(
sourceMapContents,
null,
(sourceConsumer: SourceConsumer) => {
hookSourceData.sourceConsumer = sourceConsumer;
// Now that the source map has been loaded,
// extract the original source for later.
const source = sourceMapContents.sources[0];
const originalSourceCode = sourceConsumer.sourceContentFor(
source,
true,
);
if (__DEBUG__) {
console.groupCollapsed(
'parseSourceAST() Extracted source code from source map',
);
console.log(originalSourceCode);
console.groupEnd();
}
hookSourceData.originalSourceCode = originalSourceCode;
// TODO Parsing should ideally be done off of the main thread.
hookSourceData.originalSourceAST = parse(originalSourceCode, {
sourceType: 'unambiguous',
plugins: ['jsx', 'typescript'],
});
},
),
);
} else {
// There's no source map to parse here so we can just parse the original source itself.
hookSourceData.originalSourceCode = runtimeSourceCode;
// TODO Parsing should ideally be done off of the main thread.
hookSourceData.originalSourceAST = parse(runtimeSourceCode, {
sourceType: 'unambiguous',
plugins: ['jsx', 'typescript'],
});
}
});
return Promise.all(promises);
}
function flattenHooksList(
hooksTree: HooksTree,
hooksList: Array<HooksNode>,
): void {
for (let i = 0; i < hooksTree.length; i++) {
const hook = hooksTree[i];
hooksList.push(hook);
if (hook.subHooks.length > 0) {
flattenHooksList(hook.subHooks, hooksList);
}
}
}
function updateLruCache(
fileNameToHookSourceData: Map<string, HookSourceData>,
): Promise<*> {
fileNameToHookSourceData.forEach(
({originalSourceAST, originalSourceCode, sourceConsumer}, fileName) => {
// Only set once to avoid triggering eviction/cleanup code.
if (!fileNameToMetadataCache.has(fileName)) {
if (__DEBUG__) {
console.log('updateLruCache() Caching metada for "' + fileName + '"');
}
fileNameToMetadataCache.set(fileName, {
originalSourceAST,
originalSourceCode: ((originalSourceCode: any): string),
sourceConsumer,
});
}
},
);
return Promise.resolve();
}