react-native-code-gen Add Enum Type support for C++ TurboModules

Summary:
There are cases where we want to pass an enum type into a TurboModule method which is handy to restrict certain arguments to a restricted set of values, e.g. restricting quality to ```enum Quality { SD, HD, }```

Approach:
- We are not generating an ```enum``` type in C++ but rather just cast type safe to the corresponding member type.

- We don't support mixed enum types at this time, e.g. ```export enum StringOption { One = 'one', Two = 2, Three = 'three', };``` will not work. See: https://www.typescriptlang.org/docs/handbook/enums.html#heterogeneous-enums

- We only support untyped (default to String), String, and Number enum properties

This is for C++ only, Java and ObjC are not supported atm.

Changelog:
[General][Added] - react-native-code-gen Add Enum Type support for C++ TurboModules

Reviewed By: RSNara

Differential Revision: D38880963

fbshipit-source-id: f2399b29948306bc555429b6f96c43ea4c39c46e
This commit is contained in:
Christoph Purrer
2022-08-30 00:48:06 -07:00
committed by Facebook GitHub Bot
parent 621f4cf3b1
commit b444f0e44e
22 changed files with 361 additions and 15 deletions
@@ -146,6 +146,15 @@ function translatePrimitiveJSTypeToCpp(
return wrap('int');
case 'BooleanTypeAnnotation':
return wrap('bool');
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrap('double');
case 'StringTypeAnnotation':
return wrap('jsi::String');
default:
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
case 'GenericObjectTypeAnnotation':
return wrap('jsi::Object');
case 'UnionTypeAnnotation':