Skip to content

Commit 4654f11

Browse files
author
Brian Vaughn
committed
Updated auto-appended component stack to be owners-only
1 parent da7cdec commit 4654f11

3 files changed

Lines changed: 64 additions & 15 deletions

File tree

src/backend/console.js

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

3-
import type { ReactRenderer } from './types';
3+
import type { Fiber, ReactRenderer } from './types';
44

55
let disabled: boolean = false;
66

@@ -12,24 +12,79 @@ export function enable(): void {
1212
disabled = false;
1313
}
1414

15-
export function patch({ debugCurrentFrame }: ReactRenderer): void {
15+
export function patch(
16+
{ getCurrentFiber }: ReactRenderer,
17+
getDisplayNameForFiber: (fiber: Fiber) => string | null
18+
): void {
1619
for (let method in console) {
1720
const originalMethod = console[method];
1821
const appendComponentStack =
19-
debugCurrentFrame != null &&
22+
typeof getCurrentFiber === 'function' &&
2023
(method === 'error' || method === 'warn' || method === 'trace');
2124
try {
2225
// $FlowFixMe property error|warn is not writable.
2326
console[method] = (...args) => {
2427
if (disabled) return;
28+
2529
if (appendComponentStack) {
26-
const componentStack = debugCurrentFrame.getStackAddendum();
27-
if (componentStack) {
28-
args.push(componentStack);
30+
// $FlowFixMe We know getCurrentFiber() is a function if appendComponentStack is true.
31+
let current: ?Fiber = getCurrentFiber();
32+
let ownerStack: string = '';
33+
while (current != null) {
34+
const name = getDisplayNameForFiber(current);
35+
const owner = current._debugOwner;
36+
const ownerName =
37+
owner != null ? getDisplayNameForFiber(owner) : null;
38+
39+
ownerStack += describeComponentFrame(
40+
name,
41+
current._debugSource,
42+
ownerName
43+
);
44+
45+
current = owner;
46+
}
47+
48+
if (ownerStack !== '') {
49+
args.push(ownerStack);
2950
}
3051
}
3152
originalMethod(...args);
3253
};
3354
} catch (error) {}
3455
}
3556
}
57+
58+
const BEFORE_SLASH_RE = /^(.*)[\\/]/;
59+
60+
// Copied from React repo:
61+
// https://github.com/facebook/react/blob/master/packages/shared/describeComponentFrame.js
62+
function describeComponentFrame(
63+
name: null | string,
64+
source: any,
65+
ownerName: null | string
66+
) {
67+
let sourceInfo = '';
68+
if (source) {
69+
let path = source.fileName;
70+
let fileName = path.replace(BEFORE_SLASH_RE, '');
71+
if (__DEV__) {
72+
// In DEV, include code for a common special case:
73+
// prefer "folder/index.js" instead of just "index.js".
74+
if (/^index\./.test(fileName)) {
75+
const match = path.match(BEFORE_SLASH_RE);
76+
if (match) {
77+
const pathBeforeSlash = match[1];
78+
if (pathBeforeSlash) {
79+
const folderName = pathBeforeSlash.replace(BEFORE_SLASH_RE, '');
80+
fileName = folderName + '/' + fileName;
81+
}
82+
}
83+
}
84+
}
85+
sourceInfo = ' (at ' + fileName + ':' + source.lineNumber + ')';
86+
} else if (ownerName) {
87+
sourceInfo = ' (created by ' + ownerName + ')';
88+
}
89+
return '\n in ' + (name || 'Unknown') + sourceInfo;
90+
}

src/backend/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export function attach(
275275
// Patching the console enables DevTools to do a few useful things:
276276
// * Append component stacks to warnings and error messages
277277
// * Disable logging during re-renders to inspect hooks (see inspectHooksOfFiber)
278-
patchConsole(renderer);
278+
patchConsole(renderer, getDisplayNameForFiber);
279279

280280
const {
281281
overrideHookState,

src/backend/types.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ export type GetFiberIDForNative = (
9191
) => number | null;
9292
export type FindNativeNodesForFiberID = (id: number) => ?Array<NativeType>;
9393

94-
type ReactDebugCurrentFrame = {|
95-
// Stack implementation injected by the current renderer.
96-
getCurrentStack: () => string,
97-
getStackAddendum: () => string,
98-
|};
99-
10094
export type ReactRenderer = {
10195
findFiberByHostInstance: (hostInstance: NativeType) => ?Fiber,
10296
version: string,
@@ -125,8 +119,8 @@ export type ReactRenderer = {
125119
currentDispatcherRef?: {| current: null | Dispatcher |},
126120

127121
// Only injected by React v16.9+ in DEV mode.
128-
// Enables DevTools to append component stack to error messages.
129-
debugCurrentFrame: ReactDebugCurrentFrame,
122+
// Enables DevTools to append owners-only component stack to error messages.
123+
getCurrentFiber?: () => Fiber | null,
130124

131125
// <= 15
132126
Mount?: any,

0 commit comments

Comments
 (0)