Skip to content

Commit 0b5ba2c

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Make remaining Utilities modules Flow strict-local (#57739)
Summary: Pull Request resolved: #57739 Upgrade `deepDiffer`, `codegenNativeCommands`, `Dimensions`, and `ReactNativeTestTools` from `flow` to `flow strict-local`, preserving both public type signatures and runtime behavior. These modules operate on values of arbitrary shape, so the unavoidable `any` and `Function` usages are marked with scoped `$FlowFixMe[unclear-type]` rather than tightened. `Dimensions.addEventListener` deliberately keeps its original `handler: Function` signature. Flow types object properties invariantly, so any precise payload annotation would reject existing subscribers that annotate the payload with their own object shapes, forcing unrelated call sites to change. The generated API snapshot is therefore unchanged for `Dimensions`. Changelog: [Internal] Reviewed By: javache Differential Revision: D113763789
1 parent 3ae59df commit 0b5ba2c

8 files changed

Lines changed: 41 additions & 23 deletions

File tree

packages/react-native/Libraries/Utilities/Dimensions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

@@ -111,6 +111,8 @@ class Dimensions {
111111
*/
112112
static addEventListener(
113113
type: 'change',
114+
/* $FlowFixMe[unclear-type] Callers annotate the payload with assorted
115+
* object shapes that any precise signature would reject. */
114116
handler: Function,
115117
): EventSubscription {
116118
invariant(

packages/react-native/Libraries/Utilities/ReactNativeTestTools.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

@@ -163,7 +163,9 @@ function maximumDepthOfJSON(node: ?ReactTestRendererJSON): number {
163163
}
164164
}
165165

166-
function renderAndEnforceStrictMode(element: React.Node): any {
166+
function renderAndEnforceStrictMode(
167+
element: React.Node,
168+
): ReactTestRendererType {
167169
expectNoConsoleError();
168170
return renderWithStrictMode(element);
169171
}
@@ -217,7 +219,8 @@ function scrollToBottom(instance: ReactTestInstance) {
217219
// To make error messages a little bit better, we attach a custom toString
218220
// implementation to a predicate
219221
function withMessage(fn: Predicate, message: string): Predicate {
220-
(fn as any).toString = () => message;
222+
// $FlowFixMe[cannot-write] Deliberately overriding toString on the predicate.
223+
fn.toString = () => message;
221224
return fn;
222225
}
223226

packages/react-native/Libraries/Utilities/codegenNativeCommands.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,40 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

11+
import type {HostInstance} from '../../src/private/types/HostInstance';
12+
1113
const {dispatchCommand} = require('../ReactNative/RendererProxy');
1214

1315
type NativeCommandsOptions<T = string> = Readonly<{
1416
supportedCommands: ReadonlyArray<T>,
1517
}>;
1618

19+
declare function castCommandObject<T extends interface {}>(commandObj: {
20+
[keyof T]: (...ReadonlyArray<unknown>) => void,
21+
}): T;
22+
function castCommandObject(commandObj: interface {}) {
23+
return commandObj;
24+
}
25+
1726
function codegenNativeCommands<T extends interface {}>(
18-
options: NativeCommandsOptions<keyof T>,
27+
options: NativeCommandsOptions<keyof T & string>,
1928
): T {
2029
const commandObj: {[keyof T]: (...ReadonlyArray<unknown>) => void} = {};
2130

2231
options.supportedCommands.forEach(command => {
23-
// $FlowFixMe[missing-local-annot]
24-
commandObj[command] = (ref, ...args) => {
25-
// $FlowFixMe[incompatible-type]
32+
commandObj[command] = (
33+
ref: HostInstance,
34+
...args: Array<unknown>
35+
): void => {
2636
dispatchCommand(ref, command, args);
2737
};
2838
});
2939

30-
return commandObj as any as T;
40+
return castCommandObject(commandObj);
3141
}
3242

3343
export default codegenNativeCommands;

packages/react-native/Libraries/Utilities/differ/__tests__/deepDiffer-itest.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('deepDiffer', function () {
136136
expect(
137137
deepDiffer(
138138
() => {},
139-
x => x,
139+
(x: unknown) => x,
140140
),
141141
).toBe(false);
142142
const f = () => {};
@@ -146,7 +146,7 @@ describe('deepDiffer', function () {
146146
expect(
147147
deepDiffer(
148148
() => {},
149-
x => x,
149+
(x: unknown) => x,
150150
undefined,
151151
{unsafelyIgnoreFunctions: false},
152152
),
@@ -160,7 +160,7 @@ describe('deepDiffer', function () {
160160
expect(
161161
deepDiffer(
162162
() => {},
163-
x => x,
163+
(x: unknown) => x,
164164
{unsafelyIgnoreFunctions: false},
165165
),
166166
).toBe(true);

packages/react-native/Libraries/Utilities/differ/deepDiffer.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

@@ -29,8 +29,8 @@ function unstable_setLogListeners(listeners: ?LogListeners) {
2929
* @returns {bool} true if different, false if equal
3030
*/
3131
function deepDiffer(
32-
one: any,
33-
two: any,
32+
one: unknown,
33+
two: unknown,
3434
maxDepthOrOptions: Options | number = -1,
3535
maybeOptions?: Options,
3636
): boolean {
@@ -73,7 +73,9 @@ function deepDiffer(
7373
return true;
7474
}
7575
if (Array.isArray(one)) {
76-
// We know two is also an array because the constructors are equal
76+
if (!Array.isArray(two)) {
77+
return true;
78+
}
7779
const len = one.length;
7880
if (two.length !== len) {
7981
return true;

packages/react-native/Libraries/Utilities/useWindowDimensions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export default function useWindowDimensions():
2727
useEffect(() => {
2828
function handleChange({
2929
window,
30-
}: {
30+
}: Readonly<{
3131
window: DisplayMetrics | DisplayMetricsAndroid,
32-
}) {
32+
...
33+
}>) {
3334
if (
3435
dimensions.width !== window.width ||
3536
dimensions.height !== window.height ||

packages/react-native/ReactNativeApi.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<efd59db9214ec5f9de127d9b01190bc6>>
7+
* @generated SignedSource<<05c2ae417d791bd248bc677e89f0f307>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -1730,7 +1730,7 @@ declare class CellRenderMask {
17301730
declare type Clipboard = typeof Clipboard
17311731
declare type codegenNativeCommands = typeof codegenNativeCommands
17321732
declare function codegenNativeCommands_default<T extends {}>(
1733-
options: NativeCommandsOptions<keyof T>,
1733+
options: NativeCommandsOptions<keyof T & string>,
17341734
): T
17351735
declare type codegenNativeComponent = typeof codegenNativeComponent
17361736
declare function codegenNativeComponent_default<Props extends {}>(
@@ -6051,7 +6051,7 @@ export {
60516051
VirtualizedSectionListInstance, // 12b706d5
60526052
VirtualizedSectionListProps, // 52c34787
60536053
WrapperComponentProvider, // 9b4247f6
6054-
codegenNativeCommands, // 628a7c0a
6054+
codegenNativeCommands, // 322f3f4e
60556055
codegenNativeComponent, // 520daa94
60566056
findNodeHandle, // 93f80214
60576057
processColor, // 6e877698

packages/rn-tester/js/examples/Dimensions/DimensionsExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as React from 'react';
1515
import {useEffect, useState} from 'react';
1616
import {Button, Dimensions, View, useWindowDimensions} from 'react-native';
1717

18-
type Props = {dim: string};
18+
type Props = {dim: 'screen' | 'window'};
1919

2020
function DimensionsSubscription(props: Props) {
2121
const [dims, setDims] = useState(() => Dimensions.get(props.dim));

0 commit comments

Comments
 (0)