diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 4495cc0476..57c11d3188 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -137,6 +137,14 @@ export type FunctionTypeAnnotationParamTypeAnnotation = $ReadOnly<{| | 'BooleanTypeAnnotation', |}>; +export type FunctionTypeAnnotationReturn = $ReadOnly<{| + type: + | 'StringTypeAnnotation' + | 'NumberTypeAnnotation' + | 'BooleanTypeAnnotation' + | 'VoidTypeAnnotation', +|}>; + export type FunctionTypeAnnotationParam = $ReadOnly<{| name: string, typeAnnotation: FunctionTypeAnnotationParamTypeAnnotation, @@ -145,6 +153,7 @@ export type FunctionTypeAnnotationParam = $ReadOnly<{| export type FunctionTypeAnnotation = $ReadOnly<{| type: 'FunctionTypeAnnotation', params: $ReadOnlyArray, + returnTypeAnnotation: FunctionTypeAnnotationReturn, |}>; export type MethodTypeShape = $ReadOnly<{| diff --git a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js index 3fe79a7f13..f55709cbdd 100644 --- a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js +++ b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js @@ -10,7 +10,31 @@ 'use strict'; -const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE = ` +const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM = ` +/** + * 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 { + getString: (arg: NotString) => string; +} + +export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); + +`; + +const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN = ` /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -253,7 +277,8 @@ export default codegenNativeComponent('Module'); module.exports = { TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT, - NATIVE_MODULES_WITH_NOT_EXISTING_TYPE, + NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM, + NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN, NATIVE_MODULES_WITH_NOT_ONLY_METHODS, INCORRECT_NATIVE_MODULES, COMMANDS_DEFINED_INLINE, diff --git a/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap index 232b25de63..7860aa583e 100644 --- a/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap @@ -8,7 +8,9 @@ exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_WITHOU exports[`RN Codegen Flow Parser Fails with error message INCORRECT_NATIVE_MODULES 1`] = `"Interface properties for \\"SpecWithTypo has been specified incorrectly.\\""`; -exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found GenericTypeAnnotation"`; +exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found GenericTypeAnnotation"`; + +exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found GenericTypeAnnotation"`; exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_ONLY_METHODS 1`] = `"Only methods are supported as module properties. Found BooleanTypeAnnotation in sampleBool"`; @@ -3336,6 +3338,9 @@ Object { }, }, ], + "returnTypeAnnotation": Object { + "type": "VoidTypeAnnotation", + }, "type": "FunctionTypeAnnotation", }, }, @@ -3350,6 +3355,9 @@ Object { }, }, ], + "returnTypeAnnotation": Object { + "type": "VoidTypeAnnotation", + }, "type": "FunctionTypeAnnotation", }, }, @@ -3364,6 +3372,9 @@ Object { }, }, ], + "returnTypeAnnotation": Object { + "type": "VoidTypeAnnotation", + }, "type": "FunctionTypeAnnotation", }, }, diff --git a/packages/react-native-codegen/src/parsers/flow/methods.js b/packages/react-native-codegen/src/parsers/flow/methods.js index 117b597bf6..5eadb2c3a5 100644 --- a/packages/react-native-codegen/src/parsers/flow/methods.js +++ b/packages/react-native-codegen/src/parsers/flow/methods.js @@ -14,6 +14,7 @@ import type { MethodTypeShape, FunctionTypeAnnotationParam, FunctionTypeAnnotationParamTypeAnnotation, + FunctionTypeAnnotationReturn, } from '../../CodegenSchema.js'; function wrapPrimitiveIntoTypeAnnotation( @@ -53,6 +54,25 @@ function getTypeAnnotationForParam( }; } +function getReturnTypeAnnotation( + methodName: string, + type, +): FunctionTypeAnnotationReturn { + switch (type) { + case 'BooleanTypeAnnotation': + case 'NumberTypeAnnotation': + case 'StringTypeAnnotation': + case 'VoidTypeAnnotation': + return { + type, + }; + default: + (type: empty); + throw new Error( + `Unsupported return type for method "${methodName}", Found ${type}`, + ); + } +} function buildMethodSchema(property: MethodAST): MethodTypeShape { const name: string = property.key.name; const value = property.value; @@ -67,10 +87,16 @@ function buildMethodSchema(property: MethodAST): MethodTypeShape { const params = value.params.map(param => getTypeAnnotationForParam(name, param), ); + + const returnTypeAnnotation = getReturnTypeAnnotation( + name, + value.returnType.type, + ); return { name, typeAnnotation: { type: 'FunctionTypeAnnotation', + returnTypeAnnotation, params, }, };