Skip to content

Commit 3c54df0

Browse files
authored
Fix missing stacks in WWW warnings (#17638)
1 parent b66e86d commit 3c54df0

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

packages/shared/consoleWithStackDev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function error(format, ...args) {
2626
}
2727

2828
function printWarning(level, format, args) {
29+
// When changing this logic, you might want to also
30+
// update consoleWithStackDev.www.js as well.
2931
if (__DEV__) {
3032
const hasExistingStack =
3133
args.length > 0 &&

packages/shared/forks/consoleWithStackDev.www.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,43 @@
88
// This refers to a WWW module.
99
const warningWWW = require('warning');
1010

11-
export function warn() {
12-
// TODO: use different level for "warn".
13-
const args = Array.prototype.slice.call(arguments);
14-
args.unshift(false);
15-
warningWWW.apply(null, args);
11+
export function warn(format, ...args) {
12+
if (__DEV__) {
13+
printWarning('warn', format, args);
14+
}
1615
}
1716

18-
export function error() {
19-
const args = Array.prototype.slice.call(arguments);
20-
args.unshift(false);
21-
warningWWW.apply(null, args);
17+
export function error(format, ...args) {
18+
if (__DEV__) {
19+
printWarning('error', format, args);
20+
}
21+
}
22+
23+
function printWarning(level, format, args) {
24+
if (__DEV__) {
25+
const hasExistingStack =
26+
args.length > 0 &&
27+
typeof args[args.length - 1] === 'string' &&
28+
args[args.length - 1].indexOf('\n in') === 0;
29+
30+
if (!hasExistingStack) {
31+
const React = require('react');
32+
const ReactSharedInternals =
33+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
34+
// Defensive in case this is fired before React is initialized.
35+
if (ReactSharedInternals != null) {
36+
const ReactDebugCurrentFrame =
37+
ReactSharedInternals.ReactDebugCurrentFrame;
38+
const stack = ReactDebugCurrentFrame.getStackAddendum();
39+
if (stack !== '') {
40+
format += '%s';
41+
args.push(stack);
42+
}
43+
}
44+
}
45+
// TODO: don't ignore level and pass it down somewhere too.
46+
args.unshift(format);
47+
args.unshift(false);
48+
warningWWW.apply(null, args);
49+
}
2250
}

0 commit comments

Comments
 (0)