From c6b8c75b6be3efa0e3bfeff84daabaad8de01cd9 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 5 Nov 2020 17:43:23 -0800 Subject: [PATCH] Remove miscellaneous exports of NativeModule Flow types in Codegen Schema Summary: CodegenSchema exports `NativeModuleMethodParamSchema` and `NativeModuleObjectTypeAnnotationPropertySchema`, which are partials of NativeModule type annotations. This creates unnecessary coupling between the type annotations of CodegenSchema and the files that depend on it. **Actual Problem:** Suppose that we want to rename one of these partials. Then, all imports in all files would have to be updated, even when the actual shape of the composed type annotation wasn't changed. This diff removes these partials, which reduces the surface area of the exports of CodegenSchema.js Changelog: [Internal] Reviewed By: yungsters Differential Revision: D24719396 fbshipit-source-id: c822aaa252f156c524f4ef4917ebb61b1a39ff9e --- .../react-native-codegen/src/CodegenSchema.js | 10 +- .../generators/modules/GenerateModuleCpp.js | 7 +- .../modules/GenerateModuleJavaSpec.js | 6 +- .../modules/GenerateModuleJniCpp.js | 6 +- .../GenerateModuleObjCpp/serializeMethod.js | 14 +- .../src/parsers/flow/modules/index.js | 127 +++++++++--------- 6 files changed, 86 insertions(+), 84 deletions(-) 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(() => {