Skip to content

Commit 5250b59

Browse files
panagosg7facebook-github-bot
authored andcommitted
manual annotations in preparation of natural inference rollout in xplat/js (facebook#51487)
Summary: Pull Request resolved: facebook#51487 The Flow team is improving the way Flow infers type for primitive literals. This diff prepares the codebase for the new behavior by adding type annotations, or annotations of the form `'abc' as const`. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D75114156 fbshipit-source-id: e3175af85cdd2388c3b45af4beb314f334e3f9b5
1 parent 9af86ab commit 5250b59

File tree

8 files changed

+85
-83
lines changed

8 files changed

+85
-83
lines changed

packages/react-native-fantom/src/__tests__/expect-itest.js

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe('expect', () => {
248248
}).toThrow();
249249
});
250250

251-
['toBeCalled', 'toHaveBeenCalled'].map(toHaveBeenCalledAlias =>
251+
(['toBeCalled', 'toHaveBeenCalled'] as const).map(toHaveBeenCalledAlias =>
252252
test(toHaveBeenCalledAlias, () => {
253253
const fn = jest.fn();
254254

@@ -277,87 +277,89 @@ describe('expect', () => {
277277
}),
278278
);
279279

280-
['toBeCalledTimes', 'toHaveBeenCalledTimes'].map(toHaveBeenCalledTimesAlias =>
281-
test(toHaveBeenCalledTimesAlias, () => {
282-
const fn = jest.fn();
280+
(['toBeCalledTimes', 'toHaveBeenCalledTimes'] as const).map(
281+
toHaveBeenCalledTimesAlias =>
282+
test(toHaveBeenCalledTimesAlias, () => {
283+
const fn = jest.fn();
283284

284-
expect(fn)[toHaveBeenCalledTimesAlias](0);
285-
expect(fn).not[toHaveBeenCalledTimesAlias](1);
285+
expect(fn)[toHaveBeenCalledTimesAlias](0);
286+
expect(fn).not[toHaveBeenCalledTimesAlias](1);
286287

287-
expect(() => {
288-
expect(fn).not[toHaveBeenCalledTimesAlias](0);
289-
}).toThrow();
288+
expect(() => {
289+
expect(fn).not[toHaveBeenCalledTimesAlias](0);
290+
}).toThrow();
290291

291-
expect(() => {
292-
expect(fn)[toHaveBeenCalledTimesAlias](1);
293-
}).toThrow();
292+
expect(() => {
293+
expect(fn)[toHaveBeenCalledTimesAlias](1);
294+
}).toThrow();
294295

295-
fn();
296+
fn();
296297

297-
expect(fn).not[toHaveBeenCalledTimesAlias](0);
298-
expect(fn)[toHaveBeenCalledTimesAlias](1);
298+
expect(fn).not[toHaveBeenCalledTimesAlias](0);
299+
expect(fn)[toHaveBeenCalledTimesAlias](1);
299300

300-
expect(() => {
301-
expect(fn)[toHaveBeenCalledTimesAlias](0);
302-
}).toThrow();
301+
expect(() => {
302+
expect(fn)[toHaveBeenCalledTimesAlias](0);
303+
}).toThrow();
303304

304-
expect(() => {
305-
expect(fn).not[toHaveBeenCalledTimesAlias](1);
306-
}).toThrow();
305+
expect(() => {
306+
expect(fn).not[toHaveBeenCalledTimesAlias](1);
307+
}).toThrow();
307308

308-
// Passing functions that aren't mocks should always fail
309-
expect(() => {
310-
expect(() => {})[toHaveBeenCalledTimesAlias](0);
311-
}).toThrow();
309+
// Passing functions that aren't mocks should always fail
310+
expect(() => {
311+
expect(() => {})[toHaveBeenCalledTimesAlias](0);
312+
}).toThrow();
312313

313-
expect(() => {
314-
expect(() => {}).not[toHaveBeenCalledTimesAlias](1);
315-
}).toThrow();
316-
}),
314+
expect(() => {
315+
expect(() => {}).not[toHaveBeenCalledTimesAlias](1);
316+
}).toThrow();
317+
}),
317318
);
318319

319-
['toBeCalledWith', 'toHaveBeenCalledWith'].map(toHaveBeenCalledWithAlias =>
320-
test(toHaveBeenCalledWithAlias, () => {
321-
const fn = jest.fn();
320+
(['toBeCalledWith', 'toHaveBeenCalledWith'] as const).map(
321+
toHaveBeenCalledWithAlias =>
322+
test(toHaveBeenCalledWithAlias, () => {
323+
const fn = jest.fn();
322324

323-
expect(fn).not[toHaveBeenCalledWithAlias]();
325+
expect(fn).not[toHaveBeenCalledWithAlias]();
324326

325-
expect(() => {
326-
expect(fn)[toHaveBeenCalledWithAlias]();
327-
}).toThrow();
327+
expect(() => {
328+
expect(fn)[toHaveBeenCalledWithAlias]();
329+
}).toThrow();
328330

329-
fn('happy');
330-
fn({a: 1}, 2);
331-
fn(['fantom'], {isAwesome: true});
331+
fn('happy');
332+
fn({a: 1}, 2);
333+
fn(['fantom'], {isAwesome: true});
332334

333-
expect(fn)[toHaveBeenCalledWithAlias]('happy');
334-
expect(fn)[toHaveBeenCalledWithAlias]({a: 1}, 2);
335-
expect(fn)[toHaveBeenCalledWithAlias](['fantom'], {isAwesome: true});
336-
expect(fn).not[toHaveBeenCalledWithAlias]();
337-
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1});
338-
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1}, 2, null);
339-
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1, b: 2}, 2);
335+
expect(fn)[toHaveBeenCalledWithAlias]('happy');
336+
expect(fn)[toHaveBeenCalledWithAlias]({a: 1}, 2);
337+
expect(fn)[toHaveBeenCalledWithAlias](['fantom'], {isAwesome: true});
338+
expect(fn).not[toHaveBeenCalledWithAlias]();
339+
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1});
340+
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1}, 2, null);
341+
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1, b: 2}, 2);
340342

341-
expect(() => {
342-
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1}, 2);
343-
}).toThrow();
343+
expect(() => {
344+
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1}, 2);
345+
}).toThrow();
344346

345-
expect(() => {
346-
expect(fn)[toHaveBeenCalledWithAlias](1);
347-
}).toThrow();
347+
expect(() => {
348+
expect(fn)[toHaveBeenCalledWithAlias](1);
349+
}).toThrow();
348350

349-
// Passing functions that aren't mocks should always fail
350-
expect(() => {
351-
expect(() => {})[toHaveBeenCalledWithAlias]();
352-
}).toThrow();
351+
// Passing functions that aren't mocks should always fail
352+
expect(() => {
353+
expect(() => {})[toHaveBeenCalledWithAlias]();
354+
}).toThrow();
353355

354-
expect(() => {
355-
expect(() => {}).not[toHaveBeenCalledWithAlias]();
356-
}).toThrow();
357-
}),
356+
expect(() => {
357+
expect(() => {}).not[toHaveBeenCalledWithAlias]();
358+
}).toThrow();
359+
}),
358360
);
359361

360-
['lastCalledWith', 'toHaveBeenLastCalledWith'].map(
362+
(['lastCalledWith', 'toHaveBeenLastCalledWith'] as const).map(
361363
toHaveBeenLastCalledWithAlias =>
362364
test(toHaveBeenLastCalledWithAlias, () => {
363365
const fn = jest.fn();
@@ -404,7 +406,7 @@ describe('expect', () => {
404406
}),
405407
);
406408

407-
['nthCalledWith', 'toHaveBeenNthCalledWith'].map(
409+
(['nthCalledWith', 'toHaveBeenNthCalledWith'] as const).map(
408410
toHaveBeenNthCalledWithAlias =>
409411
test(toHaveBeenNthCalledWithAlias, () => {
410412
const fn = jest.fn();

packages/react-native/Libraries/Components/TextInput/RCTTextInputViewConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {ConditionallyIgnoredEventHandlers} from '../../NativeComponent/ViewConfi
1414

1515
type PartialViewConfigWithoutName = Omit<PartialViewConfig, 'uiViewClassName'>;
1616

17-
const RCTTextInputViewConfig = {
17+
const RCTTextInputViewConfig: PartialViewConfigWithoutName = {
1818
bubblingEventTypes: {
1919
topBlur: {
2020
phasedRegistrationNames: {

packages/react-native/Libraries/Components/TextInput/TextInput.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,18 +784,19 @@ const enterKeyHintToReturnTypeMap = {
784784
previous: 'previous',
785785
search: 'search',
786786
send: 'send',
787-
};
787+
} as const;
788788

789789
const inputModeToKeyboardTypeMap = {
790790
none: 'default',
791791
text: 'default',
792792
decimal: 'decimal-pad',
793793
numeric: 'number-pad',
794794
tel: 'phone-pad',
795-
search: Platform.OS === 'ios' ? 'web-search' : 'default',
795+
search:
796+
Platform.OS === 'ios' ? ('web-search' as const) : ('default' as const),
796797
email: 'email-address',
797798
url: 'url',
798-
};
799+
} as const;
799800

800801
// Map HTML autocomplete values to Android autoComplete values
801802
const autoCompleteWebToAutoCompleteAndroidMap = {
@@ -829,7 +830,7 @@ const autoCompleteWebToAutoCompleteAndroidMap = {
829830
'tel-country-code': 'tel-country-code',
830831
'tel-national': 'tel-national',
831832
username: 'username',
832-
};
833+
} as const;
833834

834835
// Map HTML autocomplete values to iOS textContentType values
835836
const autoCompleteWebToTextContentTypeMap = {
@@ -869,7 +870,7 @@ const autoCompleteWebToTextContentTypeMap = {
869870
tel: 'telephoneNumber',
870871
url: 'URL',
871872
username: 'username',
872-
};
873+
} as const;
873874

874875
const TextInput: component(
875876
ref?: React.RefSetter<TextInputInstance>,
@@ -922,8 +923,7 @@ const TextInput: component(
922923
: Platform.OS === 'ios' &&
923924
autoComplete &&
924925
autoComplete in autoCompleteWebToTextContentTypeMap
925-
? // $FlowFixMe[invalid-computed-prop]
926-
// $FlowFixMe[prop-missing]
926+
? // $FlowFixMe[prop-missing]
927927
autoCompleteWebToTextContentTypeMap[autoComplete]
928928
: textContentType
929929
}
@@ -962,7 +962,7 @@ const verticalAlignToTextAlignVerticalMap = {
962962
top: 'top',
963963
bottom: 'bottom',
964964
middle: 'center',
965-
};
965+
} as const;
966966

967967
// $FlowFixMe[unclear-type] Unclear type. Using `any` type is not safe.
968968
export default TextInput as any as TextInputType;

packages/react-native/Libraries/NativeComponent/BaseViewConfig.android.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ const validAttributesForNonEventProps = {
174174
boxShadow:
175175
NativeReactNativeFeatureFlags != null &&
176176
ReactNativeFeatureFlags.enableNativeCSSParsing()
177-
? true
177+
? (true as const)
178178
: {
179179
process: require('../StyleSheet/processBoxShadow').default,
180180
},
181181
filter:
182182
NativeReactNativeFeatureFlags != null &&
183183
ReactNativeFeatureFlags.enableNativeCSSParsing()
184-
? true
184+
? (true as const)
185185
: {
186186
process: require('../StyleSheet/processFilter').default,
187187
},
@@ -369,7 +369,7 @@ const validAttributesForNonEventProps = {
369369
},
370370
focusable: true,
371371
backfaceVisibility: true,
372-
};
372+
} as const;
373373

374374
// Props for bubbling and direct events
375375
const validAttributesForEventProps = {
@@ -409,7 +409,7 @@ const validAttributesForEventProps = {
409409
onPointerOutCapture: true,
410410
onPointerOver: true,
411411
onPointerOverCapture: true,
412-
};
412+
} as const;
413413

414414
/**
415415
* On Android, Props are derived from a ViewManager and its ShadowNode.

packages/react-native/Libraries/NativeComponent/BaseViewConfig.ios.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ const validAttributesForNonEventProps = {
232232
filter:
233233
NativeReactNativeFeatureFlags != null &&
234234
ReactNativeFeatureFlags.enableNativeCSSParsing()
235-
? true
235+
? (true as const)
236236
: {
237237
process: require('../StyleSheet/processFilter').default,
238238
},
239239
boxShadow:
240240
NativeReactNativeFeatureFlags != null &&
241241
ReactNativeFeatureFlags.enableNativeCSSParsing()
242-
? true
242+
? (true as const)
243243
: {
244244
process: require('../StyleSheet/processBoxShadow').default,
245245
},
@@ -369,7 +369,7 @@ const validAttributesForNonEventProps = {
369369
direction: true,
370370

371371
style: ReactNativeStyleAttributes,
372-
};
372+
} as const;
373373

374374
// Props for bubbling and direct events
375375
const validAttributesForEventProps = ConditionallyIgnoredEventHandlers({

packages/react-native/Libraries/NativeComponent/ViewConfigIgnore.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export function DynamicallyInjectedByGestureHandler<T: {...}>(object: T): T {
3434
*
3535
* TODO(T110872225): Remove this logic, after achieving platform-consistency
3636
*/
37-
export function ConditionallyIgnoredEventHandlers<T: {[name: string]: true}>(
38-
value: T,
39-
): T | void {
37+
export function ConditionallyIgnoredEventHandlers<
38+
const T: {+[name: string]: true},
39+
>(value: T): T | void {
4040
if (Platform.OS === 'ios') {
4141
return value;
4242
}

packages/react-native/Libraries/Pressability/Pressability.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ const Transitions = Object.freeze({
236236
LEAVE_PRESS_RECT: 'NOT_RESPONDER',
237237
LONG_PRESS_DETECTED: 'NOT_RESPONDER',
238238
},
239-
});
239+
} as const);
240240

241241
const isActiveSignal = (signal: TouchState) =>
242242
signal === 'RESPONDER_ACTIVE_PRESS_IN' ||

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5827,7 +5827,7 @@ exports[`public API should not change unintentionally Libraries/NativeComponent/
58275827
object: T
58285828
): T;
58295829
declare export function ConditionallyIgnoredEventHandlers<
5830-
T: { [name: string]: true },
5830+
const T: { +[name: string]: true },
58315831
>(
58325832
value: T
58335833
): T | void;

0 commit comments

Comments
 (0)