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 authored and Facebook GitHub Bot committed 2022-08-30 00:48:06 -07:00
1 parent 621f4cf3b1
commit b444f0e44e
22 files changed
+361 -15

No files matched your search

@@ -338,7 +338,10 @@ function getReturnObjCType(
case 'GenericObjectTypeAnnotation':
return wrapIntoNullableIfNeeded('NSDictionary *');
default:
(typeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
(typeAnnotation.type:
| 'EnumDeclaration'
| 'MixedTypeAnnotation'
| 'UnionTypeAnnotation');
throw new Error(
`Unsupported return type for ${methodName}. Found: ${typeAnnotation.type}`,
);
@@ -378,7 +381,10 @@ function getReturnJSType(
case 'GenericObjectTypeAnnotation':
return 'ObjectKind';
default:
(typeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
(typeAnnotation.type:
| 'EnumDeclaration'
| 'MixedTypeAnnotation'
| 'UnionTypeAnnotation');
throw new Error(
`Unsupported return type for ${methodName}. Found: ${typeAnnotation.type}`,
);