From 745f3ee8c571560406629bc7af3cf4914ef1b211 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Tue, 25 Oct 2022 08:27:47 -0700 Subject: [PATCH] react-native-code-gen Add Enum Type support for iOS/Android TurboModules Summary: Adding enum support to Android/iOS generated code Changelog: [General][Added] - react-native-code-gen Add Enum Type support for iOS/Android TurboModules Reviewed By: javache Differential Revision: D38967241 fbshipit-source-id: d8bb3019dab198e16905a6422e877cd751119122 --- .../modules/GenerateModuleJavaSpec.js | 27 +++++++++++ .../modules/GenerateModuleJniCpp.js | 33 +++++++++++++ .../GenerateModuleObjCpp/StructCollector.js | 4 +- .../header/serializeConstantsStruct.js | 22 +++++++++ .../header/serializeRegularStruct.js | 22 +++++++++ .../GenerateModuleObjCpp/serializeMethod.js | 33 +++++++++++++ .../modules/__test_fixtures__/fixtures.js | 36 ++++++++++++++ .../GenerateModuleCpp-test.js.snap | 4 ++ .../GenerateModuleH-test.js.snap | 9 ++++ .../GenerateModuleHObjCpp-test.js.snap | 3 ++ .../GenerateModuleJavaSpec-test.js.snap | 4 ++ .../GenerateModuleJniCpp-test.js.snap | 6 +++ .../GenerateModuleMm-test.js.snap | 7 +++ .../modules/__test_fixtures__/fixtures.js | 23 ++++++++- .../module-parser-snapshot-test.js.snap | 47 ++++++++++++++++++- .../src/parsers/flow/modules/index.js | 1 - .../modules/__test_fixtures__/fixtures.js | 25 +++++++++- ...script-module-parser-snapshot-test.js.snap | 47 ++++++++++++++++++- .../src/parsers/typescript/modules/index.js | 1 - 19 files changed, 347 insertions(+), 7 deletions(-) diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js index cfcf87c0597e44..0b807bbbfa966e 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js @@ -138,6 +138,15 @@ function translateFunctionParamToJavaType( return !isRequired ? 'Double' : 'double'; case 'BooleanTypeAnnotation': return !isRequired ? 'Boolean' : 'boolean'; + case 'EnumDeclaration': + switch (realTypeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return !isRequired ? 'Double' : 'double'; + case 'StringTypeAnnotation': + return wrapIntoNullableIfNeeded('String'); + default: + throw new Error(createErrorMessage(realTypeAnnotation.type)); + } case 'ObjectTypeAnnotation': imports.add('com.facebook.react.bridge.ReadableMap'); if (typeAnnotation.type === 'TypeAliasTypeAnnotation') { @@ -213,6 +222,15 @@ function translateFunctionReturnTypeToJavaType( return nullable ? 'Double' : 'double'; case 'BooleanTypeAnnotation': return nullable ? 'Boolean' : 'boolean'; + case 'EnumDeclaration': + switch (realTypeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return nullable ? 'Double' : 'double'; + case 'StringTypeAnnotation': + return wrapIntoNullableIfNeeded('String'); + default: + throw new Error(createErrorMessage(realTypeAnnotation.type)); + } case 'ObjectTypeAnnotation': imports.add('com.facebook.react.bridge.WritableMap'); return wrapIntoNullableIfNeeded('WritableMap'); @@ -269,6 +287,15 @@ function getFalsyReturnStatementFromReturnType( return nullable ? 'return null;' : 'return 0;'; case 'BooleanTypeAnnotation': return nullable ? 'return null;' : 'return false;'; + case 'EnumDeclaration': + switch (realTypeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return nullable ? 'return null;' : 'return 0;'; + case 'StringTypeAnnotation': + return nullable ? 'return null;' : 'return "";'; + default: + throw new Error(createErrorMessage(realTypeAnnotation.type)); + } case 'StringTypeAnnotation': return nullable ? 'return null;' : 'return "";'; case 'ObjectTypeAnnotation': diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js index 2703c2517227ad..891f4fa6103790 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js @@ -154,6 +154,17 @@ function translateReturnTypeToKind( return 'StringKind'; case 'BooleanTypeAnnotation': return 'BooleanKind'; + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return 'NumberKind'; + case 'StringTypeAnnotation': + return 'StringKind'; + default: + throw new Error( + `Unknown enum prop type for returning value, found: ${realTypeAnnotation.type}"`, + ); + } case 'NumberTypeAnnotation': return 'NumberKind'; case 'DoubleTypeAnnotation': @@ -212,6 +223,17 @@ function translateParamTypeToJniType( return 'Ljava/lang/String;'; case 'BooleanTypeAnnotation': return !isRequired ? 'Ljava/lang/Boolean;' : 'Z'; + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return !isRequired ? 'Ljava/lang/Double;' : 'D'; + case 'StringTypeAnnotation': + return 'Ljava/lang/String;'; + default: + throw new Error( + `Unknown enum prop type for method arg, found: ${realTypeAnnotation.type}"`, + ); + } case 'NumberTypeAnnotation': return !isRequired ? 'Ljava/lang/Double;' : 'D'; case 'DoubleTypeAnnotation': @@ -267,6 +289,17 @@ function translateReturnTypeToJniType( return 'Ljava/lang/String;'; case 'BooleanTypeAnnotation': return nullable ? 'Ljava/lang/Boolean;' : 'Z'; + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return nullable ? 'Ljava/lang/Double;' : 'D'; + case 'StringTypeAnnotation': + return 'Ljava/lang/String;'; + default: + throw new Error( + `Unknown enum prop type for method return type, found: ${realTypeAnnotation.type}"`, + ); + } case 'NumberTypeAnnotation': return nullable ? 'Ljava/lang/Double;' : 'D'; case 'DoubleTypeAnnotation': diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js index d3926812c3bd0d..ef7af6ae44a13e 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js @@ -19,6 +19,7 @@ import type { NativeModuleDoubleTypeAnnotation, NativeModuleFloatTypeAnnotation, NativeModuleBooleanTypeAnnotation, + NativeModuleEnumDeclaration, NativeModuleGenericObjectTypeAnnotation, ReservedTypeAnnotation, NativeModuleTypeAliasTypeAnnotation, @@ -63,6 +64,7 @@ export type StructTypeAnnotation = | NativeModuleDoubleTypeAnnotation | NativeModuleFloatTypeAnnotation | NativeModuleBooleanTypeAnnotation + | NativeModuleEnumDeclaration | NativeModuleGenericObjectTypeAnnotation | ReservedTypeAnnotation | NativeModuleTypeAliasTypeAnnotation @@ -113,7 +115,7 @@ class StructCollector { return wrapNullable(nullable, typeAnnotation); } case 'EnumDeclaration': - throw new Error('Enum types are unsupported in structs'); + return wrapNullable(nullable, typeAnnotation); case 'MixedTypeAnnotation': throw new Error('Mixed types are unsupported in structs'); case 'UnionTypeAnnotation': diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js index d030fdcf442a9a..0fa67683e66215 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js @@ -104,6 +104,17 @@ function toObjCType( return wrapOptional('double'); case 'BooleanTypeAnnotation': return wrapOptional('bool'); + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return wrapOptional('double'); + case 'StringTypeAnnotation': + return 'NSString *'; + default: + throw new Error( + `Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`, + ); + } case 'GenericObjectTypeAnnotation': return isRequired ? 'id ' : 'id _Nullable '; case 'ArrayTypeAnnotation': @@ -171,6 +182,17 @@ function toObjCValue( return wrapPrimitive('double'); case 'BooleanTypeAnnotation': return wrapPrimitive('BOOL'); + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return wrapPrimitive('double'); + case 'StringTypeAnnotation': + return value; + default: + throw new Error( + `Couldn't convert enum into ObjC value: ${typeAnnotation.type}"`, + ); + } case 'GenericObjectTypeAnnotation': return value; case 'ArrayTypeAnnotation': diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js index 9cc4aee79ab486..e6c73c73388892 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js @@ -95,6 +95,17 @@ function toObjCType( return wrapOptional('double'); case 'BooleanTypeAnnotation': return wrapOptional('bool'); + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return wrapOptional('double'); + case 'StringTypeAnnotation': + return 'NSString *'; + default: + throw new Error( + `Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`, + ); + } case 'GenericObjectTypeAnnotation': return isRequired ? 'id ' : 'id _Nullable'; case 'ArrayTypeAnnotation': @@ -161,6 +172,17 @@ function toObjCValue( return RCTBridgingTo('Double'); case 'BooleanTypeAnnotation': return RCTBridgingTo('Bool'); + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return RCTBridgingTo('Double'); + case 'StringTypeAnnotation': + return RCTBridgingTo('String'); + default: + throw new Error( + `Couldn't convert enum into ObjC value: ${typeAnnotation.type}"`, + ); + } case 'GenericObjectTypeAnnotation': return value; case 'ArrayTypeAnnotation': diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js index 91bcba56ec2ef9..ea84c731b2c653 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js @@ -271,6 +271,17 @@ function getParamObjCType( return notStruct(notRequired ? 'NSNumber *' : 'double'); case 'BooleanTypeAnnotation': return notStruct(notRequired ? 'NSNumber *' : 'BOOL'); + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return notStruct(notRequired ? 'NSNumber *' : 'double'); + case 'StringTypeAnnotation': + return notStruct(wrapIntoNullableIfNeeded('NSString *')); + default: + throw new Error( + `Unsupported enum type for param "${paramName}" in ${methodName}. Found: ${typeAnnotation.type}`, + ); + } case 'GenericObjectTypeAnnotation': return notStruct(wrapIntoNullableIfNeeded('NSDictionary *')); default: @@ -335,6 +346,17 @@ function getReturnObjCType( return wrapIntoNullableIfNeeded('NSNumber *'); case 'BooleanTypeAnnotation': return wrapIntoNullableIfNeeded('NSNumber *'); + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return wrapIntoNullableIfNeeded('NSNumber *'); + case 'StringTypeAnnotation': + return wrapIntoNullableIfNeeded('NSString *'); + default: + throw new Error( + `Unsupported enum return type for ${methodName}. Found: ${typeAnnotation.type}`, + ); + } case 'GenericObjectTypeAnnotation': return wrapIntoNullableIfNeeded('NSDictionary *'); default: @@ -380,6 +402,17 @@ function getReturnJSType( return 'BooleanKind'; case 'GenericObjectTypeAnnotation': return 'ObjectKind'; + case 'EnumDeclaration': + switch (typeAnnotation.memberType) { + case 'NumberTypeAnnotation': + return 'NumberKind'; + case 'StringTypeAnnotation': + return 'StringKind'; + default: + throw new Error( + `Unsupported return type for ${methodName}. Found: ${typeAnnotation.type}`, + ); + } default: (typeAnnotation.type: | 'EnumDeclaration' diff --git a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js index b9064512c97052..991d76f2dd7456 100644 --- a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js @@ -275,6 +275,42 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { ], }, }, + { + name: 'getEnums', + optional: false, + typeAnnotation: { + type: 'FunctionTypeAnnotation', + returnTypeAnnotation: { + type: 'StringTypeAnnotation', + }, + params: [ + { + name: 'enumInt', + optional: false, + typeAnnotation: { + type: 'EnumDeclaration', + memberType: 'NumberTypeAnnotation', + }, + }, + { + name: 'enumFloat', + optional: false, + typeAnnotation: { + type: 'EnumDeclaration', + memberType: 'NumberTypeAnnotation', + }, + }, + { + name: 'enumString', + optional: false, + typeAnnotation: { + type: 'EnumDeclaration', + memberType: 'StringTypeAnnotation', + }, + }, + ], + }, + }, ], }, moduleNames: ['SampleTurboModule'], diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap index 2dd9ca5ce68405..7446e0559ce36e 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap @@ -337,6 +337,9 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithC static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithPromise(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->getValueWithPromise(rt, args[0].asBool()); } +static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getEnums(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getEnums(rt, args[0].asNumber(), args[1].asNumber(), args[2].asString(rt)); +} NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule(\\"SampleTurboModule\\", jsInvoker) { @@ -351,6 +354,7 @@ NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared methodMap_[\\"getValue\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValue}; methodMap_[\\"getValueWithCallback\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithCallback}; methodMap_[\\"getValueWithPromise\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithPromise}; + methodMap_[\\"getEnums\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getEnums}; } diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap index 461e34715e676e..63d09d5948b265 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap @@ -668,6 +668,7 @@ public: virtual jsi::Object getValue(jsi::Runtime &rt, double x, jsi::String y, jsi::Object z) = 0; virtual void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) = 0; virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0; + virtual jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) = 0; }; @@ -777,6 +778,14 @@ private: return bridging::callFromJs( rt, &T::getValueWithPromise, jsInvoker_, instance_, std::move(error)); } + jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) override { + static_assert( + bridging::getParameterCount(&T::getEnums) == 4, + \\"Expected getEnums(...) to have 4 parameters\\"); + + return bridging::callFromJs( + rt, &T::getEnums, jsInvoker_, instance_, std::move(enumInt), std::move(enumFloat), std::move(enumString)); + } private: T *instance_; diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap index 07ab8ba9129c19..119fb989e7a102 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap @@ -933,6 +933,9 @@ namespace JS { - (void)getValueWithPromise:(BOOL)error resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject; +- (NSString *)getEnums:(double)enumInt + enumFloat:(double)enumFloat + enumString:(NSString *)enumString; - (facebook::react::ModuleConstants)constantsToExport; - (facebook::react::ModuleConstants)getConstants; diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap index 3fec5218b9ffa3..4d41e02ccd04f8 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap @@ -376,6 +376,10 @@ public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaMo @ReactMethod @DoNotStrip public abstract void getValueWithPromise(boolean error, Promise promise); + + @ReactMethod(isBlockingSynchronousMethod = true) + @DoNotStrip + public abstract String getEnums(double enumInt, double enumFloat, String enumString); } ", } diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap index 9d771b73dddaa2..3fdf54878310b0 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap @@ -384,6 +384,11 @@ static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getVal return static_cast(turboModule).invokeJavaMethod(rt, PromiseKind, \\"getValueWithPromise\\", \\"(ZLcom/facebook/react/bridge/Promise;)V\\", args, count, cachedMethodId); } +static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + static jmethodID cachedMethodId = nullptr; + return static_cast(turboModule).invokeJavaMethod(rt, StringKind, \\"getEnums\\", \\"(DDLjava/lang/String;)Ljava/lang/String;\\", args, count, cachedMethodId); +} + NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboModule::InitParams ¶ms) : JavaTurboModule(params) { methodMap_[\\"getConstants\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants}; @@ -397,6 +402,7 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboMo methodMap_[\\"getValue\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getValue}; methodMap_[\\"getValueWithCallback\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback}; methodMap_[\\"getValueWithPromise\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise}; + methodMap_[\\"getEnums\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums}; } std::shared_ptr simple_native_modules_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams ¶ms) { diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap index 3b2acb73c23930..d4b708e3d9023d 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap @@ -453,6 +453,10 @@ namespace facebook { return static_cast(turboModule).invokeObjCMethod(rt, PromiseKind, \\"getValueWithPromise\\", @selector(getValueWithPromise:resolve:reject:), args, count); } + static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeObjCMethod(rt, StringKind, \\"getEnums\\", @selector(getEnums:enumFloat:enumString:), args, count); + } + static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { return static_cast(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getConstants\\", @selector(getConstants), args, count); } @@ -490,6 +494,9 @@ namespace facebook { methodMap_[\\"getValueWithPromise\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise}; + methodMap_[\\"getEnums\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums}; + + methodMap_[\\"getConstants\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants}; } diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js index 2d73c47301050d..50e4e18aa4c2c6 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js @@ -569,8 +569,29 @@ const IOS_ONLY_NATIVE_MODULE = ` import type {TurboModule} from '../RCTExport'; import * as TurboModuleRegistry from '../TurboModuleRegistry'; +export enum Quality { + SD, + HD, +} + +export enum Resolution { + Low = 720, + High = 1080, +} + +export enum Floppy { + LowDensity = 0.72, + HighDensity = 1.44, +} + +export enum StringOptions { + One = 'one', + Two = 'two', + Three = 'three', +} + export interface Spec extends TurboModule { - // no methods + +getEnums: (quality: Quality, resolution?: Resolution, floppy: Floppy, stringOptions: StringOptions) => string; } export default TurboModuleRegistry.getEnforcing('SampleTurboModuleIOS'); diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap index bf9ab323ea9d52..e73868dc872a3c 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap @@ -205,7 +205,52 @@ exports[`RN Codegen Flow Parser can generate fixture IOS_ONLY_NATIVE_MODULE 1`] 'type': 'NativeModule', 'aliases': {}, 'spec': { - 'properties': [] + 'properties': [ + { + 'name': 'getEnums', + 'optional': false, + 'typeAnnotation': { + 'type': 'FunctionTypeAnnotation', + 'returnTypeAnnotation': { + 'type': 'StringTypeAnnotation' + }, + 'params': [ + { + 'name': 'quality', + 'optional': false, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'StringTypeAnnotation' + } + }, + { + 'name': 'resolution', + 'optional': true, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'NumberTypeAnnotation' + } + }, + { + 'name': 'floppy', + 'optional': false, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'NumberTypeAnnotation' + } + }, + { + 'name': 'stringOptions', + 'optional': false, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'StringTypeAnnotation' + } + } + ] + } + } + ] }, 'moduleNames': [ 'SampleTurboModuleIOS' diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 914bb27128bcff..747f9162c2823b 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -252,7 +252,6 @@ function translateTypeAnnotation( default: { const maybeEumDeclaration = types[typeAnnotation.id.name]; if ( - cxxOnly && maybeEumDeclaration && maybeEumDeclaration.type === 'EnumDeclaration' ) { diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js index 61ad3642e6d232..02a2ae68b98a14 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js @@ -588,7 +588,30 @@ const IOS_ONLY_NATIVE_MODULE = ` import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry'; -export interface Spec extends TurboModule {} +export enum Quality { + SD, + HD, +} + +export enum Resolution { + Low = 720, + High = 1080, +} + +export enum Floppy { + LowDensity = 0.72, + HighDensity = 1.44, +} + +export enum StringOptions { + One = 'one', + Two = 'two', + Three = 'three', +} + +export interface Spec extends TurboModule { + readonly getEnums: (quality: Quality, resolution?: Resolution, floppy: Floppy, stringOptions: StringOptions) => string; +} export default TurboModuleRegistry.getEnforcing( 'SampleTurboModuleIOS', diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap b/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap index 03a0b11a41f1ab..6b4ff88450528a 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap +++ b/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap @@ -203,7 +203,52 @@ exports[`RN Codegen TypeScript Parser can generate fixture IOS_ONLY_NATIVE_MODUL 'type': 'NativeModule', 'aliases': {}, 'spec': { - 'properties': [] + 'properties': [ + { + 'name': 'getEnums', + 'optional': false, + 'typeAnnotation': { + 'type': 'FunctionTypeAnnotation', + 'returnTypeAnnotation': { + 'type': 'StringTypeAnnotation' + }, + 'params': [ + { + 'name': 'quality', + 'optional': false, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'StringTypeAnnotation' + } + }, + { + 'name': 'resolution', + 'optional': true, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'NumberTypeAnnotation' + } + }, + { + 'name': 'floppy', + 'optional': false, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'NumberTypeAnnotation' + } + }, + { + 'name': 'stringOptions', + 'optional': false, + 'typeAnnotation': { + 'type': 'EnumDeclaration', + 'memberType': 'StringTypeAnnotation' + } + } + ] + } + } + ] }, 'moduleNames': [ 'SampleTurboModuleIOS' diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 3c26d7ab84d0df..097a505f8898a8 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -264,7 +264,6 @@ function translateTypeAnnotation( default: { const maybeEumDeclaration = types[typeAnnotation.typeName.name]; if ( - cxxOnly && maybeEumDeclaration && maybeEumDeclaration.type === 'TSEnumDeclaration' ) {