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
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2025-02-12 04:07:10 -08:00
1 parent 1d7de4261f
commit 96a8a3da48
4 files changed
+9 -24

No files matched your search

@@ -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')}
@@ -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<boolean> = createNativeFlagGetter('commonTes
/**
* Common flag for testing (without native implementation). Do NOT modify.
*/
export const commonTestFlagWithoutNativeImplementation: Getter<boolean> = createNativeFlagGetter('commonTestFlagWithoutNativeImplementation', false, true);
export const commonTestFlagWithoutNativeImplementation: Getter<boolean> = createNativeFlagGetter('commonTestFlagWithoutNativeImplementation', false);
/**
* Prevent FabricMountingManager from reordering mountitems, which may lead to invalid state on the UI thread
*/
@@ -67,11 +67,8 @@ export function createNativeFlagGetter<K: $Keys<NativeFeatureFlags>>(
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<string> = 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`,
@@ -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', () => {