From 2deb39b8f65b58f33ce8a5d78a2291faa452cd78 Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Tue, 18 Jun 2019 06:26:31 -0700 Subject: [PATCH] Add support for enum event arguments Summary: This diff adds codegen support for event arguments such as: ``` type Event = $ReadOnly<{| orientation: ('portrait' | 'landscape'), |}>; ``` Facebook #noautofix Reviewed By: shergin Differential Revision: D15769046 fbshipit-source-id: b2ad8d044b06c2394a9aa75a198ea504672cde68 --- .../react-native-codegen/src/CodegenSchema.js | 8 + .../src/generators/GenerateEventEmitterCpp.js | 15 + .../src/generators/GenerateEventEmitterH.js | 48 +- .../generators/__test_fixtures__/fixtures.js | 26 + .../GenerateEventEmitterCpp-test.js.snap | 7 + .../GenerateEventEmitterH-test.js.snap | 18 + .../GenerateViewConfigJs-test.js.snap | 5 + .../flow/__test_fixtures__/fixtures.js | 5 + .../__snapshots__/parser-test.js.snap | 936 ++++++++++++++++++ .../src/parsers/flow/events.js | 10 +- .../src/parsers/flow/props.js | 2 + 11 files changed, 1078 insertions(+), 2 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index e4b877a95ef..60eccbc0149 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -31,6 +31,14 @@ export type ObjectPropertyType = name: string, optional: boolean, |}> + | $ReadOnly<{| + type: 'StringEnumTypeAnnotation', + name: string, + optional: boolean, + options: $ReadOnlyArray<{| + name: string, + |}>, + |}> | $ReadOnly<{| type: 'ObjectTypeAnnotation', name: string, diff --git a/packages/react-native-codegen/src/generators/GenerateEventEmitterCpp.js b/packages/react-native-codegen/src/generators/GenerateEventEmitterCpp.js index c6820ddc502..25d8048a531 100644 --- a/packages/react-native-codegen/src/generators/GenerateEventEmitterCpp.js +++ b/packages/react-native-codegen/src/generators/GenerateEventEmitterCpp.js @@ -70,6 +70,15 @@ function generateSetter(variableName, propertyName, propertyParts) { return `${variableName}.setProperty(runtime, "${propertyName}", ${eventChain}`; } +function generateEnumSetter(variableName, propertyName, propertyParts) { + const trailingPeriod = propertyParts.length === 0 ? '' : '.'; + const eventChain = `event.${propertyParts.join( + '.', + )}${trailingPeriod}${propertyName})`; + + return `${variableName}.setProperty(runtime, "${propertyName}", toString(${eventChain});`; +} + function generateSetters( parentPropertyName: string, properties: $ReadOnlyArray, @@ -87,6 +96,12 @@ function generateSetters( eventProperty.name, propertyParts, ); + case 'StringEnumTypeAnnotation': + return generateEnumSetter( + parentPropertyName, + eventProperty.name, + propertyParts, + ); case 'ObjectTypeAnnotation': const propertyName = eventProperty.name; return ` diff --git a/packages/react-native-codegen/src/generators/GenerateEventEmitterH.js b/packages/react-native-codegen/src/generators/GenerateEventEmitterH.js index 075f124c25e..0ae3b48e230 100644 --- a/packages/react-native-codegen/src/generators/GenerateEventEmitterH.js +++ b/packages/react-native-codegen/src/generators/GenerateEventEmitterH.js @@ -12,7 +12,7 @@ const nullthrows = require('nullthrows'); -const {getCppTypeForAnnotation} = require('./CppHelpers.js'); +const {getCppTypeForAnnotation, toSafeCppString} = require('./CppHelpers.js'); const {generateStructName} = require('./EventEmitterHelpers.js'); import type { @@ -67,6 +67,16 @@ struct ::_STRUCT_NAME_:: { }; `.trim(); +const enumTemplate = `enum class ::_ENUM_NAME_:: { + ::_VALUES_:: +}; + +inline char const *toString(const ::_ENUM_NAME_:: value) { + switch (value) { + ::_TO_CASES_:: + } +}`.trim(); + function getNativeTypeFromAnnotation( componentName: string, eventProperty: ObjectPropertyType, @@ -80,6 +90,11 @@ function getNativeTypeFromAnnotation( case 'Int32TypeAnnotation': case 'FloatTypeAnnotation': return getCppTypeForAnnotation(type); + case 'StringEnumTypeAnnotation': + return generateStructName( + componentName, + nameParts.concat([eventProperty.name]), + ); case 'ObjectTypeAnnotation': return generateStructName( componentName, @@ -90,6 +105,29 @@ function getNativeTypeFromAnnotation( throw new Error(`Receieved invalid event property type ${type}`); } } +function generateEnum(structs, componentName, options, nameParts) { + const structName = generateStructName(componentName, nameParts); + const fields = options + .map((option, index) => `${toSafeCppString(option.name)}`) + .join(',\n '); + + const toCases = options + .map( + option => + `case ${structName}::${toSafeCppString(option.name)}: return "${ + option.name + }";`, + ) + .join('\n' + ' '); + + structs.set( + structName, + enumTemplate + .replace(/::_ENUM_NAME_::/g, structName) + .replace('::_VALUES_::', fields) + .replace('::_TO_CASES_::', toCases), + ); +} function generateStruct( structs: StructsMap, @@ -126,6 +164,14 @@ function generateStruct( nullthrows(property.properties), ); return; + case 'StringEnumTypeAnnotation': + generateEnum( + structs, + componentName, + property.options, + nameParts.concat([name]), + ); + return; default: (property: empty); throw new Error( diff --git a/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js index eac79087086..60d0b93f517 100644 --- a/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js @@ -587,6 +587,32 @@ const EVENT_PROPS: SchemaType = { }, }, }, + { + name: 'onOrientationChange', + optional: true, + bubblingType: 'direct', + typeAnnotation: { + type: 'EventTypeAnnotation', + argument: { + type: 'ObjectTypeAnnotation', + properties: [ + { + type: 'StringEnumTypeAnnotation', + name: 'orientation', + optional: false, + options: [ + { + name: 'landscape', + }, + { + name: 'portrait', + }, + ], + }, + ], + }, + }, + }, { name: 'onEnd', optional: true, diff --git a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterCpp-test.js.snap b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterCpp-test.js.snap index 25846b02b5d..67028095e3a 100644 --- a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterCpp-test.js.snap @@ -165,6 +165,13 @@ void EventsNativeComponentEventEmitter::onEventDirect(EventsNativeComponentOnEve return payload; }); } +void EventsNativeComponentEventEmitter::onOrientationChange(EventsNativeComponentOnOrientationChangeStruct event) const { + dispatchEvent(\\"orientationChange\\", [event=std::move(event)](jsi::Runtime &runtime) { + auto payload = jsi::Object(runtime); + payload.setProperty(runtime, \\"orientation\\", toString(event.orientation)); + return payload; + }); +} void EventsNativeComponentEventEmitter::onEnd() const { dispatchEvent(\\"end\\"); } diff --git a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterH-test.js.snap b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterH-test.js.snap index bff692ada26..4e0fe22c6b2 100644 --- a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterH-test.js.snap +++ b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateEventEmitterH-test.js.snap @@ -166,6 +166,22 @@ struct EventsNativeComponentOnEventDirectStruct { bool value; }; +enum class EventsNativeComponentOnOrientationChangeOrientationStruct { + Landscape, + Portrait +}; + +inline char const *toString(const EventsNativeComponentOnOrientationChangeOrientationStruct value) { + switch (value) { + case EventsNativeComponentOnOrientationChangeOrientationStruct::Landscape: return \\"landscape\\"; + case EventsNativeComponentOnOrientationChangeOrientationStruct::Portrait: return \\"portrait\\"; + } +} + +struct EventsNativeComponentOnOrientationChangeStruct { + EventsNativeComponentOnOrientationChangeOrientationStruct orientation; +}; + class EventsNativeComponentEventEmitter : public ViewEventEmitter { public: using ViewEventEmitter::ViewEventEmitter; @@ -174,6 +190,8 @@ class EventsNativeComponentEventEmitter : public ViewEventEmitter { void onEventDirect(EventsNativeComponentOnEventDirectStruct value) const; + void onOrientationChange(EventsNativeComponentOnOrientationChangeStruct value) const; + void onEnd() const; }; diff --git a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap index 76e05e9d2ac..5edc66981b4 100644 --- a/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap +++ b/packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap @@ -221,12 +221,17 @@ const EventsNativeComponentViewConfig = { topEventDirect: { registrationName: 'onEventDirect', }, + + topOrientationChange: { + registrationName: 'onOrientationChange', + }, }, validAttributes: { disabled: true, onChange: true, onEventDirect: true, + onOrientationChange: true, onEnd: true, }, }; diff --git a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js index b993455aaff..e5fec940091 100644 --- a/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js @@ -30,6 +30,11 @@ const EVENT_DEFINITION = ` int32_optional_value: ?Int32, int32_optional_both?: ?Int32, + enum_required: ('small' | 'large'), + enum_optional_key?: ('small' | 'large'), + enum_optional_value: ?('small' | 'large'), + enum_optional_both?: ?('small' | 'large'), + object_required: { boolean_required: boolean, } diff --git a/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap index bfce6c25a94..4c1195454ab 100644 --- a/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/__tests__/__snapshots__/parser-test.js.snap @@ -1162,6 +1162,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -1337,6 +1389,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -1512,6 +1616,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -1687,6 +1843,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -1862,6 +2070,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -2037,6 +2297,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -2212,6 +2524,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -2387,6 +2751,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -2584,6 +3000,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -2759,6 +3227,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -2934,6 +3454,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -3109,6 +3681,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -3284,6 +3908,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -3459,6 +4135,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -3634,6 +4362,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -3809,6 +4589,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -4063,6 +4895,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, @@ -4238,6 +5122,58 @@ Object { "optional": true, "type": "Int32TypeAnnotation", }, + Object { + "name": "enum_required", + "optional": false, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_key", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_value", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, + Object { + "name": "enum_optional_both", + "optional": true, + "options": Array [ + Object { + "name": "small", + }, + Object { + "name": "large", + }, + ], + "type": "StringEnumTypeAnnotation", + }, Object { "name": "object_required", "optional": false, diff --git a/packages/react-native-codegen/src/parsers/flow/events.js b/packages/react-native-codegen/src/parsers/flow/events.js index 263acf32e25..10d5ce2afef 100644 --- a/packages/react-native-codegen/src/parsers/flow/events.js +++ b/packages/react-native-codegen/src/parsers/flow/events.js @@ -50,8 +50,16 @@ function getPropertyType(name, optional, typeAnnotation) { optional, properties: typeAnnotation.properties.map(buildPropertiesForEvent), }; + case 'UnionTypeAnnotation': + return { + type: 'StringEnumTypeAnnotation', + name, + optional, + options: typeAnnotation.types.map(option => ({name: option.value})), + }; default: - throw new Error(`Unable to determine type for "${name}"`); + (type: empty); + throw new Error(`Unable to determine event type for "${name}"`); } } diff --git a/packages/react-native-codegen/src/parsers/flow/props.js b/packages/react-native-codegen/src/parsers/flow/props.js index 09ff6a67ae8..d062f25f667 100644 --- a/packages/react-native-codegen/src/parsers/flow/props.js +++ b/packages/react-native-codegen/src/parsers/flow/props.js @@ -79,6 +79,7 @@ function getTypeAnnotationForArray(name, typeAnnotation, defaultValue) { options: typeAnnotation.types.map(option => ({name: option.value})), }; default: + (type: empty); throw new Error(`Unknown prop type for "${name}"`); } } @@ -177,6 +178,7 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue) { } throw new Error(`A default enum value is required for "${name}"`); default: + (type: empty); throw new Error(`Unknown prop type for "${name}"`); } }