Skip to content

Commit d9b3037

Browse files
author
Brian Vaughn
committed
Refactored console patching to not require additional renderer injections
1 parent 2ecdc60 commit d9b3037

4 files changed

Lines changed: 247 additions & 123 deletions

File tree

src/backend/console.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// @flow
22

3+
import { getInternalReactConstants } from './renderer';
4+
import describeComponentFrame from './describeComponentFrame';
5+
36
import type { Fiber, ReactRenderer } from './types';
47

58
let isDisabled: boolean = false;
@@ -17,18 +20,22 @@ const FRAME_REGEX = /\n {4}in /;
1720

1821
export function patch(
1922
targetConsole: Object,
20-
{ describeComponentFrame, getComponentName, getCurrentFiber }: ReactRenderer
23+
{ getCurrentFiber, findFiberByHostInstance, version }: ReactRenderer
2124
): void {
2225
if (hasPatched) {
2326
return;
2427
}
2528

29+
if (typeof findFiberByHostInstance !== 'function') {
30+
return;
31+
}
32+
33+
const { getDisplayNameForFiber } = getInternalReactConstants(version);
34+
2635
hasPatched = true;
2736

2837
for (let method in targetConsole) {
2938
const appendComponentStack =
30-
typeof describeComponentFrame === 'function' &&
31-
typeof getComponentName === 'function' &&
3239
typeof getCurrentFiber === 'function' &&
3340
(method === 'error' || method === 'warn' || method === 'trace');
3441

@@ -47,10 +54,10 @@ export function patch(
4754
let current: ?Fiber = getCurrentFiber();
4855
let ownerStack: string = '';
4956
while (current != null) {
50-
const name = getComponentName(current.type);
57+
const name = getDisplayNameForFiber(current);
5158
const owner = current._debugOwner;
5259
const ownerName =
53-
owner != null ? getComponentName(owner.type) : null;
60+
owner != null ? getDisplayNameForFiber(owner) : null;
5461

5562
ownerStack += describeComponentFrame(
5663
name,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// @flow
2+
3+
// This file was forked from the React GitHub repo:
4+
// https://raw.githubusercontent.com/facebook/react/master/packages/shared/describeComponentFrame.js
5+
//
6+
// It has been modified sligthly to add a zero width space as commented below.
7+
8+
const BEFORE_SLASH_RE = /^(.*)[\\/]/;
9+
10+
export default function describeComponentFrame(
11+
name: null | string,
12+
source: any,
13+
ownerName: null | string
14+
) {
15+
let sourceInfo = '';
16+
if (source) {
17+
let path = source.fileName;
18+
let fileName = path.replace(BEFORE_SLASH_RE, '');
19+
if (__DEV__) {
20+
// In DEV, include code for a common special case:
21+
// prefer "folder/index.js" instead of just "index.js".
22+
if (/^index\./.test(fileName)) {
23+
const match = path.match(BEFORE_SLASH_RE);
24+
if (match) {
25+
const pathBeforeSlash = match[1];
26+
if (pathBeforeSlash) {
27+
const folderName = pathBeforeSlash.replace(BEFORE_SLASH_RE, '');
28+
// Note the below string contains a zero width space after the "/" character.
29+
// This is to prevent browsers like Chrome from formatting the file name as a link.
30+
// (Since this is a source link, it would not work to open the source file anyway.)
31+
fileName = folderName + '/​' + fileName;
32+
}
33+
}
34+
}
35+
}
36+
sourceInfo = ' (at ' + fileName + ':' + source.lineNumber + ')';
37+
} else if (ownerName) {
38+
sourceInfo = ' (created by ' + ownerName + ')';
39+
}
40+
return '\n in ' + (name || 'Unknown') + sourceInfo;
41+
}

0 commit comments

Comments
 (0)