From 665beff9df7a32f157abe630d087a05d5dc48073 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Thu, 17 Sep 2026 04:59:19 -0700 Subject: [PATCH] Fix native component event property checks (#58195) Summary: Legacy native component event merging invokes `hasOwnProperty` directly on native/default event maps. An own event named `hasOwnProperty` shadows that method and crashes component initialization. Use a bound intrinsic own-property check for both source and destination maps, and add an exact regression. Hosted Flow caught two stale-server misses in the first patches: method unbinding/exact-object inference, then the deprecated `mixed` annotation. The final head uses the repository-standard suppression around the captured intrinsic and an explicit `unknown` indexer for the mutable test map. Fixes https://github.com/react/react-native/issues/58194. ## Changelog: [GENERAL] [FIXED] - Handle native component event names that shadow Object prototype properties. Pull Request resolved: https://github.com/react/react-native/pull/58195 Test Plan: - Focused `getNativeComponentAttributes` Jest suite: 2 tests passed on the original regression/fix run. - Final-head targeted ESLint, Prettier check, and `git diff --check` passed. - Hosted full Flow/lint passed on the final amended head. No UI change; screenshots are not applicable. Reviewed By: cipolleschi Differential Revision: D120117546 Pulled By: vzaidman fbshipit-source-id: f4e1cbf6c68bd33ff98ca8df4864b828ac31179b --- .../getNativeComponentAttributes-test.js | 17 ++++++++++++++++- .../ReactNative/getNativeComponentAttributes.js | 11 +++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/react-native/Libraries/ReactNative/__tests__/getNativeComponentAttributes-test.js b/packages/react-native/Libraries/ReactNative/__tests__/getNativeComponentAttributes-test.js index 5f4d6196476..5f23200fa7d 100644 --- a/packages/react-native/Libraries/ReactNative/__tests__/getNativeComponentAttributes-test.js +++ b/packages/react-native/Libraries/ReactNative/__tests__/getNativeComponentAttributes-test.js @@ -10,11 +10,13 @@ 'use strict'; +let mockDefaultEventTypes: {[string]: unknown} = {}; + jest.mock('../UIManager', () => ({ __esModule: true, default: { getConstants: () => ({ViewManagerNames: []}), - getDefaultEventTypes: () => ({}), + getDefaultEventTypes: () => mockDefaultEventTypes, getViewManagerConfig: name => name === 'TestView' ? { @@ -33,6 +35,10 @@ const getNativeComponentAttributes = require('../getNativeComponentAttributes').default; describe('getNativeComponentAttributes', () => { + beforeEach(() => { + mockDefaultEventTypes = {}; + }); + it('processes object font variation settings from native view configs', () => { const viewConfig = getNativeComponentAttributes('TestView'); @@ -46,4 +52,13 @@ describe('getNativeComponentAttributes', () => { }), ).toBe("'opsz' 17.25, 'wght' 552.5"); }); + + it('merges event types that shadow Object prototype properties', () => { + const hasOwnPropertyEvent = {registrationName: 'onHasOwnProperty'}; + mockDefaultEventTypes = {hasOwnProperty: hasOwnPropertyEvent}; + + const viewConfig = getNativeComponentAttributes('TestView'); + + expect(viewConfig.hasOwnProperty).toBe(hasOwnPropertyEvent); + }); }); diff --git a/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js b/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js index d42571f88d0..69b2486f09c 100644 --- a/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js +++ b/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js @@ -138,6 +138,9 @@ function attachDefaultEventTypes( } } +// $FlowFixMe[method-unbinding] +const hasOwnProperty = Object.prototype.hasOwnProperty; + // TODO: Figure out how to avoid all this runtime initialization cost. function merge( destination: ?ViewManagerConfig, @@ -151,12 +154,16 @@ function merge( } for (const key in source) { - if (!source.hasOwnProperty(key)) { + /* $FlowFixMe[invalid-this-arg] Error exposed after fixing this typing + * unsoundness in flow */ + if (!hasOwnProperty.call(source, key)) { continue; } let sourceValue = source[key]; - if (destination.hasOwnProperty(key)) { + /* $FlowFixMe[invalid-this-arg] Error exposed after fixing this typing + * unsoundness in flow */ + if (hasOwnProperty.call(destination, key)) { const destinationValue = destination[key]; if ( typeof sourceValue === 'object' &&