Skip to content

Commit

Permalink
Convert unannotated variable declaration with cast init to annotated …
Browse files Browse the repository at this point in the history
…variable declaration in xplat/js (#42880)

Summary:
Pull Request resolved: #42880

Convert `const x = (a: T)` into `const x: T = a`. These are equivalent in Flow, and helps reduce the amount of colon-casts.

```
js1 flow-runner codemod flow/castToAnnotatedVariable --target colon xplat/js
```

Changelog: [Internal]

drop-conflicts

Reviewed By: SamChou19815

Differential Revision: D53392999

fbshipit-source-id: 3b4602618d6990e8642b423edaeb4e89b01a199a
  • Loading branch information
gkz authored and facebook-github-bot committed Feb 6, 2024
1 parent 1ad3bbc commit 608029a
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Animated/Animated.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import Platform from '../Utilities/Platform';
import AnimatedImplementation from './AnimatedImplementation';
import AnimatedMock from './AnimatedMock';

const Animated = ((Platform.isDisableAnimations
const Animated: typeof AnimatedImplementation = Platform.isDisableAnimations
? AnimatedMock
: AnimatedImplementation): typeof AnimatedImplementation);
: AnimatedImplementation;

export default {
get FlatList(): AnimatedFlatList {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const addPoolingTo = function <T>(
} {
// Casting as any so that flow ignores the actual implementation and trusts
// it to match the type we declared
const NewKlass = (CopyConstructor: any);
const NewKlass: any = CopyConstructor;
NewKlass.instancePool = [];
NewKlass.getPooled = pooler || DEFAULT_POOLER;
if (!NewKlass.poolSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ class TouchableHighlight extends React.Component<Props, State> {
}
}

const Touchable = (React.forwardRef((props, hostRef) => (
<TouchableHighlight {...props} hostRef={hostRef} />
)): React.AbstractComponent<
const Touchable: React.AbstractComponent<
$ReadOnly<$Diff<Props, {|hostRef: React.Ref<typeof View>|}>>,
React.ElementRef<typeof View>,
>);
> = React.forwardRef((props, hostRef) => (
<TouchableHighlight {...props} hostRef={hostRef} />
));

Touchable.displayName = 'TouchableHighlight';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,12 @@ class TouchableOpacity extends React.Component<Props, State> {
}
}

const Touchable = (React.forwardRef((props, ref) => (
const Touchable: React.AbstractComponent<
Props,
React.ElementRef<typeof Animated.View>,
> = React.forwardRef((props, ref) => (
<TouchableOpacity {...props} hostRef={ref} />
)): React.AbstractComponent<Props, React.ElementRef<typeof Animated.View>>);
));

Touchable.displayName = 'TouchableOpacity';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export default class MutationRecord {
_removedNodes: NodeList<ReadOnlyNode>;

constructor(nativeRecord: NativeMutationRecord) {
// $FlowExpectedError[incompatible-cast] the codegen doesn't support the actual type.
const target = (nativeRecord.target: ReactNativeElement);
// $FlowExpectedError[incompatible-type] the codegen doesn't support the actual type.
const target: ReactNativeElement = nativeRecord.target;
this._target = target;
// $FlowExpectedError[incompatible-cast] the codegen doesn't support the actual type.
const addedNodes = (nativeRecord.addedNodes: $ReadOnlyArray<ReadOnlyNode>);
// $FlowExpectedError[incompatible-type] the codegen doesn't support the actual type.
const addedNodes: $ReadOnlyArray<ReadOnlyNode> = nativeRecord.addedNodes;
this._addedNodes = createNodeList(addedNodes);
const removedNodes =
// $FlowExpectedError[incompatible-cast] the codegen doesn't support the actual type.
(nativeRecord.removedNodes: $ReadOnlyArray<ReadOnlyNode>);
const removedNodes: $ReadOnlyArray<ReadOnlyNode> =
// $FlowFixMe[incompatible-type]
nativeRecord.removedNodes;
this._removedNodes = createNodeList(removedNodes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
return;
}

let nativeViewTag = (instanceHandle.stateNode.canonical
.nativeTag: number);
let nativeViewTag: number =
instanceHandle.stateNode.canonical.nativeTag;

FabricUIManager.measure(
node,
Expand Down
5 changes: 2 additions & 3 deletions packages/react-native/Libraries/Text/TextAncestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ const React = require('react');
/**
* Whether the current element is the descendant of a <Text> element.
*/
const TextAncestorContext = (React.createContext(
false,
): React$Context<$FlowFixMe>);
const TextAncestorContext: React$Context<$FlowFixMe> =
React.createContext(false);
if (__DEV__) {
TextAncestorContext.displayName = 'TextAncestorContext';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function binaryToBase64(data: ArrayBuffer | $ArrayBufferView): string {
throw new Error('data must be ArrayBuffer or typed array');
}
// Already checked that `data` is `DataView` in `ArrayBuffer.isView(data)`
const {buffer, byteOffset, byteLength} = ((data: $FlowFixMe): DataView);
const {buffer, byteOffset, byteLength}: DataView = (data: $FlowFixMe);
return base64.fromByteArray(new Uint8Array(buffer, byteOffset, byteLength));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rn-tester/js/examples/Animated/EasingExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function EasingExample(props: Props): React.Node {
<SectionList
sections={easingSections}
renderItem={info => {
const item = (info.item: EasingListItem);
const item: EasingListItem = info.item;

return (
<EasingItem
Expand Down
6 changes: 3 additions & 3 deletions packages/rn-tester/js/examples/Modal/ModalExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import ModalOnShow from './ModalOnShow';
import ModalPresentation from './ModalPresentation';

export const displayName = (undefined: ?string);
export const displayName: ?string = undefined;
export const framework = 'React';
export const title = 'Modal';
export const category = 'UI';
export const documentationURL = 'https://reactnative.dev/docs/modal';
export const description = 'Component for presenting modal views.';
export const examples = ([
export const examples: Array<RNTesterModuleExample> = [
ModalPresentation,
ModalOnShow,
]: Array<RNTesterModuleExample>);
];
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ function VisualCompletionExampleScreen(props: {
style={styles.root}
ref={node => {
if (node != null) {
// $FlowExpectedError[incompatible-cast]
const element = (node: ReactNativeElement);
// $FlowExpectedError[incompatible-type]
const element: ReactNativeElement = node;
props.vcTracker.addMutationRoot(element);
}
}}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ exports.documentationURL = 'https://reactnative.dev/docs/scrollview';
exports.category = 'Basic';
exports.description =
'Component that enables scrolling through child components';
const examples = ([
const examples: Array<RNTesterModuleExample> = [
{
name: 'scrollTo',
title: '<ScrollView>\n',
Expand Down Expand Up @@ -425,7 +425,7 @@ const examples = ([
return <AppendingList />;
},
},
]: Array<RNTesterModuleExample>);
];

if (Platform.OS === 'ios') {
examples.push({
Expand Down

0 comments on commit 608029a

Please sign in to comment.