From d96744e27711c4fa4dfad1b5a796283a232e60af Mon Sep 17 00:00:00 2001 From: Pieter Vanderwerff Date: Wed, 22 Jun 2022 18:46:51 -0700 Subject: [PATCH] Add LTI annotations to function params in xplat/js [2/2] Summary: Add annotations to function parameters required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predicatable. Reviewed By: evanyeung Differential Revision: D37360113 fbshipit-source-id: 870bcfe680542b3861fefbaf372db0ae8b32cbf3 --- Libraries/Image/AssetSourceResolver.js | 4 +-- .../components/GenerateViewConfigJs.js | 32 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Libraries/Image/AssetSourceResolver.js b/Libraries/Image/AssetSourceResolver.js index 6953ae1d0404c1..61b7c10794d317 100644 --- a/Libraries/Image/AssetSourceResolver.js +++ b/Libraries/Image/AssetSourceResolver.js @@ -35,7 +35,7 @@ const { /** * Returns a path like 'assets/AwesomeModule/icon@2x.png' */ -function getScaledAssetPath(asset): string { +function getScaledAssetPath(asset: PackagerAsset): string { const scale = pickScale(asset.scales, PixelRatio.get()); const scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; const assetDir = getBasePath(asset); @@ -45,7 +45,7 @@ function getScaledAssetPath(asset): string { /** * Returns a path like 'drawable-mdpi/icon.png' */ -function getAssetPathInDrawableFolder(asset): string { +function getAssetPathInDrawableFolder(asset: PackagerAsset): string { const scale = pickScale(asset.scales, PixelRatio.get()); const drawbleFolder = getAndroidResourceFolderName(asset, scale); const fileName = getAndroidResourceIdentifier(asset); diff --git a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js index 7fae7e23673dea..49e1a9208d74e5 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js +++ b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js @@ -9,6 +9,11 @@ */ 'use strict'; +import type { + PropTypeAnnotation, + EventTypeShape, + ComponentShape, +} from '../../CodegenSchema'; const j = require('jscodeshift'); @@ -46,7 +51,7 @@ ${componentConfig} // this multiple times. const UIMANAGER_IMPORT = 'const {UIManager} = require("react-native")'; -function getReactDiffProcessValue(typeAnnotation) { +function getReactDiffProcessValue(typeAnnotation: PropTypeAnnotation) { switch (typeAnnotation.type) { case 'BooleanTypeAnnotation': case 'StringTypeAnnotation': @@ -151,7 +156,7 @@ if (UIManager.hasViewManagerConfig('${componentName}')) { `.trim(); // Replicates the behavior of RCTNormalizeInputEventName in RCTEventDispatcher.m -function normalizeInputEventName(name) { +function normalizeInputEventName(name: string) { if (name.startsWith('on')) { return name.replace(/^on/, 'top'); } else if (!name.startsWith('top')) { @@ -162,7 +167,10 @@ function normalizeInputEventName(name) { } // Replicates the behavior of viewConfig in RCTComponentData.m -function getValidAttributesForEvents(events, imports) { +function getValidAttributesForEvents( + events: $ReadOnlyArray, + imports: Set, +) { imports.add( "const {ConditionallyIgnoredEventHandlers} = require('react-native/Libraries/NativeComponent/ViewConfigIgnore');", ); @@ -178,7 +186,10 @@ function getValidAttributesForEvents(events, imports) { ]); } -function generateBubblingEventInfo(event, nameOveride) { +function generateBubblingEventInfo( + event: EventTypeShape, + nameOveride: void | string, +) { return j.property( 'init', j.identifier(nameOveride || normalizeInputEventName(event.name)), @@ -199,7 +210,10 @@ function generateBubblingEventInfo(event, nameOveride) { ); } -function generateDirectEventInfo(event, nameOveride) { +function generateDirectEventInfo( + event: EventTypeShape, + nameOveride: void | string, +) { return j.property( 'init', j.identifier(nameOveride || normalizeInputEventName(event.name)), @@ -216,8 +230,8 @@ function generateDirectEventInfo(event, nameOveride) { function buildViewConfig( schema: SchemaType, componentName: string, - component, - imports, + component: ComponentShape, + imports: Set, ) { const componentProps = component.props; const componentEvents = component.events; @@ -326,8 +340,8 @@ function buildViewConfig( function buildCommands( schema: SchemaType, componentName: string, - component, - imports, + component: ComponentShape, + imports: Set, ) { const commands = component.commands;