Skip to content

Commit 9647333

Browse files
authored
Add RN fork of consoleWithStackDev so we can improve the mainline one (#30305)
We're removing this wrapper from the mainline but RN is still using component stacks to filter out warnings. This is unfortunate since it'll be hard to keep track of the interplay with these, DevTools and how you're supposed to implement error dialogs in userspace.
1 parent 2d3f81b commit 9647333

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import ReactSharedInternals from 'shared/ReactSharedInternals';
9+
10+
let suppressWarning = false;
11+
export function setSuppressWarning(newSuppressWarning) {
12+
if (__DEV__) {
13+
suppressWarning = newSuppressWarning;
14+
}
15+
}
16+
17+
// In DEV, calls to console.warn and console.error get replaced
18+
// by calls to these methods by a Babel plugin.
19+
//
20+
// In PROD (or in packages without access to React internals),
21+
// they are left as they are instead.
22+
23+
export function warn(format, ...args) {
24+
if (__DEV__) {
25+
if (!suppressWarning) {
26+
printWarning('warn', format, args, new Error('react-stack-top-frame'));
27+
}
28+
}
29+
}
30+
31+
export function error(format, ...args) {
32+
if (__DEV__) {
33+
if (!suppressWarning) {
34+
printWarning('error', format, args, new Error('react-stack-top-frame'));
35+
}
36+
}
37+
}
38+
39+
export let isWritingAppendedStack = false;
40+
41+
function printWarning(level, format, args, currentStack) {
42+
if (__DEV__) {
43+
if (ReactSharedInternals.getCurrentStack) {
44+
const stack = ReactSharedInternals.getCurrentStack(currentStack);
45+
if (stack !== '') {
46+
isWritingAppendedStack = true;
47+
format += '%s';
48+
args = args.concat([stack]);
49+
}
50+
}
51+
52+
args.unshift(format);
53+
// We intentionally don't use spread (or .apply) directly because it
54+
// breaks IE9: https://github.com/facebook/react/issues/13610
55+
// eslint-disable-next-line react-internal/no-production-logging
56+
Function.prototype.apply.call(console[level], console, args);
57+
isWritingAppendedStack = false;
58+
}
59+
}

scripts/rollup/forks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ const forks = Object.freeze({
209209
switch (bundleType) {
210210
case FB_WWW_DEV:
211211
return './packages/shared/forks/consoleWithStackDev.www.js';
212+
case RN_OSS_DEV:
213+
case RN_FB_DEV:
214+
return './packages/shared/forks/consoleWithStackDev.rn.js';
212215
default:
213216
return null;
214217
}

0 commit comments

Comments
 (0)