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
This commit is contained in:
Ramanpreet Nara 2020-11-05 17:43:23 -08:00 коммит произвёл Facebook GitHub Bot
Родитель 0d748cf8bb
Коммит a31d7aa2d3
5 изменённых файлов: 71 добавлений и 36 удалений

Просмотреть файл

@ -48,11 +48,21 @@ export type StringEnumTypeAnnotation = $ReadOnly<{|
|}>,
|}>;
export type VoidTypeAnnotation = $ReadOnly<{|
type: 'VoidTypeAnnotation',
|}>;
type ObjectTypeAnnotation<+T> = $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<NamedShape<T>>,
|}>;
type FunctionTypeAnnotation<+P, +R> = $ReadOnly<{|
type: 'FunctionTypeAnnotation',
params: $ReadOnlyArray<NamedShape<P>>,
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<CommandFunctionTypeParamAnnotation>,
|}>;
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<NamedShape<Nullable<NativeModuleParamTypeAnnotation>>>,
returnTypeAnnotation: Nullable<NativeModuleReturnTypeAnnotation>,
|}>;
export type NativeModuleFunctionTypeAnnotation = FunctionTypeAnnotation<
Nullable<NativeModuleParamTypeAnnotation>,
Nullable<NativeModuleReturnTypeAnnotation>,
>;
export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation<
Nullable<NativeModuleBaseTypeAnnotation>,
@ -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;

Просмотреть файл

@ -15,7 +15,7 @@ import type {
CommandTypeAnnotation,
ComponentShape,
SchemaType,
CommandFunctionTypeParamAnnotation,
CommandParamTypeAnnotation,
} from '../../CodegenSchema';
type FilesOutput = Map<string, string>;
@ -104,7 +104,9 @@ NS_ASSUME_NONNULL_BEGIN
NS_ASSUME_NONNULL_END
`.trim();
function getObjCParamType(param: CommandFunctionTypeParamAnnotation): string {
type Param = NamedShape<CommandParamTypeAnnotation>;
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 {

Просмотреть файл

@ -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',
},
},
},
],

Просмотреть файл

@ -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'
}
}
}
]

Просмотреть файл

@ -95,6 +95,9 @@ function buildCommandSchema(property, types: TypeDeclarationMap) {
typeAnnotation: {
type: 'FunctionTypeAnnotation',
params,
returnTypeAnnotation: {
type: 'VoidTypeAnnotation',
},
},
};
}