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
@@ -156,7 +156,10 @@ function translateFunctionParamToJavaType(
imports.add('com.facebook.react.bridge.Callback');
return 'Callback';
default:
(realTypeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
(realTypeAnnotation.type:
| 'EnumDeclaration'
| 'MixedTypeAnnotation'
| 'UnionTypeAnnotation');
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
}
@@ -220,7 +223,10 @@ function translateFunctionReturnTypeToJavaType(
imports.add('com.facebook.react.bridge.WritableArray');
return wrapIntoNullableIfNeeded('WritableArray');
default:
(realTypeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
(realTypeAnnotation.type:
| 'EnumDeclaration'
| 'MixedTypeAnnotation'
| 'UnionTypeAnnotation');
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
}
@@ -272,7 +278,10 @@ function getFalsyReturnStatementFromReturnType(
case 'ArrayTypeAnnotation':
return 'return null;';
default:
(realTypeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
(realTypeAnnotation.type:
| 'EnumDeclaration'
| 'MixedTypeAnnotation'
| 'UnionTypeAnnotation');
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
}