diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 1c6b90f9e84..8fc315ae745 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -251,22 +251,14 @@ export type NativeModuleAliasMap = $ReadOnly<{| export type NativeModuleFunctionTypeAnnotation = $ReadOnly<{| type: 'FunctionTypeAnnotation', - params: $ReadOnlyArray, + params: $ReadOnlyArray>>, returnTypeAnnotation: Nullable, |}>; -export type NativeModuleMethodParamSchema = NamedShape< - Nullable, ->; - export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation< Nullable, >; -export type NativeModuleObjectTypeAnnotationPropertySchema = NamedShape< - Nullable, ->; - export type NativeModuleArrayTypeAnnotation< +T: Nullable, > = $ReadOnly<{| diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js index 5050f213707..dd30806c0cf 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js @@ -13,7 +13,8 @@ import type { SchemaType, NativeModulePropertySchema, - NativeModuleMethodParamSchema, + Nullable, + NamedShape, NativeModuleFunctionTypeAnnotation, NativeModuleParamTypeAnnotation, } from '../../CodegenSchema'; @@ -99,8 +100,10 @@ ${modules} `; }; +type Param = NamedShape>; + function serializeArg( - arg: NativeModuleMethodParamSchema, + arg: Param, index: number, resolveAlias: AliasResolver, ): string { diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js index a18315346d1..fdaf07657b2 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js @@ -12,9 +12,9 @@ import type { Nullable, + NamedShape, SchemaType, NativeModulePropertySchema, - NativeModuleMethodParamSchema, NativeModuleReturnTypeAnnotation, NativeModuleFunctionTypeAnnotation, NativeModuleParamTypeAnnotation, @@ -91,8 +91,10 @@ function MethodTemplate( )})${methodClosing}`; } +type Param = NamedShape>; + function translateFunctionParamToJavaType( - param: NativeModuleMethodParamSchema, + param: Param, createErrorMessage: (typeName: string) => string, resolveAlias: AliasResolver, imports: Set, diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js index 8b4a9317766..d02b0d6731b 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js @@ -12,9 +12,9 @@ import type { Nullable, + NamedShape, SchemaType, NativeModulePropertySchema, - NativeModuleMethodParamSchema, NativeModuleReturnTypeAnnotation, NativeModuleParamTypeAnnotation, NativeModuleFunctionTypeAnnotation, @@ -177,8 +177,10 @@ function translateReturnTypeToKind( } } +type Param = NamedShape>; + function translateParamTypeToJniType( - param: NativeModuleMethodParamSchema, + param: Param, resolveAlias: AliasResolver, ): string { const {optional, typeAnnotation: nullableTypeAnnotation} = param; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js index bf12da4ecfa..54ab9a2b72b 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js @@ -11,10 +11,11 @@ 'use strict'; import type { - NativeModuleMethodParamSchema, - NativeModuleReturnTypeAnnotation, NativeModulePropertySchema, Nullable, + NamedShape, + NativeModuleParamTypeAnnotation, + NativeModuleReturnTypeAnnotation, } from '../../../CodegenSchema'; import type {AliasResolver} from '../Utils'; @@ -164,10 +165,9 @@ function serializeMethod( ]; } -function getParamStructName( - methodName: string, - param: NativeModuleMethodParamSchema, -): string { +type Param = NamedShape>; + +function getParamStructName(methodName: string, param: Param): string { const [typeAnnotation] = unwrapNullable(param.typeAnnotation); if (typeAnnotation.type === 'TypeAliasTypeAnnotation') { return typeAnnotation.name; @@ -179,7 +179,7 @@ function getParamStructName( function getParamObjCType( hasteModuleName: string, methodName: string, - param: NativeModuleMethodParamSchema, + param: Param, structName: string, structCollector: StructCollector, resolveAlias: AliasResolver, diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 76727185f01..6e44e657208 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -11,13 +11,13 @@ 'use strict'; import type { + NamedShape, NativeModuleAliasMap, NativeModuleArrayTypeAnnotation, NativeModuleBaseTypeAnnotation, NativeModuleFunctionTypeAnnotation, - NativeModuleMethodParamSchema, - NativeModuleObjectTypeAnnotationPropertySchema, NativeModulePropertySchema, + NativeModuleParamTypeAnnotation, NativeModuleSchema, Nullable, } from '../../../CodegenSchema.js'; @@ -207,68 +207,70 @@ function translateTypeAnnotation( const objectTypeAnnotation = { type: 'ObjectTypeAnnotation', properties: (typeAnnotation.properties: Array<$FlowFixMe>) - .map(property => { - return guard(() => { - if (property.type !== 'ObjectTypeProperty') { - throw new UnsupportedObjectPropertyTypeAnnotationParserError( - hasteModuleName, - property, - property.type, - ); - } + .map>>( + property => { + return guard(() => { + if (property.type !== 'ObjectTypeProperty') { + throw new UnsupportedObjectPropertyTypeAnnotationParserError( + hasteModuleName, + property, + property.type, + ); + } - const {optional, key} = property; + const {optional, key} = property; - const [ - propertyTypeAnnotation, - isPropertyNullable, - ] = unwrapNullable( - translateTypeAnnotation( - hasteModuleName, - property.value, - types, - aliasMap, - guard, - ), - ); - - if (propertyTypeAnnotation.type === 'FunctionTypeAnnotation') { - throw new UnsupportedObjectPropertyValueTypeAnnotationParserError( - hasteModuleName, - property.value, - property.key, - propertyTypeAnnotation.type, - ); - } - - if (propertyTypeAnnotation.type === 'VoidTypeAnnotation') { - throw new UnsupportedObjectPropertyValueTypeAnnotationParserError( - hasteModuleName, - property.value, - property.key, - 'void', - ); - } - - if (propertyTypeAnnotation.type === 'PromiseTypeAnnotation') { - throw new UnsupportedObjectPropertyValueTypeAnnotationParserError( - hasteModuleName, - property.value, - property.key, - 'Promise', - ); - } - - return { - name: key.name, - optional, - typeAnnotation: wrapNullable( - isPropertyNullable, + const [ propertyTypeAnnotation, - ), - }; - }); - }) + isPropertyNullable, + ] = unwrapNullable( + translateTypeAnnotation( + hasteModuleName, + property.value, + types, + aliasMap, + guard, + ), + ); + + if (propertyTypeAnnotation.type === 'FunctionTypeAnnotation') { + throw new UnsupportedObjectPropertyValueTypeAnnotationParserError( + hasteModuleName, + property.value, + property.key, + propertyTypeAnnotation.type, + ); + } + + if (propertyTypeAnnotation.type === 'VoidTypeAnnotation') { + throw new UnsupportedObjectPropertyValueTypeAnnotationParserError( + hasteModuleName, + property.value, + property.key, + 'void', + ); + } + + if (propertyTypeAnnotation.type === 'PromiseTypeAnnotation') { + throw new UnsupportedObjectPropertyValueTypeAnnotationParserError( + hasteModuleName, + property.value, + property.key, + 'Promise', + ); + } + + return { + name: key.name, + optional, + typeAnnotation: wrapNullable( + isPropertyNullable, + propertyTypeAnnotation, + ), + }; + }); + }, + ) .filter(Boolean), }; @@ -391,7 +393,8 @@ function translateFunctionTypeAnnotation( aliasMap: {...NativeModuleAliasMap}, guard: ParserErrorCapturer, ): NativeModuleFunctionTypeAnnotation { - const params: Array = []; + type Param = NamedShape>; + const params: Array = []; for (const flowParam of (flowFunctionTypeAnnotation.params: $ReadOnlyArray<$FlowFixMe>)) { const parsedParam = guard(() => {