@samitouri / QOS-React-2 / commits / 9647333b3d

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.

Sebastian Markbåge committed Jul 10, 2024 at 13:37 UTC 9647333b3d5a5d2a3ca7fe2a78d2d3da24bc4984
2 files changed +62
packages/shared/forks/consoleWithStackDev.rn.js new
+59
@@ -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
+3
@@ -209,6 +209,9 @@ const forks = Object.freeze({
209 switch (bundleType) {
210 case FB_WWW_DEV:
211 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';
215 default:
216 return null;
217 }