From 96a8a3da48ddd8b6d570b8510f0560c247f9d013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Wed, 12 Feb 2025 04:07:10 -0800 Subject: [PATCH] Only log errors accessing feature flags in JS if the native module itself is not defined (#49350) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49350 Changelog: [internal] Reviewed By: GijsWeterings Differential Revision: D69520832 fbshipit-source-id: fb72103705c930eb5f0944c931b4fdeabde841ae --- .../js/ReactNativeFeatureFlags.js-template.js | 2 +- .../featureflags/ReactNativeFeatureFlags.js | 4 ++-- .../featureflags/ReactNativeFeatureFlagsBase.js | 11 ++++------- .../__tests__/ReactNativeFeatureFlags-test.js | 16 ++-------------- 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/packages/react-native/scripts/featureflags/templates/js/ReactNativeFeatureFlags.js-template.js b/packages/react-native/scripts/featureflags/templates/js/ReactNativeFeatureFlags.js-template.js index 96fd0417680..5bf488ba9fb 100644 --- a/packages/react-native/scripts/featureflags/templates/js/ReactNativeFeatureFlags.js-template.js +++ b/packages/react-native/scripts/featureflags/templates/js/ReactNativeFeatureFlags.js-template.js @@ -75,7 +75,7 @@ ${Object.entries(definitions.common) */ export const ${flagName}: Getter<${typeof flagConfig.defaultValue}> = createNativeFlagGetter('${flagName}', ${JSON.stringify( flagConfig.defaultValue, - )}${flagConfig.skipNativeAPI === true ? ', true' : ''});`, + )});`, ) .join('\n')} diff --git a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js index cbe0eec4dd8..4a93f78d63a 100644 --- a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<7f675a8e759b564728cfc96c1a3af7b1>> + * @generated SignedSource<<4c34b78c4af6de8e0784ecade5f2e593>> * @flow strict */ @@ -176,7 +176,7 @@ export const commonTestFlag: Getter = createNativeFlagGetter('commonTes /** * Common flag for testing (without native implementation). Do NOT modify. */ -export const commonTestFlagWithoutNativeImplementation: Getter = createNativeFlagGetter('commonTestFlagWithoutNativeImplementation', false, true); +export const commonTestFlagWithoutNativeImplementation: Getter = createNativeFlagGetter('commonTestFlagWithoutNativeImplementation', false); /** * Prevent FabricMountingManager from reordering mountitems, which may lead to invalid state on the UI thread */ diff --git a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlagsBase.js b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlagsBase.js index 9c3663cdfe6..1f4818c3991 100644 --- a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlagsBase.js +++ b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlagsBase.js @@ -67,11 +67,8 @@ export function createNativeFlagGetter>( return createGetter( configName, () => { - const valueFromNative = NativeReactNativeFeatureFlags?.[configName]?.(); - if (valueFromNative == null && !skipUnavailableNativeModuleError) { - logUnavailableNativeModuleError(configName); - } - return valueFromNative; + maybeLogUnavailableNativeModuleError(configName); + return NativeReactNativeFeatureFlags?.[configName]?.(); }, defaultValue, ); @@ -100,8 +97,8 @@ export function setOverrides( const reportedConfigNames: Set = new Set(); -function logUnavailableNativeModuleError(configName: string): void { - if (!reportedConfigNames.has(configName)) { +function maybeLogUnavailableNativeModuleError(configName: string): void { + if (!NativeReactNativeFeatureFlags && !reportedConfigNames.has(configName)) { reportedConfigNames.add(configName); console.error( `Could not access feature flag '${configName}' because native module method was not available`, diff --git a/packages/react-native/src/private/featureflags/__tests__/ReactNativeFeatureFlags-test.js b/packages/react-native/src/private/featureflags/__tests__/ReactNativeFeatureFlags-test.js index 3a46809c65d..f9faf5ed997 100644 --- a/packages/react-native/src/private/featureflags/__tests__/ReactNativeFeatureFlags-test.js +++ b/packages/react-native/src/private/featureflags/__tests__/ReactNativeFeatureFlags-test.js @@ -26,16 +26,7 @@ describe('ReactNativeFeatureFlags', () => { ); }); - it('should provide default values for common flags and NOT log an error if the native module is NOT available and `skipNativeAPI` is used', () => { - const ReactNativeFeatureFlags = require('../ReactNativeFeatureFlags'); - expect( - ReactNativeFeatureFlags.commonTestFlagWithoutNativeImplementation(), - ).toBe(false); - - expect(console.error).toHaveBeenCalledTimes(0); - }); - - it('should provide default values for common flags and log an error if the method in the native module is NOT available', () => { + it('should provide default values for common flags and NOT log an error if the native module is available but the method is NOT defined', () => { jest.doMock('../specs/NativeReactNativeFeatureFlags', () => ({ __esModule: true, default: {}, @@ -44,10 +35,7 @@ describe('ReactNativeFeatureFlags', () => { const ReactNativeFeatureFlags = require('../ReactNativeFeatureFlags'); expect(ReactNativeFeatureFlags.commonTestFlag()).toBe(false); - expect(console.error).toHaveBeenCalledTimes(1); - expect(console.error).toHaveBeenCalledWith( - "Could not access feature flag 'commonTestFlag' because native module method was not available", - ); + expect(console.error).toHaveBeenCalledTimes(0); }); it('should access and cache common flags from the native module if it is available', () => {