Skip to content

Commit

Permalink
Add support for callback
Browse files Browse the repository at this point in the history
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
  • Loading branch information
osdnk authored and facebook-github-bot committed Jul 8, 2019
1 parent a7664db commit a269c1f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -170,7 +175,7 @@ export type FunctionTypeAnnotationReturn =
|}>
| $ReadOnly<{|
type: 'PromiseTypeAnnotation',
resolvingType: FunctionTypeAnnotationReturn,
resolvedType: FunctionTypeAnnotationReturn,
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -3734,7 +3793,7 @@ Object {
"optional": false,
"params": Array [],
"returnTypeAnnotation": Object {
"resolvingType": Object {
"resolvedType": Object {
"type": "StringTypeAnnotation",
},
"type": "PromiseTypeAnnotation",
Expand All @@ -3748,7 +3807,7 @@ Object {
"optional": false,
"params": Array [],
"returnTypeAnnotation": Object {
"resolvingType": Object {
"resolvedType": Object {
"type": "StringTypeAnnotation",
},
"type": "PromiseTypeAnnotation",
Expand Down
23 changes: 21 additions & 2 deletions packages/react-native-codegen/src/parsers/flow/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function getTypeAnnotationForParam(
);
}
}
if (param.typeAnnotation.type === 'ObjectTypeAnnotation') {
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
return {
nullable,
name: paramName,
Expand All @@ -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 (
Expand Down Expand Up @@ -230,7 +249,7 @@ function getReturnTypeAnnotation(
) {
return {
type: 'PromiseTypeAnnotation',
resolvingType: getReturnTypeAnnotation(
resolvedType: getReturnTypeAnnotation(
methodName,
typeAnnotation.typeParameters.params[0],
types,
Expand Down

0 comments on commit a269c1f

Please sign in to comment.