Skip to content

Commit 58862fc

Browse files
committed
Use the debug location in parent Component Stacks if available
As a precaution I double check that it has the name in it.
1 parent 1496d14 commit 58862fc

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/react-reconciler/src/ReactFiberComponentStack.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
7979
for (let i = debugInfo.length - 1; i >= 0; i--) {
8080
const entry = debugInfo[i];
8181
if (typeof entry.name === 'string') {
82-
info += describeDebugInfoFrame(entry.name, entry.env);
82+
info += describeDebugInfoFrame(
83+
entry.name,
84+
entry.env,
85+
entry.debugLocation,
86+
);
8387
}
8488
}
8589
}

packages/react-server/src/ReactFizzComponentStack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function describeComponentStackByType(
8686
}
8787
}
8888
if (typeof type.name === 'string') {
89-
return describeDebugInfoFrame(type.name, type.env);
89+
return describeDebugInfoFrame(type.name, type.env, type.debugLocation);
9090
}
9191
}
9292
switch (type) {

packages/shared/ReactComponentStackFrame.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import ReactSharedInternals from 'shared/ReactSharedInternals';
1313

1414
import DefaultPrepareStackTrace from 'shared/DefaultPrepareStackTrace';
1515

16+
import {formatOwnerStack} from './ReactOwnerStackFrames';
17+
1618
let prefix;
1719
let suffix;
1820
export function describeBuiltInComponentFrame(name: string): string {
@@ -38,7 +40,24 @@ export function describeBuiltInComponentFrame(name: string): string {
3840
return '\n' + prefix + name + suffix;
3941
}
4042

41-
export function describeDebugInfoFrame(name: string, env: ?string): string {
43+
export function describeDebugInfoFrame(
44+
name: string,
45+
env: ?string,
46+
location: ?Error,
47+
): string {
48+
if (location != null) {
49+
// If we have a location, it's the child's owner stack. Treat the bottom most frame as
50+
// the location of this function.
51+
const childStack = formatOwnerStack(location);
52+
const idx = childStack.lastIndexOf('\n');
53+
const lastLine = idx === -1 ? childStack : childStack.slice(idx + 1);
54+
if (lastLine.indexOf(name) !== -1) {
55+
// For async stacks it's possible we don't have the owner on it. As a precaution only
56+
// use this frame if it has the name of the function in it.
57+
return '\n' + lastLine;
58+
}
59+
}
60+
4261
return describeBuiltInComponentFrame(name + (env ? ' [' + env + ']' : ''));
4362
}
4463

0 commit comments

Comments
 (0)