From 949296571b36080d4ebe0bc8fac9ef3907367475 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Fri, 15 Mar 2024 04:23:57 -0700 Subject: [PATCH] Console polyfill: preserve unknown methods from originalConsole if found (#43459) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43459 Changelog: [General][Changed] - Console polyfill now copies all properties from the existing `console` object Fusebox now exposes a full WHATWG `console` object integrated directly with CDP debugging (D54826073). Some WHATWG `console` methods are missing from React Native's polyfill/shim, so let's pass those through unmodified so they can still be called. (Long term, we shouldn't need most of `console.js`, but let's get there gradually as there are many RN users still depending on `nativeLoggingHook` etc.) NOTE: We also update the "bundled" copy of `console.js` that lives in the jsinspector-modern C++ test suite. Reviewed By: huntie Differential Revision: D54827902 fbshipit-source-id: c6c9128903496810192614f4f8d80b68b02e25c4 --- packages/polyfills/console.js | 6 +++++- .../ReactCommon/jsinspector-modern/tests/prelude.js.h | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/polyfills/console.js b/packages/polyfills/console.js index 338e1590cb..aac13350cc 100644 --- a/packages/polyfills/console.js +++ b/packages/polyfills/console.js @@ -554,6 +554,7 @@ if (global.nativeLoggingHook) { } global.console = { + ...(originalConsole ?? {}), error: getNativeLogFunction(LOG_LEVELS.error), info: getNativeLogFunction(LOG_LEVELS.info), log: getNativeLogFunction(LOG_LEVELS.info), @@ -578,7 +579,10 @@ if (global.nativeLoggingHook) { if (__DEV__ && originalConsole) { Object.keys(console).forEach(methodName => { const reactNativeMethod = console[methodName]; - if (originalConsole[methodName]) { + if ( + originalConsole[methodName] && + reactNativeMethod !== originalConsole[methodName] + ) { console[methodName] = function () { originalConsole[methodName](...arguments); reactNativeMethod.apply(console, arguments); diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tests/prelude.js.h b/packages/react-native/ReactCommon/jsinspector-modern/tests/prelude.js.h index 31d00ca9df..994222a3d5 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tests/prelude.js.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tests/prelude.js.h @@ -553,6 +553,7 @@ if (global.nativeLoggingHook) { } global.console = { + ...(originalConsole ?? {}), error: getNativeLogFunction(LOG_LEVELS.error), info: getNativeLogFunction(LOG_LEVELS.info), log: getNativeLogFunction(LOG_LEVELS.info), @@ -577,7 +578,10 @@ if (global.nativeLoggingHook) { if (__DEV__ && originalConsole) { Object.keys(console).forEach(methodName => { const reactNativeMethod = console[methodName]; - if (originalConsole[methodName]) { + if ( + originalConsole[methodName] && + reactNativeMethod !== originalConsole[methodName] + ) { console[methodName] = function () { originalConsole[methodName](...arguments); reactNativeMethod.apply(console, arguments);