From 50a7d464fbad2342d4db514c7dd2b84e2bb8b14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 4 Sep 2026 06:40:51 -0700 Subject: [PATCH] Make assorted Libraries modules Flow strict-local (#57733) Summary: Pull Request resolved: https://github.com/react/react-native/pull/57733 Upgrade several remaining Libraries modules (WebSocket, PanResponder, RCTEventEmitter, and Flow type tests) from `flow` to `flow strict-local` with accurate types. WebSocket exposes spec-mandated getters/setters and opts out of the `unsafe-getters-setters` lint with a scoped directive. Behavior is unchanged. Changelog: [Internal] Reviewed By: javache Differential Revision: D113763779 fbshipit-source-id: 86827a975e996a4c85dd92344d36746e61887dde --- .../Libraries/EventEmitter/RCTEventEmitter.js | 18 ++++++++-- .../Libraries/Interaction/PanResponder.js | 2 +- .../__flowtests__/SectionList-flowtest.js | 4 +-- .../Libraries/WebSocket/WebSocket.js | 34 +++++++++---------- .../ReactNativeTypes-flowtest.js | 4 +-- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/packages/react-native/Libraries/EventEmitter/RCTEventEmitter.js b/packages/react-native/Libraries/EventEmitter/RCTEventEmitter.js index 069caedcb25..3e5bb8e6624 100644 --- a/packages/react-native/Libraries/EventEmitter/RCTEventEmitter.js +++ b/packages/react-native/Libraries/EventEmitter/RCTEventEmitter.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. * - * @flow + * @flow strict-local * @format */ @@ -12,8 +12,22 @@ import registerCallableModule from '../Core/registerCallableModule'; +type EventEmitterModule = { + receiveEvent: ( + rootNodeID: number, + topLevelType: string, + nativeEventParam: unknown, + ) => void, + receiveTouches: ( + eventTopLevelType: string, + touches: Array, + changedIndices: Array, + ) => void, + ... +}; + const RCTEventEmitter = { - register(eventEmitter: any) { + register(eventEmitter: EventEmitterModule) { registerCallableModule('RCTEventEmitter', eventEmitter); }, }; diff --git a/packages/react-native/Libraries/Interaction/PanResponder.js b/packages/react-native/Libraries/Interaction/PanResponder.js index 3c31c1a895c..fbf0a5d5f0b 100644 --- a/packages/react-native/Libraries/Interaction/PanResponder.js +++ b/packages/react-native/Libraries/Interaction/PanResponder.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. * - * @flow + * @flow strict-local * @format */ diff --git a/packages/react-native/Libraries/Lists/__flowtests__/SectionList-flowtest.js b/packages/react-native/Libraries/Lists/__flowtests__/SectionList-flowtest.js index 9f5a7efd200..769e0da3237 100644 --- a/packages/react-native/Libraries/Lists/__flowtests__/SectionList-flowtest.js +++ b/packages/react-native/Libraries/Lists/__flowtests__/SectionList-flowtest.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. * - * @flow + * @flow strict-local * @format */ @@ -22,7 +22,7 @@ function renderMyListItem(info: { const renderMyHeader = ({ section, }: { - section: {fooNumber: number, ...} & Object, + section: {fooNumber: number, ...}, ... }) => ; diff --git a/packages/react-native/Libraries/WebSocket/WebSocket.js b/packages/react-native/Libraries/WebSocket/WebSocket.js index 9c08c3bdd0b..2eccf431b19 100644 --- a/packages/react-native/Libraries/WebSocket/WebSocket.js +++ b/packages/react-native/Libraries/WebSocket/WebSocket.js @@ -4,10 +4,12 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict-local * @format */ +// flowlint unsafe-getters-setters:off + import type {EventCallback} from '../../src/private/webapis/dom/events/EventTarget'; import type {BlobData} from '../Blob/BlobTypes'; import type {EventSubscription} from '../vendor/emitter/EventEmitter'; @@ -98,29 +100,29 @@ class WebSocket extends EventTarget { constructor( url: string, protocols: ?string | ?Array, - options: ?{headers?: {origin?: string, ...}, ...}, + options: ?{headers?: {origin?: string, ...}, origin?: string, ...}, ) { super(); this.url = url; - if (typeof protocols === 'string') { - protocols = [protocols]; - } + const protocolList: ?Array = + typeof protocols === 'string' + ? [protocols] + : Array.isArray(protocols) + ? protocols + : null; - const {headers = {}, ...unrecognized} = options || {}; + const {headers = {}, ...unrecognized} = (options ?? {}) as { + headers?: {origin?: string, ...}, + origin?: string, + ... + }; // Preserve deprecated backwards compatibility for the 'origin' option - // $FlowFixMe[prop-missing] if (unrecognized && typeof unrecognized.origin === 'string') { console.warn( 'Specifying `origin` as a WebSocket connection option is deprecated. Include it under `headers` instead.', ); - /* $FlowFixMe[prop-missing] (>=0.54.0 site=react_native_fb,react_native_ - * oss) This comment suppresses an error found when Flow v0.54 was - * deployed. To see the error delete this comment and run Flow. */ headers.origin = unrecognized.origin; - /* $FlowFixMe[prop-missing] (>=0.54.0 site=react_native_fb,react_native_ - * oss) This comment suppresses an error found when Flow v0.54 was - * deployed. To see the error delete this comment and run Flow. */ delete unrecognized.origin; } @@ -134,10 +136,6 @@ class WebSocket extends EventTarget { ); } - if (!Array.isArray(protocols)) { - protocols = null; - } - this._eventEmitter = new NativeEventEmitter( // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior // If you want to use the native module on other platforms, please remove this condition and test its behavior @@ -149,7 +147,7 @@ class WebSocket extends EventTarget { global.__NETWORK_REPORTER__?.createDevToolsRequestId(); NativeWebSocketModule.connect( url, - protocols, + protocolList, {headers, unstable_devToolsRequestId: devToolsRequestId}, this._socketId, ); diff --git a/packages/react-native/Libraries/__flowtests__/ReactNativeTypes-flowtest.js b/packages/react-native/Libraries/__flowtests__/ReactNativeTypes-flowtest.js index 57c5c891073..38c0a45aac4 100644 --- a/packages/react-native/Libraries/__flowtests__/ReactNativeTypes-flowtest.js +++ b/packages/react-native/Libraries/__flowtests__/ReactNativeTypes-flowtest.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. * - * @flow + * @flow strict-local * @format */ @@ -15,7 +15,7 @@ import * as React from 'react'; function takesHostComponentInstance(instance: HostInstance | null): void {} -const MyHostComponent = 'Host' as any as HostComponent<{...}>; +declare const MyHostComponent: HostComponent<{...}>; {