From 9647333b3d5a5d2a3ca7fe2a78d2d3da24bc4984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Wed, 10 Jul 2024 13:37:56 -0400 Subject: [PATCH] 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. --- .../shared/forks/consoleWithStackDev.rn.js | 59 +++++++++++++++++++ scripts/rollup/forks.js | 3 + 2 files changed, 62 insertions(+) create mode 100644 packages/shared/forks/consoleWithStackDev.rn.js diff --git a/packages/shared/forks/consoleWithStackDev.rn.js b/packages/shared/forks/consoleWithStackDev.rn.js new file mode 100644 index 0000000000000..6b567a446a647 --- /dev/null +++ b/packages/shared/forks/consoleWithStackDev.rn.js @@ -0,0 +1,59 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import ReactSharedInternals from 'shared/ReactSharedInternals'; + +let suppressWarning = false; +export function setSuppressWarning(newSuppressWarning) { + if (__DEV__) { + suppressWarning = newSuppressWarning; + } +} + +// In DEV, calls to console.warn and console.error get replaced +// by calls to these methods by a Babel plugin. +// +// In PROD (or in packages without access to React internals), +// they are left as they are instead. + +export function warn(format, ...args) { + if (__DEV__) { + if (!suppressWarning) { + printWarning('warn', format, args, new Error('react-stack-top-frame')); + } + } +} + +export function error(format, ...args) { + if (__DEV__) { + if (!suppressWarning) { + printWarning('error', format, args, new Error('react-stack-top-frame')); + } + } +} + +export let isWritingAppendedStack = false; + +function printWarning(level, format, args, currentStack) { + if (__DEV__) { + if (ReactSharedInternals.getCurrentStack) { + const stack = ReactSharedInternals.getCurrentStack(currentStack); + if (stack !== '') { + isWritingAppendedStack = true; + format += '%s'; + args = args.concat([stack]); + } + } + + args.unshift(format); + // We intentionally don't use spread (or .apply) directly because it + // breaks IE9: https://github.com/facebook/react/issues/13610 + // eslint-disable-next-line react-internal/no-production-logging + Function.prototype.apply.call(console[level], console, args); + isWritingAppendedStack = false; + } +} diff --git a/scripts/rollup/forks.js b/scripts/rollup/forks.js index cffc66d2324f3..9dcbc6c0b77a5 100644 --- a/scripts/rollup/forks.js +++ b/scripts/rollup/forks.js @@ -209,6 +209,9 @@ const forks = Object.freeze({ switch (bundleType) { case FB_WWW_DEV: return './packages/shared/forks/consoleWithStackDev.www.js'; + case RN_OSS_DEV: + case RN_FB_DEV: + return './packages/shared/forks/consoleWithStackDev.rn.js'; default: return null; }