Fix missing stacks in WWW warnings (#17638)
Dan Abramov committed
Dec 17, 2019 at 15:21 UTC
3c54df0914f02fed146faa519a5a899d0e3af32e
2 files changed
+39
-9
packages/shared/consoleWithStackDev.js
+2
@@ -26,6 +26,8 @@ export function error(format, ...args) {
26
}
27
28
function printWarning(level, format, args) {
29
+ // When changing this logic, you might want to also
30
+ // update consoleWithStackDev.www.js as well.
31
if (__DEV__) {
32
const hasExistingStack =
33
args.length > 0 &&
packages/shared/forks/consoleWithStackDev.www.js
+37
-9
@@ -8,15 +8,43 @@
8
// This refers to a WWW module.
9
const warningWWW = require('warning');
10
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
+ }
15
}
16
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
+ }
50
}