7
* @flow
8
*/
9
10
-import type {Source} from 'shared/ReactElementType';
10
import type {LazyComponent} from 'react/src/ReactLazy';
11
12
import {enableComponentStackLocations} from 'shared/ReactFeatureFlags';
28
let prefix;
29
export function describeBuiltInComponentFrame(
30
name: string,
32
- source: void | null | Source,
31
ownerFn: void | null | Function,
32
): string {
33
if (enableComponentStackLocations) {
47
if (__DEV__ && ownerFn) {
48
ownerName = ownerFn.displayName || ownerFn.name || null;
49
}
52
- return describeComponentFrame(name, source, ownerName);
50
+ return describeComponentFrame(name, ownerName);
51
}
52
}
53
291
return syntheticFrame;
292
}
293
296
-const BEFORE_SLASH_RE = /^(.*)[\\\/]/;
297
-
298
-function describeComponentFrame(
299
- name: null | string,
300
- source: void | null | Source,
301
- ownerName: null | string,
302
-) {
294
+function describeComponentFrame(name: null | string, ownerName: null | string) {
295
let sourceInfo = '';
304
- if (__DEV__ && source) {
305
- const path = source.fileName;
306
- let fileName = path.replace(BEFORE_SLASH_RE, '');
307
- // In DEV, include code for a common special case:
308
- // prefer "folder/index.js" instead of just "index.js".
309
- if (/^index\./.test(fileName)) {
310
- const match = path.match(BEFORE_SLASH_RE);
311
- if (match) {
312
- const pathBeforeSlash = match[1];
313
- if (pathBeforeSlash) {
314
- const folderName = pathBeforeSlash.replace(BEFORE_SLASH_RE, '');
315
- fileName = folderName + '/' + fileName;
316
- }
317
- }
318
- }
319
- sourceInfo = ' (at ' + fileName + ':' + source.lineNumber + ')';
320
- } else if (ownerName) {
296
+ if (ownerName) {
297
sourceInfo = ' (created by ' + ownerName + ')';
298
}
299
return '\n in ' + (name || 'Unknown') + sourceInfo;
301
302
export function describeClassComponentFrame(
303
ctor: Function,
328
- source: void | null | Source,
304
ownerFn: void | null | Function,
305
): string {
306
if (enableComponentStackLocations) {
307
return describeNativeComponentFrame(ctor, true);
308
} else {
334
- return describeFunctionComponentFrame(ctor, source, ownerFn);
309
+ return describeFunctionComponentFrame(ctor, ownerFn);
310
}
311
}
312
313
export function describeFunctionComponentFrame(
314
fn: Function,
340
- source: void | null | Source,
315
ownerFn: void | null | Function,
316
): string {
317
if (enableComponentStackLocations) {
325
if (__DEV__ && ownerFn) {
326
ownerName = ownerFn.displayName || ownerFn.name || null;
327
}
354
- return describeComponentFrame(name, source, ownerName);
328
+ return describeComponentFrame(name, ownerName);
329
}
330
}
331
336
337
export function describeUnknownElementTypeFrameInDEV(
338
type: any,
365
- source: void | null | Source,
339
ownerFn: void | null | Function,
340
): string {
341
if (!__DEV__) {
348
if (enableComponentStackLocations) {
349
return describeNativeComponentFrame(type, shouldConstruct(type));
350
} else {
378
- return describeFunctionComponentFrame(type, source, ownerFn);
351
+ return describeFunctionComponentFrame(type, ownerFn);
352
}
353
}
354
if (typeof type === 'string') {
382
- return describeBuiltInComponentFrame(type, source, ownerFn);
355
+ return describeBuiltInComponentFrame(type, ownerFn);
356
}
357
switch (type) {
358
case REACT_SUSPENSE_TYPE:
386
- return describeBuiltInComponentFrame('Suspense', source, ownerFn);
359
+ return describeBuiltInComponentFrame('Suspense', ownerFn);
360
case REACT_SUSPENSE_LIST_TYPE:
388
- return describeBuiltInComponentFrame('SuspenseList', source, ownerFn);
361
+ return describeBuiltInComponentFrame('SuspenseList', ownerFn);
362
}
363
if (typeof type === 'object') {
364
switch (type.$$typeof) {
365
case REACT_FORWARD_REF_TYPE:
393
- return describeFunctionComponentFrame(type.render, source, ownerFn);
366
+ return describeFunctionComponentFrame(type.render, ownerFn);
367
case REACT_MEMO_TYPE:
368
// Memo may contain any component type so we recursively resolve it.
396
- return describeUnknownElementTypeFrameInDEV(type.type, source, ownerFn);
369
+ return describeUnknownElementTypeFrameInDEV(type.type, ownerFn);
370
case REACT_LAZY_TYPE: {
371
const lazyComponent: LazyComponent<any, any> = (type: any);
372
const payload = lazyComponent._payload;
373
const init = lazyComponent._init;
374
try {
375
// Lazy may contain any component type so we recursively resolve it.
403
- return describeUnknownElementTypeFrameInDEV(
404
- init(payload),
405
- source,
406
- ownerFn,
407
- );
376
+ return describeUnknownElementTypeFrameInDEV(init(payload), ownerFn);
377
} catch (x) {}
378
}
379
}