diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index a1d4dc742cb5bc..17b3c933b326b7 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -27,6 +27,7 @@ const { getEventArgument, buildPropertiesForEvent, handleEventHandler, + emitBuildEventSchema, } = require('../../parsers-commons'); const { emitBoolProp, @@ -245,36 +246,19 @@ function buildEventSchema( ); const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name); - if (paperTopLevelNameDeprecated != null) { - return { - name, - optional, - bubblingType: nonNullableBubblingType, - paperTopLevelNameDeprecated, - typeAnnotation: { - type: 'EventTypeAnnotation', - argument: getEventArgument( - nonNullableArgumentProps, - parser, - getPropertyType, - ), - }, - }; - } + const argument = getEventArgument( + nonNullableArgumentProps, + parser, + getPropertyType, + ); - return { + return emitBuildEventSchema( + paperTopLevelNameDeprecated, name, optional, - bubblingType: nonNullableBubblingType, - typeAnnotation: { - type: 'EventTypeAnnotation', - argument: getEventArgument( - nonNullableArgumentProps, - parser, - getPropertyType, - ), - }, - }; + nonNullableBubblingType, + argument, + ); } // $FlowFixMe[unclear-type] there's no flowtype for ASTs diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index da81ac62ec84ac..46fc5ef397937f 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -26,6 +26,7 @@ import type { PropTypeAnnotation, EventTypeAnnotation, ObjectTypeAnnotation, + EventTypeShape, } from '../CodegenSchema.js'; import type {Parser} from './parser'; @@ -1125,6 +1126,37 @@ function handleEventHandler( } } +function emitBuildEventSchema( + paperTopLevelNameDeprecated: $FlowFixMe, + name: $FlowFixMe, + optional: $FlowFixMe, + nonNullableBubblingType: 'direct' | 'bubble', + argument: ObjectTypeAnnotation, +): ?EventTypeShape { + if (paperTopLevelNameDeprecated != null) { + return { + name, + optional, + bubblingType: nonNullableBubblingType, + paperTopLevelNameDeprecated, + typeAnnotation: { + type: 'EventTypeAnnotation', + argument: argument, + }, + }; + } + + return { + name, + optional, + bubblingType: nonNullableBubblingType, + typeAnnotation: { + type: 'EventTypeAnnotation', + argument: argument, + }, + }; +} + module.exports = { wrapModuleSchema, unwrapNullable, @@ -1154,4 +1186,5 @@ module.exports = { buildPropertiesForEvent, verifyPropNotAlreadyDefined, handleEventHandler, + emitBuildEventSchema, }; diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 83379b2ed67e36..0be6d104d580f0 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -28,6 +28,7 @@ const { getEventArgument, buildPropertiesForEvent, handleEventHandler, + emitBuildEventSchema, } = require('../../parsers-commons'); const { emitBoolProp, @@ -259,36 +260,19 @@ function buildEventSchema( ); const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name); - if (paperTopLevelNameDeprecated != null) { - return { - name, - optional, - bubblingType: nonNullableBubblingType, - paperTopLevelNameDeprecated, - typeAnnotation: { - type: 'EventTypeAnnotation', - argument: getEventArgument( - nonNullableArgumentProps, - parser, - getPropertyType, - ), - }, - }; - } + const argument = getEventArgument( + nonNullableArgumentProps, + parser, + getPropertyType, + ); - return { + return emitBuildEventSchema( + paperTopLevelNameDeprecated, name, optional, - bubblingType: nonNullableBubblingType, - typeAnnotation: { - type: 'EventTypeAnnotation', - argument: getEventArgument( - nonNullableArgumentProps, - parser, - getPropertyType, - ), - }, - }; + nonNullableBubblingType, + argument, + ); } function getEvents(