From 6d7e763e8512c33c0fa59e2e90ab5791c445550f Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 1 Oct 2020 19:26:14 -0700 Subject: [PATCH] Make JniCpp Generator handle NullableTypeAnnotation Summary: Changelog: [Internal] Reviewed By: hramos Differential Revision: D24027245 fbshipit-source-id: 73f9e7ec7ccb7627d2df63798a4269c860388dde --- .../modules/GenerateModuleJniCpp.js | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js index 72b48a7fcda..ecaf21c571c 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js @@ -11,6 +11,7 @@ 'use strict'; import type { + Nullable, SchemaType, NativeModulePropertySchema, NativeModuleMethodParamSchema, @@ -19,6 +20,7 @@ import type { import type {AliasResolver} from './Utils'; const {createAliasResolver, getModules} = require('./Utils'); +const {unwrapNullable} = require('../../parsers/flow/modules/utils'); type FilesOutput = Map; @@ -75,9 +77,10 @@ std::shared_ptr ::_LIBRARY_NAME_::_ModuleProvider(const std::string `; function translateReturnTypeToKind( - typeAnnotation: NativeModuleReturnTypeAnnotation, + nullableTypeAnnotation: Nullable, resolveAlias: AliasResolver, ): string { + const [typeAnnotation] = unwrapNullable(nullableTypeAnnotation); let realTypeAnnotation = typeAnnotation; if (realTypeAnnotation.type === 'TypeAliasTypeAnnotation') { realTypeAnnotation = resolveAlias(realTypeAnnotation.name); @@ -128,8 +131,9 @@ function translateParamTypeToJniType( param: NativeModuleMethodParamSchema, resolveAlias: AliasResolver, ): string { - const {optional, typeAnnotation} = param; - const isRequired = !optional && !typeAnnotation.nullable; + const {optional, typeAnnotation: nullableTypeAnnotation} = param; + const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation); + const isRequired = !optional && !nullable; let realTypeAnnotation = typeAnnotation; if (realTypeAnnotation.type === 'TypeAliasTypeAnnotation') { @@ -176,10 +180,10 @@ function translateParamTypeToJniType( } function translateReturnTypeToJniType( - typeAnnotation: NativeModuleReturnTypeAnnotation, + nullableTypeAnnotation: Nullable, resolveAlias: AliasResolver, ): string { - const {nullable} = typeAnnotation; + const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation); let realTypeAnnotation = typeAnnotation; if (realTypeAnnotation.type === 'TypeAliasTypeAnnotation') { @@ -232,14 +236,13 @@ function translateMethodTypeToJniSignature( resolveAlias: AliasResolver, ): string { const {name, typeAnnotation} = property; - const {returnTypeAnnotation} = typeAnnotation; + let [{returnTypeAnnotation, params}] = unwrapNullable(typeAnnotation); - const params = [...typeAnnotation.params]; + params = [...params]; let processedReturnTypeAnnotation = returnTypeAnnotation; const isPromiseReturn = returnTypeAnnotation.type === 'PromiseTypeAnnotation'; if (isPromiseReturn) { processedReturnTypeAnnotation = { - nullable: false, type: 'VoidTypeAnnotation', }; } @@ -269,12 +272,13 @@ function translateMethodForImplementation( property: NativeModulePropertySchema, resolveAlias: AliasResolver, ): string { - const {returnTypeAnnotation} = property.typeAnnotation; + const [propertyTypeAnnotation] = unwrapNullable(property.typeAnnotation); + const {returnTypeAnnotation} = propertyTypeAnnotation; const numberOfParams = - property.typeAnnotation.params.length + + propertyTypeAnnotation.params.length + (returnTypeAnnotation.type === 'PromiseTypeAnnotation' ? 1 : 0); - const translatedArguments = property.typeAnnotation.params + const translatedArguments = propertyTypeAnnotation.params .map(param => param.name) .concat( returnTypeAnnotation.type === 'PromiseTypeAnnotation' ? ['promise'] : [], @@ -331,20 +335,20 @@ module.exports = { .replace( '::_PROPERTIES_MAP_::', properties - .map( - ({ - name: propertyName, - typeAnnotation: {params, returnTypeAnnotation}, - }) => - propertyName === 'getConstants' && + .map(({name: propertyName, typeAnnotation}) => { + const [{returnTypeAnnotation, params}] = unwrapNullable( + typeAnnotation, + ); + + return propertyName === 'getConstants' && returnTypeAnnotation.type === 'ObjectTypeAnnotation' && returnTypeAnnotation.properties && returnTypeAnnotation.properties.length === 0 - ? '' - : propertyDefTemplate - .replace(/::_PROPERTY_NAME_::/g, propertyName) - .replace(/::_ARGS_COUNT_::/g, params.length.toString()), - ) + ? '' + : propertyDefTemplate + .replace(/::_PROPERTY_NAME_::/g, propertyName) + .replace(/::_ARGS_COUNT_::/g, params.length.toString()); + }) .join('\n'), ) .replace(/::_MODULE_NAME_::/g, name);