From a31d7aa2d3acd1317dff4148cffe3e886d5b5404 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 5 Nov 2020 17:43:23 -0800 Subject: [PATCH] Standardize on one FunctionTypeAnnotation shape Summary: Commands are `FunctionTypeAnnotation`, but they don't have a return type. I this diff, I introduced a `FunctionTypeAnnotation<+P, +R>` utility type, and made the `CommandTypeAnnotation` be an instantiation of it, with the return type fixed to `VoidTypeAnnotation`. Now, the shape of a FunctionTypeAnnotation is unified across the NativeModule and Component schemas. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D24719965 fbshipit-source-id: 0089c3b23f05b0c534ba28dbe336c7f2db5866b3 --- .../react-native-codegen/src/CodegenSchema.js | 41 +++++++++---------- .../components/GenerateComponentHObjCpp.js | 18 ++++---- .../components/__test_fixtures__/fixtures.js | 20 +++++++++ .../component-parser-test.js.snap | 25 ++++++++--- .../src/parsers/flow/components/commands.js | 3 ++ 5 files changed, 71 insertions(+), 36 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index b5d931a119..8efda90ff7 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -48,11 +48,21 @@ export type StringEnumTypeAnnotation = $ReadOnly<{| |}>, |}>; +export type VoidTypeAnnotation = $ReadOnly<{| + type: 'VoidTypeAnnotation', +|}>; + type ObjectTypeAnnotation<+T> = $ReadOnly<{| type: 'ObjectTypeAnnotation', properties: $ReadOnlyArray>, |}>; +type FunctionTypeAnnotation<+P, +R> = $ReadOnly<{| + type: 'FunctionTypeAnnotation', + params: $ReadOnlyArray>, + returnTypeAnnotation: R, +|}>; + export type NamedShape<+T> = $ReadOnly<{ name: string, optional: boolean, @@ -188,18 +198,12 @@ export type PropTypeAnnotation = |}>, |}>; -export type CommandTypeAnnotation = $ReadOnly<{| - type: 'FunctionTypeAnnotation', - params: $ReadOnlyArray, -|}>; +export type CommandTypeAnnotation = FunctionTypeAnnotation< + CommandParamTypeAnnotation, + VoidTypeAnnotation, +>; -// TODO: Unify this function type annotation with NativeModule schema -export type CommandFunctionTypeParamAnnotation = $ReadOnly<{| - name: string, - typeAnnotation: CommandParamTypeAnnotation, -|}>; - -type CommandParamTypeAnnotation = +export type CommandParamTypeAnnotation = | ReservedTypeAnnotation | BooleanTypeAnnotation | Int32TypeAnnotation @@ -249,11 +253,10 @@ export type NativeModuleAliasMap = $ReadOnly<{| [aliasName: string]: NativeModuleObjectTypeAnnotation, |}>; -export type NativeModuleFunctionTypeAnnotation = $ReadOnly<{| - type: 'FunctionTypeAnnotation', - params: $ReadOnlyArray>>, - returnTypeAnnotation: Nullable, -|}>; +export type NativeModuleFunctionTypeAnnotation = FunctionTypeAnnotation< + Nullable, + Nullable, +>; export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation< Nullable, @@ -307,10 +310,6 @@ export type NativeModulePromiseTypeAnnotation = $ReadOnly<{| type: 'PromiseTypeAnnotation', |}>; -export type NativeModuleVoidTypeAnnotation = $ReadOnly<{| - type: 'VoidTypeAnnotation', -|}>; - export type NativeModuleBaseTypeAnnotation = | NativeModuleStringTypeAnnotation | NativeModuleNumberTypeAnnotation @@ -340,4 +339,4 @@ export type NativeModuleTypeAnnotation = type NativeModuleParamOnlyTypeAnnotation = NativeModuleFunctionTypeAnnotation; type NativeModuleReturnOnlyTypeAnnotation = | NativeModulePromiseTypeAnnotation - | NativeModuleVoidTypeAnnotation; + | VoidTypeAnnotation; diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js index 057cf1a5cb..b8950352b5 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js @@ -15,7 +15,7 @@ import type { CommandTypeAnnotation, ComponentShape, SchemaType, - CommandFunctionTypeParamAnnotation, + CommandParamTypeAnnotation, } from '../../CodegenSchema'; type FilesOutput = Map; @@ -104,7 +104,9 @@ NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END `.trim(); -function getObjCParamType(param: CommandFunctionTypeParamAnnotation): string { +type Param = NamedShape; + +function getObjCParamType(param: Param): string { const {typeAnnotation} = param; switch (typeAnnotation.type) { @@ -132,9 +134,7 @@ function getObjCParamType(param: CommandFunctionTypeParamAnnotation): string { } } -function getObjCExpectedKindParamType( - param: CommandFunctionTypeParamAnnotation, -): string { +function getObjCExpectedKindParamType(param: Param): string { const {typeAnnotation} = param; switch (typeAnnotation.type) { @@ -162,9 +162,7 @@ function getObjCExpectedKindParamType( } } -function getReadableExpectedKindParamType( - param: CommandFunctionTypeParamAnnotation, -): string { +function getReadableExpectedKindParamType(param: Param): string { const {typeAnnotation} = param; switch (typeAnnotation.type) { @@ -193,7 +191,7 @@ function getReadableExpectedKindParamType( } function getObjCRightHandAssignmentParamType( - param: CommandFunctionTypeParamAnnotation, + param: Param, index: number, ): string { const {typeAnnotation} = param; @@ -253,7 +251,7 @@ function generateProtocol( } function generateConvertAndValidateParam( - param: CommandFunctionTypeParamAnnotation, + param: Param, index: number, componentName: string, ): string { diff --git a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js index 5e82ace633..2737ac892d 100644 --- a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js @@ -1456,6 +1456,9 @@ const COMMANDS: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', params: [], + returnTypeAnnotation: { + type: 'VoidTypeAnnotation', + }, }, }, { @@ -1466,35 +1469,43 @@ const COMMANDS: SchemaType = { params: [ { name: 'x', + optional: false, typeAnnotation: { type: 'Int32TypeAnnotation', }, }, { name: 'y', + optional: false, typeAnnotation: { type: 'FloatTypeAnnotation', }, }, { name: 'z', + optional: false, typeAnnotation: { type: 'DoubleTypeAnnotation', }, }, { name: 'message', + optional: false, typeAnnotation: { type: 'StringTypeAnnotation', }, }, { name: 'animated', + optional: false, typeAnnotation: { type: 'BooleanTypeAnnotation', }, }, ], + returnTypeAnnotation: { + type: 'VoidTypeAnnotation', + }, }, }, ], @@ -1536,12 +1547,16 @@ const COMMANDS_AND_PROPS: SchemaType = { params: [ { name: 'rootTag', + optional: false, typeAnnotation: { type: 'ReservedTypeAnnotation', name: 'RootTag', }, }, ], + returnTypeAnnotation: { + type: 'VoidTypeAnnotation', + }, }, }, { @@ -1552,17 +1567,22 @@ const COMMANDS_AND_PROPS: SchemaType = { params: [ { name: 'x', + optional: false, typeAnnotation: { type: 'Int32TypeAnnotation', }, }, { name: 'y', + optional: false, typeAnnotation: { type: 'Int32TypeAnnotation', }, }, ], + returnTypeAnnotation: { + type: 'VoidTypeAnnotation', + }, }, }, ], diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap index 61cef44115..161a60b909 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap @@ -2889,7 +2889,10 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_AND_EVENTS_TYPES_E 'type': 'BooleanTypeAnnotation' } } - ] + ], + 'returnTypeAnnotation': { + 'type': 'VoidTypeAnnotation' + } } } ] @@ -2929,7 +2932,10 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_T 'name': 'RootTag' } } - ] + ], + 'returnTypeAnnotation': { + 'type': 'VoidTypeAnnotation' + } } }, { @@ -2950,7 +2956,10 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_T 'type': 'Int32TypeAnnotation' } } - ] + ], + 'returnTypeAnnotation': { + 'type': 'VoidTypeAnnotation' + } } }, { @@ -2983,7 +2992,10 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_T 'type': 'BooleanTypeAnnotation' } } - ] + ], + 'returnTypeAnnotation': { + 'type': 'VoidTypeAnnotation' + } } } ] @@ -3028,7 +3040,10 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_WITH_EXTERNAL_TYPE 'type': 'BooleanTypeAnnotation' } } - ] + ], + 'returnTypeAnnotation': { + 'type': 'VoidTypeAnnotation' + } } } ] diff --git a/packages/react-native-codegen/src/parsers/flow/components/commands.js b/packages/react-native-codegen/src/parsers/flow/components/commands.js index 1734cf75cf..48103dc595 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/commands.js +++ b/packages/react-native-codegen/src/parsers/flow/components/commands.js @@ -95,6 +95,9 @@ function buildCommandSchema(property, types: TypeDeclarationMap) { typeAnnotation: { type: 'FunctionTypeAnnotation', params, + returnTypeAnnotation: { + type: 'VoidTypeAnnotation', + }, }, }; }