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
This commit is contained in:
Oskar Eichler authored and meta-codesync[bot] committed 2026-09-17 04:59:19 -07:00
1 parent 86ba0c6334
commit 665beff9df
2 files changed
+25 -3

No files matched your search

@@ -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);
});
});
@@ -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' &&