Summary: This part of writing codegen was straightforward. I've added check for 'FunctionTypeAnnotation' and then reuse exisiting methods' parser for generating schema for callback.

Reviewed By: TheSavior

Differential Revision: D16131213

fbshipit-source-id: 2ec0e241f2174dee3a93857563db0626013d004e
This commit is contained in:
Michał Osadnik 2019-07-08 09:28:02 -07:00 коммит произвёл Facebook Github Bot
Родитель a7664dbaf1
Коммит a269c1f6c2
4 изменённых файлов: 117 добавлений и 5 удалений

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

@ -151,6 +151,11 @@ export type FunctionTypeAnnotationParamTypeAnnotation =
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<ObjectParamTypeAnnotation>,
|}>
| $ReadOnly<{|
type: 'FunctionTypeAnnotation',
params: $ReadOnlyArray<FunctionTypeAnnotationParam>,
returnTypeAnnotation: FunctionTypeAnnotationReturn,
|}>;
export type FunctionTypeAnnotationReturnArrayElementType = FunctionTypeAnnotationParamTypeAnnotation;
@ -170,7 +175,7 @@ export type FunctionTypeAnnotationReturn =
|}>
| $ReadOnly<{|
type: 'PromiseTypeAnnotation',
resolvingType: FunctionTypeAnnotationReturn,
resolvedType: FunctionTypeAnnotationReturn,
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',

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

@ -269,6 +269,34 @@ export interface Spec extends TurboModule {
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;
const NATIVE_MODULE_WITH_CALLBACK = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from '../RCTExport';
import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
// Exported methods.
+getValueWithCallback: (
callback: (value: string, arr: Array<Array<string>>) => void,
) => void;
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;
const EVENT_DEFINITION = `
@ -820,6 +848,7 @@ module.exports = {
NATIVE_MODULE_WITH_COMPLEX_ARRAY,
NATIVE_MODULE_WITH_ARRAY_WITH_ALIAS,
NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
NATIVE_MODULE_WITH_CALLBACK,
EMPTY_NATIVE_MODULE,
ALL_PROP_TYPES_NO_EVENTS,
ARRAY_PROP_TYPES_NO_EVENTS,

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

@ -3482,6 +3482,65 @@ Object {
}
`;
exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_CALLBACK 1`] = `
Object {
"modules": Object {
"SampleTurboModule": Object {
"nativeModules": Object {
"SampleTurboModule": Object {
"properties": Array [
Object {
"name": "getValueWithCallback",
"typeAnnotation": Object {
"optional": false,
"params": Array [
Object {
"name": "callback",
"nullable": false,
"typeAnnotation": Object {
"params": Array [
Object {
"name": "value",
"nullable": false,
"typeAnnotation": Object {
"type": "StringTypeAnnotation",
},
},
Object {
"name": "arr",
"nullable": false,
"typeAnnotation": Object {
"elementType": Object {
"elementType": Object {
"type": "StringTypeAnnotation",
},
"type": "ArrayTypeAnnotation",
},
"type": "ArrayTypeAnnotation",
},
},
],
"returnTypeAnnotation": Object {
"type": "VoidTypeAnnotation",
},
"type": "FunctionTypeAnnotation",
},
},
],
"returnTypeAnnotation": Object {
"type": "VoidTypeAnnotation",
},
"type": "FunctionTypeAnnotation",
},
},
],
},
},
},
},
}
`;
exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_COMPLEX_ARRAY 1`] = `
Object {
"modules": Object {
@ -3734,7 +3793,7 @@ Object {
"optional": false,
"params": Array [],
"returnTypeAnnotation": Object {
"resolvingType": Object {
"resolvedType": Object {
"type": "StringTypeAnnotation",
},
"type": "PromiseTypeAnnotation",
@ -3748,7 +3807,7 @@ Object {
"optional": false,
"params": Array [],
"returnTypeAnnotation": Object {
"resolvingType": Object {
"resolvedType": Object {
"type": "StringTypeAnnotation",
},
"type": "PromiseTypeAnnotation",

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

@ -174,7 +174,7 @@ function getTypeAnnotationForParam(
);
}
}
if (param.typeAnnotation.type === 'ObjectTypeAnnotation') {
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
return {
nullable,
name: paramName,
@ -189,6 +189,25 @@ function getTypeAnnotationForParam(
},
};
}
if (typeAnnotation.type === 'FunctionTypeAnnotation') {
const params = typeAnnotation.params.map(callbackParam =>
getTypeAnnotationForParam(name, callbackParam, types),
);
const returnTypeAnnotation = getReturnTypeAnnotation(
name,
typeAnnotation.returnType,
types,
);
return {
name: paramName,
nullable,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
params,
returnTypeAnnotation,
},
};
}
const type = typeAnnotation.type;
if (
@ -230,7 +249,7 @@ function getReturnTypeAnnotation(
) {
return {
type: 'PromiseTypeAnnotation',
resolvingType: getReturnTypeAnnotation(
resolvedType: getReturnTypeAnnotation(
methodName,
typeAnnotation.typeParameters.params[0],
types,